public void ClearCacheObjectTypes(string typeName) { var type = TypeFinder.GetTypeByName(typeName); if (type == null) { return; } var isInterface = type.IsInterface; foreach (var kvp in _items .Where(x => { // entry.Value is Lazy<object> and not null, its value may be null // remove null values as well, does not hurt // get non-created as NonCreatedValue & exceptions as null var value = DictionaryCacheProviderBase.GetSafeLazyValue(x.Value, true); // if T is an interface remove anything that implements that interface // otherwise remove exact types (not inherited types) return(value == null || (isInterface ? (type.IsInstanceOfType(value)) : (value.GetType() == type))); })) { _items.TryRemove(kvp.Key, out _); } }
public void ClearCacheObjectTypes <T>(Func <string, T, bool> predicate) { var typeOfT = typeof(T); var isInterface = typeOfT.IsInterface; foreach (var kvp in _items .Where(x => { // entry.Value is Lazy<object> and not null, its value may be null // remove null values as well, does not hurt // compare on exact type, don't use "is" // get non-created as NonCreatedValue & exceptions as null var value = DictionaryCacheProviderBase.GetSafeLazyValue(x.Value, true); if (value == null) { return(true); } // if T is an interface remove anything that implements that interface // otherwise remove exact types (not inherited types) return((isInterface ? (value is T) : (value.GetType() == typeOfT)) // run predicate on the 'public key' part only, ie without prefix && predicate(x.Key, (T)value)); })) { _items.TryRemove(kvp.Key, out _); } }
public object GetCacheItem(string cacheKey, Func <object> getCacheItem, TimeSpan?timeout, bool isSliding = false, CacheItemPriority priority = CacheItemPriority.Normal, CacheItemRemovedCallback removedCallback = null, string[] dependentFiles = null) { // see notes in HttpRuntimeCacheProvider Lazy <object> result; using (var lck = new UpgradeableReadLock(_locker)) { result = MemoryCache.Get(cacheKey) as Lazy <object>; if (result == null || DictionaryCacheProviderBase.GetSafeLazyValue(result, true) == null) // get non-created as NonCreatedValue & exceptions as null { result = DictionaryCacheProviderBase.GetSafeLazy(getCacheItem); var policy = GetPolicy(timeout, isSliding, removedCallback, dependentFiles); lck.UpgradeToWriteLock(); //NOTE: This does an add or update MemoryCache.Set(cacheKey, result, policy); } } //return result.Value; var value = result.Value; // will not throw (safe lazy) var eh = value as DictionaryCacheProviderBase.ExceptionHolder; if (eh != null) { throw eh.Exception; // throw once! } return(value); }
public IEnumerable <object> GetCacheItemsByKeyExpression(string regexString) { return(_items .Where(kvp => Regex.IsMatch(kvp.Key, regexString)) .Select(kvp => DictionaryCacheProviderBase.GetSafeLazyValue(kvp.Value)) .Where(x => x != null)); }
public IEnumerable <object> GetCacheItemsByKeySearch(string keyStartsWith) { return(_items .Where(kvp => kvp.Key.InvariantStartsWith(keyStartsWith)) .Select(kvp => DictionaryCacheProviderBase.GetSafeLazyValue(kvp.Value)) .Where(x => x != null)); }
public virtual void ClearCacheObjectTypes <T>() { using (new WriteLock(_locker)) { var typeOfT = typeof(T); var isInterface = typeOfT.IsInterface; foreach (var key in MemoryCache .Where(x => { // x.Value is Lazy<object> and not null, its value may be null // remove null values as well, does not hurt // get non-created as NonCreatedValue & exceptions as null var value = DictionaryCacheProviderBase.GetSafeLazyValue((Lazy <object>)x.Value, true); // if T is an interface remove anything that implements that interface // otherwise remove exact types (not inherited types) return(value == null || (isInterface ? (value is T) : (value.GetType() == typeOfT))); }) .Select(x => x.Key) .ToArray()) // ToArray required to remove { MemoryCache.Remove(key); } } }
public object GetCacheItem(string cacheKey) { Lazy <object> result; using (new ReadLock(_locker)) { result = MemoryCache.Get(cacheKey) as Lazy <object>; // null if key not found } return(result == null ? null : DictionaryCacheProviderBase.GetSafeLazyValue(result)); // return exceptions as null }
public void InsertCacheItem(string cacheKey, Func <object> getCacheItem, TimeSpan?timeout = null, bool isSliding = false, CacheItemPriority priority = CacheItemPriority.Normal, CacheItemRemovedCallback removedCallback = null, string[] dependentFiles = null) { InnerProvider.InsertCacheItem(cacheKey, () => { var result = DictionaryCacheProviderBase.GetSafeLazy(getCacheItem); var value = result.Value; // force evaluation now - this may throw if cacheItem throws, and then nothing goes into cache if (value == null) { return(null); // do not store null values (backward compat) } return(CheckCloneableAndTracksChanges(value)); }, timeout, isSliding, priority, removedCallback, dependentFiles); }
public IEnumerable <object> GetCacheItemsByKeyExpression(string regexString) { KeyValuePair <string, object>[] entries; using (new ReadLock(_locker)) { entries = MemoryCache .Where(x => Regex.IsMatch(x.Key, regexString)) .ToArray(); // evaluate while locked } return(entries .Select(x => DictionaryCacheProviderBase.GetSafeLazyValue((Lazy <object>)x.Value)) // return exceptions as null .Where(x => x != null) // backward compat, don't store null values in the cache .ToList()); }
public IEnumerable <object> GetCacheItemsByKeySearch(string keyStartsWith) { KeyValuePair <string, object>[] entries; using (new ReadLock(_locker)) { entries = MemoryCache .Where(x => x.Key.InvariantStartsWith(keyStartsWith)) .ToArray(); // evaluate while locked } return(entries .Select(x => DictionaryCacheProviderBase.GetSafeLazyValue((Lazy <object>)x.Value)) // return exceptions as null .Where(x => x != null) // backward compat, don't store null values in the cache .ToList()); }
public object GetCacheItem(string cacheKey, Func <object> getCacheItem) { var cached = InnerProvider.GetCacheItem(cacheKey, () => { var result = DictionaryCacheProviderBase.GetSafeLazy(getCacheItem); var value = result.Value; // force evaluation now - this may throw if cacheItem throws, and then nothing goes into cache if (value == null) { return(null); // do not store null values (backward compat) } return(CheckCloneableAndTracksChanges(value)); }); return(CheckCloneableAndTracksChanges(cached)); }
public void InsertCacheItem(string cacheKey, Func <object> getCacheItem, TimeSpan?timeout = null, bool isSliding = false, CacheItemPriority priority = CacheItemPriority.Normal, CacheItemRemovedCallback removedCallback = null, string[] dependentFiles = null) { // NOTE - here also we must insert a Lazy<object> but we can evaluate it right now // and make sure we don't store a null value. var result = DictionaryCacheProviderBase.GetSafeLazy(getCacheItem); var value = result.Value; // force evaluation now if (value == null) { return; // do not store null values (backward compat) } var policy = GetPolicy(timeout, isSliding, removedCallback, dependentFiles); //NOTE: This does an add or update MemoryCache.Set(cacheKey, result, policy); }
public object GetCacheItem(string cacheKey) { Lazy <object> result; try { _locker.EnterReadLock(); result = MemoryCache.Get(cacheKey) as Lazy <object>; // null if key not found } finally { if (_locker.IsReadLockHeld) { _locker.ExitReadLock(); } } return(result == null ? null : DictionaryCacheProviderBase.GetSafeLazyValue(result)); // return exceptions as null }
public object GetCacheItem(string cacheKey, Func <object> getCacheItem) { var result = _items.GetOrAdd(cacheKey, k => DictionaryCacheProviderBase.GetSafeLazy(getCacheItem)); var value = result.Value; // will not throw (safe lazy) if (!(value is DictionaryCacheProviderBase.ExceptionHolder eh)) { return(value); } // and... it's in the cache anyway - so contrary to other cache providers, // which would trick with GetSafeLazyValue, we need to remove by ourselves, // in order NOT to cache exceptions _items.TryRemove(cacheKey, out result); eh.Exception.Throw(); // throw once! return(null); // never reached }
public virtual void ClearCacheObjectTypes(string typeName) { var type = TypeFinder.GetTypeByName(typeName); if (type == null) { return; } var isInterface = type.IsInterface; try { _locker.EnterWriteLock(); foreach (var key in MemoryCache .Where(x => { // x.Value is Lazy<object> and not null, its value may be null // remove null values as well, does not hurt // get non-created as NonCreatedValue & exceptions as null var value = DictionaryCacheProviderBase.GetSafeLazyValue((Lazy <object>)x.Value, true); // if T is an interface remove anything that implements that interface // otherwise remove exact types (not inherited types) return(value == null || (isInterface ? (type.IsInstanceOfType(value)) : (value.GetType() == type))); }) .Select(x => x.Key) .ToArray()) // ToArray required to remove { MemoryCache.Remove(key); } } finally { if (_locker.IsWriteLockHeld) { _locker.ExitWriteLock(); } } }
public virtual void ClearCacheObjectTypes <T>(Func <string, T, bool> predicate) { try { _locker.EnterWriteLock(); var typeOfT = typeof(T); var isInterface = typeOfT.IsInterface; foreach (var key in MemoryCache .Where(x => { // x.Value is Lazy<object> and not null, its value may be null // remove null values as well, does not hurt // get non-created as NonCreatedValue & exceptions as null var value = DictionaryCacheProviderBase.GetSafeLazyValue((Lazy <object>)x.Value, true); if (value == null) { return(true); } // if T is an interface remove anything that implements that interface // otherwise remove exact types (not inherited types) return((isInterface ? (value is T) : (value.GetType() == typeOfT)) && predicate(x.Key, (T)value)); }) .Select(x => x.Key) .ToArray()) // ToArray required to remove { MemoryCache.Remove(key); } } finally { if (_locker.IsWriteLockHeld) { _locker.ExitWriteLock(); } } }
public object GetCacheItem(string cacheKey) { _items.TryGetValue(cacheKey, out var result); // else null return(result == null ? null : DictionaryCacheProviderBase.GetSafeLazyValue(result)); // return exceptions as null }