Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        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;
                }
            });
        }