using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Web.Caching; namespace Umbraco.Core.Cache { /// /// A cache provider that statically caches everything in an in memory dictionary /// internal class StaticCacheProvider : CacheProviderBase { private readonly ConcurrentDictionary _staticCache = new ConcurrentDictionary(); public override void ClearAllCache() { _staticCache.Clear(); } public override void ClearCacheItem(string key) { object val; _staticCache.TryRemove(key, out val); } public override void ClearCacheObjectTypes(string typeName) { foreach (var key in _staticCache.Keys) { if (_staticCache[key] != null && _staticCache[key].GetType().ToString().InvariantEquals(typeName)) { object val; _staticCache.TryRemove(key, out val); } } } public override void ClearCacheObjectTypes() { foreach (var key in _staticCache.Keys) { if (_staticCache[key] != null && _staticCache[key].GetType() == typeof(T)) { object val; _staticCache.TryRemove(key, out val); } } } public override void ClearCacheByKeySearch(string keyStartsWith) { foreach (var key in _staticCache.Keys) { if (key.InvariantStartsWith(keyStartsWith)) { ClearCacheItem(key); } } } public override void ClearCacheByKeyExpression(string regexString) { foreach (var key in _staticCache.Keys) { if (Regex.IsMatch(key, regexString)) { ClearCacheItem(key); } } } public override IEnumerable GetCacheItemsByKeySearch(string keyStartsWith) { return (from KeyValuePair c in _staticCache where c.Key.InvariantStartsWith(keyStartsWith) select c.Value.TryConvertTo() into attempt where attempt.Success select attempt.Result).ToList(); } public override T GetCacheItem(string cacheKey) { var result = _staticCache[cacheKey]; if (result == null) { return default(T); } return result.TryConvertTo().Result; } public override T GetCacheItem(string cacheKey, Func getCacheItem) { return (T)_staticCache.GetOrAdd(cacheKey, getCacheItem); } } }