public void Add(ICacheKey cacheKey, object value, ICacheDependency dependency, DateTime expirationDate, TimeSpan slidingSpan, RuntimeCommon.Caching.CacheItemPriority priority) { var policy = new CacheItemPolicy(); { //configurations var depKeys = TransformDependency(dependency)?.ToList(); if (!depKeys.IsNullOrEmpty()) { foreach (var key in depKeys) { subscription.SubscribeTopic(key); } var monitor = new PubSubMonitor(cacheKey.GetKeyAsString(), depKeys, filter); policy.ChangeMonitors.Add(monitor); } policy.SlidingExpiration = slidingSpan == CacheUtils.NoSliding ? ObjectCache.NoSlidingExpiration : slidingSpan; policy.AbsoluteExpiration = expirationDate == CacheUtils.NoExpiration ? ObjectCache.InfiniteAbsoluteExpiration : expirationDate; policy.Priority = priority == RuntimeCommon.Caching.CacheItemPriority.NotRemovable ? System.Runtime.Caching.CacheItemPriority.NotRemovable : System.Runtime.Caching.CacheItemPriority.Default; } memoryCacheInstance.Set(cacheKey.GetKeyAsString(), value, policy); }
private CacheEntryUpdateCallback EncapsulateListenerCallback(ICacheKey originalKey, List <string> depKeys, CacheItemInvalidationCallback invalidationCallback) { return((args) => { if (args.RemovedReason == CacheEntryRemovedReason.ChangeMonitorChanged || args.RemovedReason == CacheEntryRemovedReason.Expired) { invalidationCallback?.Invoke(); args.UpdatedCacheItem = new CacheItem(originalKey.GetKeyAsString(), 0); var newPolicy = new CacheItemPolicy(); { //configurations if (!depKeys.IsNullOrEmpty()) { newPolicy.ChangeMonitors.Add(new PubSubMonitor(originalKey.GetKeyAsString(), depKeys, filter)); } newPolicy.SlidingExpiration = ObjectCache.NoSlidingExpiration; newPolicy.AbsoluteExpiration = DateTime.UtcNow.AddDays(1); newPolicy.Priority = System.Runtime.Caching.CacheItemPriority.NotRemovable; newPolicy.UpdateCallback = EncapsulateListenerCallback(originalKey, depKeys, invalidationCallback); } args.UpdatedCacheItemPolicy = newPolicy; } }); }
public void Listen(ICacheKey cacheKey, ICacheDependency dependency, CacheItemInvalidationCallback invalidationCallback) { var policy = new CacheItemPolicy(); { //configurations var depKeys = TransformDependency(dependency)?.ToList(); if (!depKeys.IsNullOrEmpty()) { foreach (var key in depKeys) { subscription.SubscribeTopic(key); } var monitor = new PubSubMonitor(cacheKey.GetKeyAsString(), depKeys, filter); policy.ChangeMonitors.Add(monitor); } policy.SlidingExpiration = ObjectCache.NoSlidingExpiration; policy.AbsoluteExpiration = DateTime.UtcNow.AddDays(1); policy.Priority = System.Runtime.Caching.CacheItemPriority.NotRemovable; policy.UpdateCallback = EncapsulateListenerCallback(cacheKey, depKeys, invalidationCallback); } memoryCacheInstance.Set(cacheKey.GetKeyAsString(), 0, policy); }
public object Get(ICacheKey key) { return(memoryCacheInstance.Get(key.GetKeyAsString())); }
public object StopListen(ICacheKey key) { return(memoryCacheInstance.Remove(key.GetKeyAsString())); }