public override CacheModificationResult AddOrChange <T>(CacheKey key, CacheValueOf <T> cacheObject) { using (new WriteLockDisposable(ThreadLocker)) { var exists = _memoryCache.GetCacheItem(key) != null; var cacheItemPolicy = cacheObject.Policy.ToCacheItemPolicy(); var entryDate = DateTime.Now; var policyExpiry = cacheItemPolicy.AbsoluteExpiration.Subtract(entryDate); cacheItemPolicy.RemovedCallback += arguments => LogHelper.TraceIfEnabled( GetType(), "Item was removed from cache ({0}). Policy had {1}s to run when entered at {2}. Key: {3}", () => arguments.RemovedReason.ToString(), () => policyExpiry.TotalSeconds.ToString(), () => entryDate.ToString(), () => key); _keyTracker.AddOrUpdate(key, key, (existingKey, existingValue) => key); if (exists) { LogHelper.TraceIfEnabled(GetType(), "Updating item with {0} left to run", () => policyExpiry.TotalSeconds.ToString()); _memoryCache.Set(key, cacheObject, cacheItemPolicy); return(new CacheModificationResult(true, false)); } var diff = cacheObject.Policy.GetExpiryDate().Subtract(DateTimeOffset.Now); LogHelper.TraceIfEnabled(GetType(), "Adding item with {0} left to run", () => policyExpiry.TotalSeconds.ToString()); _memoryCache.Add(key, cacheObject, cacheItemPolicy); } return(new CacheModificationResult(false, true)); }
public override CacheModificationResult AddOrChange <T>(CacheKey key, CacheValueOf <T> cacheObject) { using (new WriteLockDisposable(ThreadLocker)) { var exists = Get <T>(key) != null; if (exists) { _context.Items[key] = cacheObject; return(new CacheModificationResult(true, false)); } _context.Items.Add(key, cacheObject); } return(new CacheModificationResult(false, true)); }
public override CacheModificationResult AddOrChange <T>(CacheKey key, CacheValueOf <T> cacheObject) { CacheModificationResult toReturn = null; _cacheStore.AddOrUpdate( key, keyForAdding => { toReturn = new CacheModificationResult(false, true); return(cacheObject); }, (keyForUpdating, existing) => { toReturn = new CacheModificationResult(true, false); return(cacheObject); }); return(toReturn); }
public abstract CacheModificationResult AddOrChange <T>(CacheKey key, CacheValueOf <T> cacheObject);
public virtual CacheModificationResult AddOrChange(CacheKey key, CacheValueOf <object> cacheObject) { return(AddOrChange <object>(key, cacheObject)); }
public virtual CacheModificationResult AddOrChangeValue(CacheKey key, object cacheObject) { var value = new CacheValueOf <object>(cacheObject); return(AddOrChange(key, value)); }