private static void cacheDependency(string key, HttpContext context, ICacheEntity item) { string dependencyKey = item.GetCacheItemKey(); CacheDependencyList dependencyList = context.Cache[dependencyKey] as CacheDependencyList; if (dependencyList == null) { insert(context, dependencyKey, dependencyList = new CacheDependencyList()); } if (!dependencyList.Contains(key)) { dependencyList.Add(key); } string typeDependencyKey = item.GetType().Name; CacheDependencyList typeDependencyList = context.Cache[typeDependencyKey] as CacheDependencyList; if (typeDependencyList == null) { insert(context, typeDependencyKey, typeDependencyList = new CacheDependencyList()); } if (!typeDependencyList.Contains(dependencyKey)) { typeDependencyList.Add(dependencyKey); } }
private static void invalidateDependency(HttpContext context, string dependencyKey) { CacheDependencyList dependencyList = context.Cache[dependencyKey] as CacheDependencyList; if (dependencyList == null) { return; } foreach (string dependency in dependencyList) { CacheDependencyList dependencyItems = context.Cache[dependency] as CacheDependencyList; if (dependencyItems != null) { foreach (string dependencyItem in dependencyItems) { invalidateByKey(context, dependencyItem); } } invalidateByKey(context, dependency); } invalidateByKey(context, dependencyKey); }
public void Invalidate <T>() where T : ICacheEntity { HttpContext context = HttpContext.Current; CacheDependencyList dependencyList = context.Cache[typeof(T).Name] as CacheDependencyList; if (dependencyList != null) { foreach (string dependency in dependencyList) { invalidateDependency(context, dependency); } } }
public T GetItems <T, K>(string key, CachePartition partition, Func <T> getUncachedItems, Func <K, IEnumerable <ICacheEntity> > getDependencies) where T : IEnumerable <K> { HttpContext context = HttpContext.Current; string fullKey = partition != null ? key + partition.ToString() : key; T cacheItem = (T)context.Cache[fullKey]; if (cacheItem != null) { return(cacheItem); } T items = getUncachedItems(); if (items != null) { insert(context, fullKey, items); // partitions if (partition != null) { CacheDependencyList partitionList = context.Cache[key] as CacheDependencyList; if (partitionList == null) { insert(context, key, partitionList = new CacheDependencyList()); } string keyAndPartition = key + partition.ToString(); if (!partitionList.Contains(keyAndPartition)) { partitionList.Add(keyAndPartition); } } // dependencies if (getDependencies != null) { if (items.Count() > 0) { foreach (K item in items) { // add dependencies for each item foreach (ICacheEntity dependency in getDependencies(item)) { cacheDependency(key, context, dependency); } } } else { // allow the caller to add dependencies for dependent objects even though no items were found foreach (ICacheEntity dependency in getDependencies(default(K))) { cacheDependency(key, context, dependency); } } } } return(items); }