#region Apache Notice /***************************************************************************** * $Header: $ * $Revision$ * $Date$ * * iBATIS.NET Data Mapper * Copyright (C) 2004 - Gilles Bayon * * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ********************************************************************************/ #endregion #region Imports using System; using System.Collections; #endregion namespace IBatisNet.DataMapper.Configuration.Cache.Memory { /// /// Summary description for MemoryCacheControler. /// public class MemoryCacheControler : ICacheController { #region Fields private MemoryCacheLevel _cacheLevel = MemoryCacheLevel.Weak; private Hashtable _cache = null; #endregion #region Constructor (s) / Destructor /// /// Constructor /// public MemoryCacheControler() { _cache = Hashtable.Synchronized( new Hashtable() ); } #endregion #region ICacheController Members /// /// Remove an object from a cache model /// /// the key to the object /// the removed object(?) public object Remove(object key) { object value = null; object reference = this[key]; _cache.Remove(key); if (reference != null) { if (reference is StrongReference) { value = ((StrongReference) reference).Target; } else if (reference is WeakReference) { value = ((WeakReference) reference).Target; } } return value; } /// /// Adds an item with the specified key and value into cached data. /// Gets a cached object with the specified key. /// /// The cached object or null public object this[object key] { get { object value = null; object reference = _cache[key]; if (reference != null) { if (reference is StrongReference) { value = ((StrongReference) reference).Target; } else if (reference is WeakReference) { value = ((WeakReference) reference).Target; } } return value; } set { object reference = null; if (_cacheLevel.Equals(MemoryCacheLevel.Weak)) { reference = new WeakReference(value); } else if (_cacheLevel.Equals(MemoryCacheLevel.Strong)) { reference = new StrongReference(value); } _cache[key] = reference; } } /// /// Clears all elements from the cache. /// public void Flush() { lock(this) { _cache.Clear(); } } /// /// Configures the cache /// public void Configure(IDictionary properties) { string referenceType = (string)properties["Type"];; if (referenceType != null) { _cacheLevel = MemoryCacheLevel.GetByRefenceType(referenceType.ToUpper()); } } #endregion /// /// Class to implement a strong (permanent) reference. /// private class StrongReference { private object _target = null; public StrongReference(object obj) { _target = obj; } /// /// Gets the object (the target) referenced by this instance. /// public object Target { get { return _target ; } } } } }