public void Add(string key, LazyLock item, ICacheDetails cacheDetails) { var policy = new CacheItemPolicy(); // Set timeout policy.Priority = CacheItemPriority.NotRemovable; if (IsTimespanSet(cacheDetails.AbsoluteCacheExpiration)) { policy.AbsoluteExpiration = DateTimeOffset.Now.Add(cacheDetails.AbsoluteCacheExpiration); } else if (IsTimespanSet(cacheDetails.SlidingCacheExpiration)) { policy.SlidingExpiration = cacheDetails.SlidingCacheExpiration; } // Add dependencies var dependencies = (IList <ChangeMonitor>)cacheDetails.CacheDependency.Dependency; if (dependencies != null) { foreach (var dependency in dependencies) { policy.ChangeMonitors.Add(dependency); } } // Setting priority to not removable ensures an // app pool recycle doesn't unload the item, but a timeout will. policy.Priority = CacheItemPriority.NotRemovable; // Setup callback policy.RemovedCallback = CacheItemRemoved; cache.Add(key, item, policy); }
public T GetOrAdd(string key, Func <T> loadFunction, Func <ICacheDetails> getCacheDetailsFunction) { LazyLock lazy; bool success; synclock.EnterReadLock(); try { success = this.cacheProvider.TryGetValue(key, out lazy); } finally { synclock.ExitReadLock(); } if (!success) { synclock.EnterWriteLock(); try { if (!this.cacheProvider.TryGetValue(key, out lazy)) { lazy = new LazyLock(); var cacheDetails = getCacheDetailsFunction(); this.cacheProvider.Add(key, lazy, cacheDetails); } } finally { synclock.ExitWriteLock(); } } return(lazy.Get(loadFunction)); }
public bool TryGetValue(string key, out LazyLock value) { value = this.Get(key); if (value != null) { return(true); } return(false); }
public void Add(string key, LazyLock item, ICacheDetails cacheDetails) { DateTime absolute = System.Web.Caching.Cache.NoAbsoluteExpiration; TimeSpan sliding = System.Web.Caching.Cache.NoSlidingExpiration; if (IsTimespanSet(cacheDetails.AbsoluteCacheExpiration)) { absolute = DateTime.UtcNow.Add(cacheDetails.AbsoluteCacheExpiration); } else if (IsTimespanSet(cacheDetails.SlidingCacheExpiration)) { sliding = cacheDetails.SlidingCacheExpiration; } var dependency = (CacheDependency)cacheDetails.CacheDependency.Dependency; Context.Cache.Insert(key, item, dependency, absolute, sliding, CacheItemPriority.NotRemovable, this.OnItemRemoved); }