using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Threading; using System.Web; using System.Web.Caching; using Umbraco.Core.Logging; using CacheItemPriority = System.Web.Caching.CacheItemPriority; namespace Umbraco.Core.Cache { /// /// A CacheProvider that wraps the logic of the HttpRuntime.Cache /// internal class HttpRuntimeCacheProvider : DictionaryCacheProviderBase, IRuntimeCacheProvider { private readonly System.Web.Caching.Cache _cache; private readonly DictionaryCacheWrapper _wrapper; public HttpRuntimeCacheProvider(System.Web.Caching.Cache cache) { _cache = cache; _wrapper = new DictionaryCacheWrapper(_cache, s => _cache.Get(s.ToString()), o => _cache.Remove(o.ToString())); } protected override DictionaryCacheWrapper DictionaryCache { get { return _wrapper; } } private IEnumerable> EnumerateDictionaryCache() { // DictionaryCache just wraps _cache which has a special enumerator var enumerator = _cache.GetEnumerator(); while (enumerator.MoveNext()) { var key = enumerator.Key as string; if (key == null) continue; yield return new KeyValuePair(key, enumerator.Value); } } public override void ClearAllCache() { using (new WriteLock(Locker)) { foreach (var kvp in EnumerateDictionaryCache() .Where(x => x.Key.StartsWith(CacheItemPrefix) && x.Value != null)) _cache.Remove(kvp.Key); } } public override void ClearCacheItem(string key) { using (new WriteLock(Locker)) { var cacheKey = GetCacheKey(key); _cache.Remove(cacheKey); } } public override void ClearCacheObjectTypes(string typeName) { var typeName2 = typeName; using (new WriteLock(Locker)) { foreach (var kvp in EnumerateDictionaryCache() .Where(x => x.Key.StartsWith(CacheItemPrefix) && x.Value != null && x.Value.GetType().ToString().InvariantEquals(typeName2))) _cache.Remove(kvp.Key); } } public override void ClearCacheObjectTypes() { // should we use "is" or compare types? //var typeOfT = typeof(T); using (new WriteLock(Locker)) { foreach (var kvp in EnumerateDictionaryCache() .Where(x => x.Key.StartsWith(CacheItemPrefix) //&& x.Value != null //&& x.Value.GetType() == typeOfT)) && x.Value is T)) _cache.Remove(kvp.Key); } } public override void ClearCacheObjectTypes(Func predicate) { // see note above // should we use "is" or compare types? try { using (new WriteLock(Locker)) { foreach (var kvp in EnumerateDictionaryCache() .Where(x => x.Key.StartsWith(CacheItemPrefix) && x.Value is T && predicate(x.Key, (T) x.Value))) _cache.Remove(kvp.Key); } } catch (Exception e) { // oops, what is this?! LogHelper.Error("Cache clearing error", e); } } public override void ClearCacheByKeySearch(string keyStartsWith) { using (new WriteLock(Locker)) { foreach (var kvp in EnumerateDictionaryCache() .Where(x => x.Key.InvariantStartsWith(string.Format("{0}-{1}", CacheItemPrefix, keyStartsWith)))) _cache.Remove(kvp.Key); } } public override void ClearCacheByKeyExpression(string regexString) { var plen = CacheItemPrefix.Length + 1; // string.Format("{0}-", CacheItemPrefix) using (new WriteLock(Locker)) { foreach (var kvp in EnumerateDictionaryCache() .Where(x => x.Key.StartsWith(CacheItemPrefix) && Regex.IsMatch(x.Key.Substring(plen), regexString))) _cache.Remove(kvp.Key); } } public override IEnumerable GetCacheItemsByKeySearch(string keyStartsWith) { return EnumerateDictionaryCache() .Where(x => x.Key.InvariantStartsWith(string.Format("{0}-{1}", CacheItemPrefix, keyStartsWith))) .Select(x => ((Lazy)x.Value).Value) .ToList(); } public override IEnumerable GetCacheItemsByKeyExpression(string regexString) { var plen = CacheItemPrefix.Length + 1; // string.Format("{0}-", CacheItemPrefix) return EnumerateDictionaryCache() .Where(x => x.Key.StartsWith(CacheItemPrefix) && Regex.IsMatch(x.Key.Substring(plen), regexString)) .Select(x => ((Lazy)x.Value).Value) .ToList(); } /// /// Gets (and adds if necessary) an item from the cache with all of the default parameters /// /// /// /// public override object GetCacheItem(string cacheKey, Func getCacheItem) { return GetCacheItem(cacheKey, getCacheItem, null, dependentFiles: null); } /// /// This overload is here for legacy purposes /// /// /// /// /// /// /// /// /// internal object GetCacheItem(string cacheKey, Func getCacheItem, TimeSpan? timeout, bool isSliding = false, CacheItemPriority priority = CacheItemPriority.Normal, CacheItemRemovedCallback removedCallback = null, CacheDependency dependency = null) { cacheKey = GetCacheKey(cacheKey); // NOTE - because we don't know what getCacheItem does, how long it will take and whether it will hang, // getCacheItem should run OUTSIDE of the global application lock else we run into lock contention and // nasty performance issues. // So.... we insert a Lazy in the cache while holding the global application lock, and then rely // on the Lazy lock to ensure that getCacheItem runs once and everybody waits on it, while the global // application lock has been released. // Note that this means we'll end up storing null values in the cache, whereas in the past we made sure // not to store them. There's code below, commented out, to make sure we do re-compute the value if it // was previously computed as null - effectively reproducing the past behavior - but I'm not quite sure // it is a good idea. Lazy result; using (var lck = new UpgradeableReadLock(Locker)) { result = DictionaryCache.Get(cacheKey) as Lazy; if (result == null /* || (result.IsValueCreated && result.Value == null) */) { lck.UpgradeToWriteLock(); result = new Lazy(getCacheItem); var absolute = isSliding ? System.Web.Caching.Cache.NoAbsoluteExpiration : (timeout == null ? System.Web.Caching.Cache.NoAbsoluteExpiration : DateTime.Now.Add(timeout.Value)); var sliding = isSliding == false ? System.Web.Caching.Cache.NoSlidingExpiration : (timeout ?? System.Web.Caching.Cache.NoSlidingExpiration); _cache.Insert(cacheKey, result, dependency, absolute, sliding, priority, removedCallback); } } return result.Value; } public object GetCacheItem(string cacheKey, Func getCacheItem, TimeSpan? timeout, bool isSliding = false, CacheItemPriority priority = CacheItemPriority.Normal, CacheItemRemovedCallback removedCallback = null, string[] dependentFiles = null) { CacheDependency dependency = null; if (dependentFiles != null && dependentFiles.Any()) { dependency = new CacheDependency(dependentFiles); } return GetCacheItem(cacheKey, getCacheItem, timeout, isSliding, priority, removedCallback, dependency); } /// /// This overload is here for legacy purposes /// /// /// /// /// /// /// /// internal void InsertCacheItem(string cacheKey, Func getCacheItem, TimeSpan? timeout = null, bool isSliding = false, CacheItemPriority priority = CacheItemPriority.Normal, CacheItemRemovedCallback removedCallback = null, CacheDependency dependency = null) { // NOTE - here also we must insert a Lazy but we can evaluate it right now // and make sure we don't store a null value. Though I'm not sure it is a good idea. var result = new Lazy(getCacheItem); var value = result.Value; // force evaluation now //if (value == null) return; cacheKey = GetCacheKey(cacheKey); var absolute = isSliding ? System.Web.Caching.Cache.NoAbsoluteExpiration : (timeout == null ? System.Web.Caching.Cache.NoAbsoluteExpiration : DateTime.Now.Add(timeout.Value)); var sliding = isSliding == false ? System.Web.Caching.Cache.NoSlidingExpiration : (timeout ?? System.Web.Caching.Cache.NoSlidingExpiration); _cache.Insert(cacheKey, result, dependency, absolute, sliding, priority, removedCallback); } public void InsertCacheItem(string cacheKey, Func getCacheItem, TimeSpan? timeout = null, bool isSliding = false, CacheItemPriority priority = CacheItemPriority.Normal, CacheItemRemovedCallback removedCallback = null, string[] dependentFiles = null) { CacheDependency dependency = null; if (dependentFiles != null && dependentFiles.Any()) { dependency = new CacheDependency(dependentFiles); } InsertCacheItem(cacheKey, getCacheItem, timeout, isSliding, priority, removedCallback, dependency); } } }