using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Reflection; using System.Diagnostics; using System.Threading; using System.Web.Caching; using System.Web; using System.Collections; namespace Sleis.Utility { public static class CacheUtility { public static string[] Clear() { string[] result = null; lock (Cache) { List keys = new List(); IDictionaryEnumerator enumerator = Cache.GetEnumerator(); // copy all keys that currently exist in Cache while (enumerator.MoveNext()) { keys.Add(enumerator.Key.ToString()); } for (int i = 0; i < keys.Count; i++) { Cache.Remove(keys[i]); } result = keys.ToArray(); } return result; } public static void Add(string key, object value, TimeSpan cacheDuration) { Add(key, value, cacheDuration, true); } public static void Add(string key, object value, string fileOrDirectoryDependency) { Cache.Add(key, value, new CacheDependency(fileOrDirectoryDependency), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Default, null); } public static void Add(string key, object value, TimeSpan cacheDuration, bool isSlidingExpiration) { if (isSlidingExpiration) { Cache.Add(key, value, null, Cache.NoAbsoluteExpiration, cacheDuration, CacheItemPriority.Default, null); } else { Cache.Add(key, value, null, DateTime.Now + cacheDuration, Cache.NoSlidingExpiration, CacheItemPriority.Default, null); } } public static T Get(string key) where T : class { return Cache[key] as T; } public static T Remove(string key) where T : class { return Cache.Remove(key) as T; } public static void Remove(string key) { Cache.Remove(key); } private static Cache Cache { get { return HttpRuntime.Cache; } } } public class ObjCache where U : class { // Constructors public ObjCache() { } public ObjCache(int maxNumerOfCachedObjects) { m_MaxNumberOfCachedObjects = maxNumerOfCachedObjects; } public void Clear() { lock (m_LockObject) { m_Cache.Clear(); m_InsertOrderList.Clear(); } } // Methods public U this[T key] { get { U target; lock (m_LockObject) { if (m_Cache.TryGetValue(key, out target)) { return target; } } return null; } set { lock (m_LockObject) { if (value == null) { U target; if (m_Cache.TryGetValue(key, out target)) { m_Cache.Remove(key); m_InsertOrderList.Remove(target); } } else { m_InsertOrderList.Remove(value); m_InsertOrderList.Add(value); // Put at end of list if (m_InsertOrderList.Count > m_MaxNumberOfCachedObjects) { for (int i = 0; i < m_InsertOrderList.Count - m_MaxNumberOfCachedObjects; ++i) { U target = m_InsertOrderList[i]; T removeKey = default(T); bool foundKey = false; foreach (KeyValuePair pair in m_Cache) { if (Object.ReferenceEquals(value, pair.Value)) { removeKey = pair.Key; foundKey = true; break; } } if (foundKey) { m_Cache.Remove(removeKey); } } m_InsertOrderList.RemoveRange(0, m_InsertOrderList.Count - m_MaxNumberOfCachedObjects); } m_Cache[key] = value; } } } } protected Dictionary m_Cache = new Dictionary(); protected List m_InsertOrderList = new List(); protected object m_LockObject = new object(); protected int m_MaxNumberOfCachedObjects = int.MaxValue; } public class StringKeyObjCache : ObjCache where U : class { // Constructors public StringKeyObjCache() { } public StringKeyObjCache(int maxNumerOfCachedObjects) : base(maxNumerOfCachedObjects) { } public void ClearIfKeyStartsWith(string prefix) { lock (m_LockObject) { List removeKeys = new List(); foreach (KeyValuePair pair in m_Cache) { if (pair.Key.StartsWith(prefix)) { removeKeys.Add(pair.Key); } } foreach (string removeKey in removeKeys) { m_Cache.Remove(removeKey); } } } } public class PrefixedKeyObjectCache where T : class { string _keyPrefix; public PrefixedKeyObjectCache(string keyPrefix) { _keyPrefix = keyPrefix; } public void Add(string key, object value, TimeSpan cacheDuration) { CacheUtility.Add(_keyPrefix + key, value, cacheDuration); } public void Add(string key, object value, string fileOrDirectoryDependency) { CacheUtility.Add(_keyPrefix + key, value, fileOrDirectoryDependency); } public void Add(string key, object value, TimeSpan cacheDuration, bool isSlidingExpiration) { CacheUtility.Add(_keyPrefix + key, value, cacheDuration, isSlidingExpiration); } public T Get(string key) { return CacheUtility.Get(_keyPrefix + key); } public T Remove(string key) { return CacheUtility.Remove(_keyPrefix + key); } public T this[string key] { get { return this.Get(key); } } } }