private static CacheItemPolicy BuildCachePolicy( DateTime?absoluteExpiration = null, TimeSpan?slidingExpiration = null, CacheEntryRemovedCallback removed = null, CacheEntryUpdateCallback updated = null, ICacheDependency dependency = null) { var policy = new CacheItemPolicy { Priority = CacheItemPriority.Default, AbsoluteExpiration = absoluteExpiration.HasValue ? absoluteExpiration.Value : ObjectCache.InfiniteAbsoluteExpiration, SlidingExpiration = slidingExpiration.HasValue ? slidingExpiration.Value : ObjectCache.NoSlidingExpiration, RemovedCallback = removed, UpdateCallback = updated }; if (dependency != null) { var fileDependency = dependency as IFileCacheDependency; if (fileDependency != null) { policy.ChangeMonitors.Add(new HostFileChangeMonitor(fileDependency.FilePaths)); } } return(policy); }
private void ContentCacheUpdateCallback(CacheEntryUpdateArguments arguments) { if (arguments.RemovedReason == CacheEntryRemovedReason.Expired) { var expiredCacheItem = MemoryCache.Default.GetCacheItem(arguments.Key); if (expiredCacheItem != null) { String url = expiredCacheItem.Key; Logger.Trace(String.Format("Return From ContentCacheUpdateCallback {0}", url)); var ret = GetJsonFromCacheOrWebservice(url); expiredCacheItem.Value = ret; arguments.UpdatedCacheItem = expiredCacheItem; var policy = new CacheItemPolicy(); policy.Priority = CacheItemPriority.Default; _callbackU = new CacheEntryUpdateCallback(ContentCacheUpdateCallback); policy.UpdateCallback = _callbackU; policy.AbsoluteExpiration = DateTime.Now.AddMinutes(_cacheMinute); arguments.UpdatedCacheItemPolicy = policy; } else { arguments.UpdatedCacheItem = null; } } }
public String GetJsonFromCacheOrWebservice(string url) { String key = url; String ret = ""; Logger.Trace("key=" + url + " IsCacheEnable=" + IsCacheEnable); if (IsCacheEnable) { ret = (String)MemoryCache.Default.Get(key); if (String.IsNullOrEmpty(ret)) { ret = MakeJsonRequest(url); CacheItemPolicy policy = null; CacheEntryRemovedCallback callback = null; policy = new CacheItemPolicy(); policy.Priority = CacheItemPriority.Default; _callbackU = new CacheEntryUpdateCallback(ContentCacheUpdateCallback); policy.UpdateCallback = _callbackU; policy.AbsoluteExpiration = DateTime.Now.AddMinutes(_cacheMinute); MemoryCache.Default.Set(key, ret, policy); } else { Logger.Trace("Get data from Cache=" + url); } } else { ret = MakeJsonRequest(url); Logger.Trace("Get data from API=" + url); } return(ret); }
private CacheItemPolicy GetCachePolicy <T>(Func <T> getter = null) { CacheEntryUpdateCallback callback = (CacheEntryUpdateArguments args) => { if (args.RemovedReason == CacheEntryRemovedReason.Expired || args.RemovedReason == CacheEntryRemovedReason.Removed) { CacheItemPolicy cachePolicy = GetCachePolicy <T>(getter); var items = getter(); if (items != null) { args.UpdatedCacheItem = new CacheItem(args.Key, items); args.UpdatedCacheItemPolicy = cachePolicy; } } }; var cip = new CacheItemPolicy() { AbsoluteExpiration = DateTime.Now.AddHours(24), //AbsoluteExpiration = DateTime.Now.AddHours(2), UpdateCallback = new CacheEntryUpdateCallback(callback) }; return(cip); }
private static T AddOrGetExist <T>(string key, T value, double expiredSec, CacheEntryUpdateCallback action = null) { return((T)_cache.AddOrGetExisting(key, value, new CacheItemPolicy() { AbsoluteExpiration = DateTimeOffset.Now.AddSeconds(expiredSec), UpdateCallback = action })); }
public static T AddOrUpdate <T>(string key, T value, double expiredSec, CacheEntryUpdateCallback action = null) { if (_cache.Contains(key)) { _cache[key] = value; } else { AddOrGetExist(key, value, expiredSec, action); } return((T)_cache[key]); }
public void SetPolicy(CacheItemPolicy policy) { if (policy == null) { return; } absoluteExpiration = policy.AbsoluteExpiration; monitors = policy.ChangeMonitors; priority = policy.Priority; removedCallback = policy.RemovedCallback; slidingExpiration = policy.SlidingExpiration; updateCallback = policy.UpdateCallback; }
void ValidatePolicy(CacheItemPolicy policy, bool allowUpdateCallback) { CacheEntryUpdateCallback updateCallback = policy.UpdateCallback; if (!allowUpdateCallback && updateCallback != null) { throw new ArgumentException("CacheItemPolicy.UpdateCallback must be null.", "policy"); } if (updateCallback != null && policy.RemovedCallback != null) { throw new ArgumentException("Only one callback can be specified. Either RemovedCallback or UpdateCallback must be null.", "policy"); } DateTimeOffset absoluteExpiration = policy.AbsoluteExpiration; TimeSpan slidingExpiration = policy.SlidingExpiration; if (absoluteExpiration != ObjectCache.InfiniteAbsoluteExpiration && slidingExpiration != TimeSpan.Zero) { throw new ArgumentException( "policy", "'AbsoluteExpiration' must be ObjectCache.InfiniteAbsoluteExpiration or 'SlidingExpiration' must be TimeSpan.Zero" ); } long ticks = slidingExpiration.Ticks; if (ticks < 0 || ticks > 315360000000000) { throw new ArgumentOutOfRangeException( "policy", "SlidingExpiration must be greater than or equal to '00:00:00' and less than or equal to '365.00:00:00'." ); } CacheItemPriority priority = policy.Priority; if (priority < CacheItemPriority.Default || priority > CacheItemPriority.NotRemovable) { throw new ArgumentOutOfRangeException( "policy", "'Priority' must be greater than or equal to 'Default' and less than or equal to 'NotRemovable'" ); } }
public MemoryCacheEntry(MemoryCache owner, string key, object value, DateTimeOffset absoluteExpiration, Collection <ChangeMonitor> monitors, CacheItemPriority priority, CacheEntryRemovedCallback removedCallback, TimeSpan slidingExpiration, CacheEntryUpdateCallback updateCallback) { if (value == null) { throw new ArgumentNullException("value"); } if (owner == null) { throw new ArgumentNullException("owner"); } this.owner = owner; this.Key = key; this.Value = value; this.absoluteExpiration = absoluteExpiration; this.monitors = monitors; this.priority = priority; this.removedCallback = removedCallback; this.slidingExpiration = slidingExpiration; this.updateCallback = updateCallback; this.LastModified = DateTime.UtcNow; if (absoluteExpiration != ObjectCache.InfiniteAbsoluteExpiration) { expiresAt = absoluteExpiration.UtcTicks; } else if (slidingExpiration != ObjectCache.NoSlidingExpiration) { expiresAt = GetSlidingExpiry(); } else { expiresAt = 0; } expiresAtNext = expiresAt; }
// DevDiv Bugs 162763: // Add a an event that fires *before* an item is evicted from the ASP.NET Cache internal void Set(string key, object value, Collection<ChangeMonitor> changeMonitors, DateTimeOffset absoluteExpiration, TimeSpan slidingExpiration, CacheEntryUpdateCallback onUpdateCallback) { if (key == null) { throw new ArgumentNullException("key"); } if (changeMonitors == null && absoluteExpiration == ObjectCache.InfiniteAbsoluteExpiration && slidingExpiration == ObjectCache.NoSlidingExpiration) { throw new ArgumentException(R.Invalid_argument_combination); } if (onUpdateCallback == null) { throw new ArgumentNullException("onUpdateCallback"); } if (IsDisposed) { if (changeMonitors != null) { foreach (ChangeMonitor monitor in changeMonitors) { if (monitor != null) { monitor.Dispose(); } } } return; } // Insert updatable cache entry MemoryCacheKey cacheKey = new MemoryCacheKey(key); MemoryCacheStore store = GetStore(cacheKey); MemoryCacheEntry cacheEntry = new MemoryCacheEntry(key, value, ObjectCache.InfiniteAbsoluteExpiration, ObjectCache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null, null, this); store.Set(cacheKey, cacheEntry); // Ensure the sentinel depends on its updatable entry string[] cacheKeys = { key }; ChangeMonitor expensiveObjectDep = CreateCacheEntryChangeMonitor(cacheKeys); if (changeMonitors == null) { changeMonitors = new Collection<ChangeMonitor>(); } changeMonitors.Add(expensiveObjectDep); // Insert sentinel entry for the updatable cache entry MemoryCacheKey sentinelCacheKey = new MemoryCacheKey("OnUpdateSentinel" + key); MemoryCacheStore sentinelStore = GetStore(sentinelCacheKey); MemoryCacheEntry sentinelCacheEntry = new MemoryCacheEntry(sentinelCacheKey.Key, new SentinelEntry(key, expensiveObjectDep, onUpdateCallback), absoluteExpiration, slidingExpiration, CacheItemPriority.NotRemovable, changeMonitors, s_sentinelRemovedCallback, this); sentinelStore.Set(sentinelCacheKey, sentinelCacheEntry); cacheEntry.ConfigureUpdateSentinel(sentinelStore, sentinelCacheEntry); }
internal void Set(string key, object value, Collection<ChangeMonitor> changeMonitors, DateTimeOffset absoluteExpiration, TimeSpan slidingExpiration, CacheEntryUpdateCallback onUpdateCallback) { if (key == null) { throw new ArgumentNullException("key"); } if (((changeMonitors == null) && (absoluteExpiration == ObjectCache.InfiniteAbsoluteExpiration)) && (slidingExpiration == ObjectCache.NoSlidingExpiration)) { throw new ArgumentException(R.Invalid_argument_combination); } if (onUpdateCallback == null) { throw new ArgumentNullException("onUpdateCallback"); } if (this.IsDisposed) { if (changeMonitors != null) { foreach (ChangeMonitor monitor in changeMonitors) { if (monitor != null) { monitor.Dispose(); } } } } else { MemoryCacheKey key2 = new MemoryCacheKey(key); this.GetStore(key2).Set(key2, new MemoryCacheEntry(key, value, ObjectCache.InfiniteAbsoluteExpiration, ObjectCache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null, null, this)); string[] keys = new string[] { key }; ChangeMonitor item = this.CreateCacheEntryChangeMonitor(keys, null); if (changeMonitors == null) { changeMonitors = new Collection<ChangeMonitor>(); } changeMonitors.Add(item); key2 = new MemoryCacheKey("OnUpdateSentinel" + key); this.GetStore(key2).Set(key2, new MemoryCacheEntry(key2.Key, new SentinelEntry(key, item, onUpdateCallback), absoluteExpiration, slidingExpiration, CacheItemPriority.NotRemovable, changeMonitors, s_sentinelRemovedCallback, this)); } }
public static bool Set(string key, object value, TimeSpan slidingExpiration, CacheEntryUpdateCallback cacheEntryUpdateCallback) { CacheItemPolicy cacheItemPolicy = CreateCacheItemPolicy(slidingExpiration, cacheEntryUpdateCallback); return(Set(key, value, cacheItemPolicy)); }
internal SentinelEntry(string key, ChangeMonitor expensiveObjectDependency, CacheEntryUpdateCallback callback) { _key = key; _expensiveObjectDependency = expensiveObjectDependency; _updateCallback = callback; }
private static CacheItemPolicy CreateCacheItemPolicy(DateTimeOffset absoluteExpiration, CacheEntryUpdateCallback cacheEntryUpdateCallback) { if (absoluteExpiration == null) { return(CreateCacheItemPolicy()); } CacheItemPolicy cacheItemPolicy = new CacheItemPolicy(); cacheItemPolicy.Priority = CacheItemPriority.Default; cacheItemPolicy.AbsoluteExpiration = absoluteExpiration; if (cacheEntryUpdateCallback != null) { cacheItemPolicy.UpdateCallback = cacheEntryUpdateCallback; } return(cacheItemPolicy); }
public static bool Set(string key, object value, DateTimeOffset absoluteExpiration, CacheEntryUpdateCallback cacheEntryUpdateCallback) { CacheItemPolicy cacheItemPolicy = CreateCacheItemPolicy(absoluteExpiration, cacheEntryUpdateCallback); return(Set(key, value, cacheItemPolicy)); }
public LibChangeMonitorBase() { updatecallback = new CacheEntryUpdateCallback(CacheRemoveCallBack); }
private static CacheItemPolicy CreateCacheItemPolicy(TimeSpan slidingExpiration, CacheEntryUpdateCallback cacheEntryUpdateCallback) { if (slidingExpiration == TimeSpan.MinValue) { return(CreateCacheItemPolicy()); } CacheItemPolicy cacheItemPolicy = new CacheItemPolicy(); cacheItemPolicy.Priority = CacheItemPriority.Default; cacheItemPolicy.SlidingExpiration = slidingExpiration; if (cacheEntryUpdateCallback != null) { cacheItemPolicy.UpdateCallback = cacheEntryUpdateCallback; } return(cacheItemPolicy); }
public static void SetCachedObject(string cacheId, object obj, TimeSpan timeSpan, CacheEntryUpdateCallback refreshAction = null) { var policy = new CacheItemPolicy(); policy.AbsoluteExpiration = DateTimeOffset.Now.Add(timeSpan); if (refreshAction != null) { policy.UpdateCallback = refreshAction; } if (obj != null) { _cm.Add(cacheId, obj, policy); } }
internal SentinelEntry(string key, ChangeMonitor expensiveObjectDependency, CacheEntryUpdateCallback callback) { _key = key; _expensiveObjectDependency = expensiveObjectDependency; _updateCallback = callback; }
public MemoryCacheEntry (MemoryCache owner, string key, object value, DateTimeOffset absoluteExpiration, Collection <ChangeMonitor> monitors, CacheItemPriority priority, CacheEntryRemovedCallback removedCallback, TimeSpan slidingExpiration, CacheEntryUpdateCallback updateCallback) { if (value == null) throw new ArgumentNullException ("value"); if (owner == null) throw new ArgumentNullException ("owner"); this.owner = owner; this.Key = key; this.Value = value; this.absoluteExpiration = absoluteExpiration; this.monitors = monitors; this.priority = priority; this.removedCallback = removedCallback; this.slidingExpiration = slidingExpiration; this.updateCallback = updateCallback; this.LastModified = DateTime.UtcNow; if (absoluteExpiration != ObjectCache.InfiniteAbsoluteExpiration) expiresAt = absoluteExpiration.Ticks; else if (slidingExpiration != ObjectCache.NoSlidingExpiration) expiresAt = DateTime.Now.Ticks + slidingExpiration.Ticks; else expiresAt = 0; }
public static T GetOrAdd <T>(string key, Func <T> valudFunc, double expiredSec, bool forceUpdate, CacheEntryUpdateCallback action = null) { if (!_cache.Contains(key)) { AddOrGetExist(key, valudFunc, expiredSec, action); } else { if (forceUpdate) { _cache.Remove(key); AddOrGetExist(key, valudFunc, expiredSec, action); } } return((T)_cache.Get(key)); }
static CacheItemPolicy GenerateCacheItemPolicy(DateTimeOffset absoluteExpiration, TimeSpan slidingExpiration, CacheEntryUpdateCallback onUpdateCallback = null) { var policy = new CacheItemPolicy(); policy.AbsoluteExpiration = absoluteExpiration; policy.SlidingExpiration = slidingExpiration; policy.UpdateCallback = onUpdateCallback; return policy; }
public void SetPolicy (CacheItemPolicy policy) { if (policy == null) return; absoluteExpiration = policy.AbsoluteExpiration; monitors = policy.ChangeMonitors; priority = policy.Priority; removedCallback = policy.RemovedCallback; slidingExpiration = policy.SlidingExpiration; updateCallback = policy.UpdateCallback; }
internal void Set(string key, object value, Collection <ChangeMonitor> changeMonitors, DateTimeOffset absoluteExpiration, TimeSpan slidingExpiration, CacheEntryUpdateCallback onUpdateCallback) { if (key == null) { throw new ArgumentNullException("key"); } if (((changeMonitors == null) && (absoluteExpiration == ObjectCache.InfiniteAbsoluteExpiration)) && (slidingExpiration == ObjectCache.NoSlidingExpiration)) { throw new ArgumentException(R.Invalid_argument_combination); } if (onUpdateCallback == null) { throw new ArgumentNullException("onUpdateCallback"); } if (this.IsDisposed) { if (changeMonitors != null) { foreach (ChangeMonitor monitor in changeMonitors) { if (monitor != null) { monitor.Dispose(); } } } } else { MemoryCacheKey key2 = new MemoryCacheKey(key); this.GetStore(key2).Set(key2, new MemoryCacheEntry(key, value, ObjectCache.InfiniteAbsoluteExpiration, ObjectCache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null, null, this)); string[] keys = new string[] { key }; ChangeMonitor item = this.CreateCacheEntryChangeMonitor(keys, null); if (changeMonitors == null) { changeMonitors = new Collection <ChangeMonitor>(); } changeMonitors.Add(item); key2 = new MemoryCacheKey("OnUpdateSentinel" + key); this.GetStore(key2).Set(key2, new MemoryCacheEntry(key2.Key, new SentinelEntry(key, item, onUpdateCallback), absoluteExpiration, slidingExpiration, CacheItemPriority.NotRemovable, changeMonitors, s_sentinelRemovedCallback, this)); } }
internal void Set(string key, object value, Collection <ChangeMonitor> changeMonitors, DateTimeOffset absoluteExpiration, TimeSpan slidingExpiration, CacheEntryUpdateCallback onUpdateCallback) { if (key == null) { throw new ArgumentNullException(nameof(key)); } if (changeMonitors == null && absoluteExpiration == ObjectCache.InfiniteAbsoluteExpiration && slidingExpiration == ObjectCache.NoSlidingExpiration) { throw new ArgumentException(SR.Invalid_argument_combination); } if (onUpdateCallback == null) { throw new ArgumentNullException(nameof(onUpdateCallback)); } if (IsDisposed) { if (changeMonitors != null) { foreach (ChangeMonitor monitor in changeMonitors) { if (monitor != null) { monitor.Dispose(); } } } return; } // Insert updatable cache entry MemoryCacheKey cacheKey = new MemoryCacheKey(key); MemoryCacheStore store = GetStore(cacheKey); MemoryCacheEntry cacheEntry = new MemoryCacheEntry(key, value, ObjectCache.InfiniteAbsoluteExpiration, ObjectCache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null, null, this); store.Set(cacheKey, cacheEntry); // Ensure the sentinel depends on its updatable entry string[] cacheKeys = { key }; ChangeMonitor expensiveObjectDep = CreateCacheEntryChangeMonitor(cacheKeys); if (changeMonitors == null) { changeMonitors = new Collection <ChangeMonitor>(); } changeMonitors.Add(expensiveObjectDep); // Insert sentinel entry for the updatable cache entry MemoryCacheKey sentinelCacheKey = new MemoryCacheKey("OnUpdateSentinel" + key); MemoryCacheStore sentinelStore = GetStore(sentinelCacheKey); MemoryCacheEntry sentinelCacheEntry = new MemoryCacheEntry(sentinelCacheKey.Key, new SentinelEntry(key, expensiveObjectDep, onUpdateCallback), absoluteExpiration, slidingExpiration, CacheItemPriority.NotRemovable, changeMonitors, s_sentinelRemovedCallback, this); sentinelStore.Set(sentinelCacheKey, sentinelCacheEntry); cacheEntry.ConfigureUpdateSentinel(sentinelStore, sentinelCacheEntry); }
public void SetWithCallback <T>(string key, T o, DateTime duration, CacheEntryRemovedCallback removedCallback, CacheEntryUpdateCallback updateCallback) where T : class { if (string.IsNullOrWhiteSpace(key)) { return; } if (o == null) { return; } var policy = new CacheItemPolicy(); policy.RemovedCallback = removedCallback; policy.UpdateCallback = updateCallback; policy.AbsoluteExpiration = duration; _cache.Set(key, o, policy); }