using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Umbraco.Core.Cache { /// /// Represents a cache provider that statically caches item in a concurrent dictionary. /// public class StaticCacheProvider : ICacheProvider { internal readonly ConcurrentDictionary StaticCache = new ConcurrentDictionary(); public virtual void ClearAllCache() { StaticCache.Clear(); } public virtual void ClearCacheItem(string key) { object val; StaticCache.TryRemove(key, out val); } public virtual void ClearCacheObjectTypes(string typeName) { StaticCache.RemoveAll(kvp => kvp.Value != null && kvp.Value.GetType().ToString().InvariantEquals(typeName)); } public virtual void ClearCacheObjectTypes() { var typeOfT = typeof(T); StaticCache.RemoveAll(kvp => kvp.Value != null && kvp.Value.GetType() == typeOfT); } public virtual void ClearCacheObjectTypes(Func predicate) { var typeOfT = typeof(T); StaticCache.RemoveAll(kvp => kvp.Value != null && kvp.Value.GetType() == typeOfT && predicate(kvp.Key, (T)kvp.Value)); } public virtual void ClearCacheByKeySearch(string keyStartsWith) { StaticCache.RemoveAll(kvp => kvp.Key.InvariantStartsWith(keyStartsWith)); } public virtual void ClearCacheByKeyExpression(string regexString) { StaticCache.RemoveAll(kvp => Regex.IsMatch(kvp.Key, regexString)); } public virtual IEnumerable GetCacheItemsByKeySearch(string keyStartsWith) { return (from KeyValuePair c in StaticCache where c.Key.InvariantStartsWith(keyStartsWith) select c.Value).ToList(); } public IEnumerable GetCacheItemsByKeyExpression(string regexString) { return (from KeyValuePair c in StaticCache where Regex.IsMatch(c.Key, regexString) select c.Value).ToList(); } public virtual object GetCacheItem(string cacheKey) { var result = StaticCache[cacheKey]; return result; } public virtual object GetCacheItem(string cacheKey, Func getCacheItem) { return StaticCache.GetOrAdd(cacheKey, key => getCacheItem()); } } }