Example #1
0
        private CacheItemPolicy Policy(ICacheKey cacheKey)
        {
            EnsureParentKeyPolicies(cacheKey.ParentCacheKey);

            CacheItemPolicy policy = new CacheItemPolicy();

            if (cacheKey.Expiration != TimeSpan.Zero)
            {
                policy.AbsoluteExpiration = DateTime.Now.Add(cacheKey.Expiration);
            }
            policy.ChangeMonitors.Add(_cache.CreateCacheEntryChangeMonitor(new string[] { cacheKey.ParentCacheKey?.Key ?? cacheKey.Key }));

            return(policy);
        }
Example #2
0
        protected void SetRegionToken(string region)
        {
            var regionKey = GetRegionKey(region);

            CacheItemPolicy tokenPolicy = new CacheItemPolicy()
            {
                Priority           = CacheItemPriority.Default,
                ChangeMonitors     = { _cache.CreateCacheEntryChangeMonitor(new[] { _instanceKey }) },
                AbsoluteExpiration = ObjectCache.InfiniteAbsoluteExpiration,
                SlidingExpiration  = ObjectCache.NoSlidingExpiration
            };

            _cache.Set(regionKey, region, tokenPolicy);
        }
        private CacheItemPolicy GetCacheItemPolicy(TimeSpan?duration, IEnumerable <string> dependencies)
        {
            var absoluteExpiration = ObjectCache.InfiniteAbsoluteExpiration;

            if (duration.HasValue)
            {
                absoluteExpiration = DateTime.UtcNow + duration.Value;
            }

            var cacheItemPolicy = new CacheItemPolicy
            {
                AbsoluteExpiration = absoluteExpiration,
                SlidingExpiration  = ObjectCache.NoSlidingExpiration
            };

            if (dependencies != null && dependencies.Any())
            {
                // INFO: we can only depend on existing items, otherwise this entry will be removed immediately.
                dependencies = dependencies.Where(x => x != null && _cache.Contains(x));
                if (dependencies.Any())
                {
                    cacheItemPolicy.ChangeMonitors.Add(_cache.CreateCacheEntryChangeMonitor(dependencies));
                }
            }

            //cacheItemPolicy.RemovedCallback = OnRemoveEntry;

            return(cacheItemPolicy);
        }
Example #4
0
        private void CreateRegionToken(string region)
        {
            var key = GetRegionTokenKey(region);

            // add region token with dependency on our instance token, so that all regions get
            // removed whenever the instance gets cleared.
            var policy = new CacheItemPolicy()
            {
                Priority           = CacheItemPriority.NotRemovable,
                AbsoluteExpiration = System.Runtime.Caching.ObjectCache.InfiniteAbsoluteExpiration,
                SlidingExpiration  = System.Runtime.Caching.ObjectCache.NoSlidingExpiration,
                ChangeMonitors     = { _cache.CreateCacheEntryChangeMonitor(new[] { _instanceKey }) },
            };

            _cache.Add(key, region, policy);
        }
    private void Initialise()
    {
        var             monitor = _cache.CreateCacheEntryChangeMonitor(_cacheKeys);
        CacheItemPolicy pol     = new CacheItemPolicy {
            AbsoluteExpiration = DateTime.MaxValue, Priority = CacheItemPriority.NotRemovable
        };

        pol.ChangeMonitors.Add(monitor);
        pol.RemovedCallback = Callback;
        _cache.Add(_uniqueId, _uniqueId, pol);
        FinishInit();
    }
 public void AddToCache(string youtubeUrl, MusicFile musicFile, TimeSpan cachePersistTime)
 {
     cache.Add(new CacheItem(youtubeUrl, musicFile), new CacheItemPolicy()
     {
         AbsoluteExpiration = DateTime.Now + cachePersistTime
     });
     cache.CreateCacheEntryChangeMonitor(new string[] { youtubeUrl }).NotifyOnChanged(state => { if (!cache.Contains(youtubeUrl))
                                                                                                 {
                                                                                                     ScheduleDelete(musicFile.FilePath);
                                                                                                 }
                                                                                      });
 }
Example #7
0
        private void ConfigureDependency(string[] tags, CacheItemPolicy policy)
        {
            if (tags != null && tags.Length > 0)
            {
                var now           = DateTime.Now;
                var tagExpiration = now.AddDays(10);

                foreach (var item in tags)
                {
                    AddTag(_cache, now, tagExpiration, item);
                }

                policy.ChangeMonitors.Add(
                    _cache.CreateCacheEntryChangeMonitor(tags)
                    );
            }
        }
Example #8
0
        public void Put(CachedItem cachedItem)
        {
            var key           = cachedItem.EventInformation.CacheKey;
            var unwrappedKeys = _cacheKeyUnwrapper.UnwrapKey(key);

            createDependencies(unwrappedKeys);

            var policy = new CacheItemPolicy
            {
                AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(_timeoutMinutes),
                RemovedCallback    = arguments => _eventListenersCallback.OnDelete(cachedItem)
            };

            policy.ChangeMonitors.Add(cache.CreateCacheEntryChangeMonitor(unwrappedKeys));
            cache.Set(key, cachedItem, policy);
            _eventListenersCallback.OnPut(cachedItem);
        }
Example #9
0
        public void Add(string key, object o, DateTimeOffset expiration, string dependsOnKey = null)
        {
            var cachePolicy = new CacheItemPolicy
            {
                AbsoluteExpiration = expiration
            };

            if (!string.IsNullOrWhiteSpace(dependsOnKey))
            {
                cachePolicy.ChangeMonitors.Add(
                    Cache.CreateCacheEntryChangeMonitor(new[] { dependsOnKey })
                    );
            }
            lock (Cache)
            {
                Cache.Add(key, o, cachePolicy);
            }
        }
Example #10
0
        private CachedItem executeAndPutInCache(EventInformation eventInformation, IEnumerable <string> dependingRemoveKeys, Func <object> originalMethod)
        {
            var methodResult = originalMethod();
            var cachedItem   = new CachedItem(eventInformation, methodResult);
            var key          = cachedItem.EventInformation.CacheKey;
            var dependedKeys = dependingRemoveKeys.ToList();

            dependedKeys.Add(mainCacheKey);
            createDependencies(dependedKeys);

            var policy = new CacheItemPolicy
            {
                AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(_timeoutMinutes),
                RemovedCallback    = arguments => _eventListenersCallback.OnCacheRemoval(cachedItem)
            };

            policy.ChangeMonitors.Add(cache.CreateCacheEntryChangeMonitor(dependedKeys));
            cache.Set(key, cachedItem, policy);
            return(cachedItem);
        }
Example #11
0
        private CacheItemPolicy GetCacheItemPolicy(Action <string, TenantInstance> removedCallback, CacheItem cacheDependency)
        {
            var policy = new CacheItemPolicy();

            if (lifetimeOptions.UseSlidingExpiration)
            {
                policy.SlidingExpiration = lifetimeOptions.Lifetime;
            }
            else
            {
                policy.AbsoluteExpiration = DateTimeOffset.UtcNow.Add(lifetimeOptions.Lifetime);
            }

            policy.RemovedCallback = args => removedCallback(args.CacheItem.Key, args.CacheItem.Value as TenantInstance);

            // make the cache item dependent on the provided cache dependency
            var changeMonitor = cache.CreateCacheEntryChangeMonitor(new[] { cacheDependency.Key });

            policy.ChangeMonitors.Add(changeMonitor);

            return(policy);
        }
        public virtual void Set(CacheKey cacheKey, object value)
        {
            CacheItemPolicy policy = new CacheItemPolicy
            {
                SlidingExpiration = TimeSpan.FromDays(365.0)
            };

            if (!cacheKey.IsArea)
            {
                var areaCacheItem = _cache.Get(cacheKey.Area);
                if (areaCacheItem == null)
                {
                    _cache.Set(cacheKey.Area, new Object(), new CacheItemPolicy()
                    {
                        SlidingExpiration = TimeSpan.FromDays(365.0)
                    });
                }

                policy.ChangeMonitors.Add(_cache.CreateCacheEntryChangeMonitor(new[] { cacheKey.Area }));
            }

            ReleaseInvalidation(cacheKey);
            _cache.Set(cacheKey.FullCacheKey, value, policy, null);
        }
Example #13
0
        public void Add(object data, string key, string[] tags, TimeSpan expiration)
        {
            var policy = new CacheItemPolicy
            {
                AbsoluteExpiration = DateTime.Now + expiration
            };

            if (tags != null && tags.Length > 0)
            {
                var now           = DateTime.Now;
                var tagExpiration = now.AddDays(10);

                foreach (var item in tags)
                {
                    AddTag(_cache, now, tagExpiration, item);
                }

                policy.ChangeMonitors.Add(
                    _cache.CreateCacheEntryChangeMonitor(tags)
                    );
            }

            _cache.Set(key, data, policy);
        }
Example #14
0
 internal CacheEntryChangeMonitor CreateCacheEntryChangeMonitor(IEnumerable <String> keys, bool isPublic)
 {
     return((isPublic) ? _cachePublic.CreateCacheEntryChangeMonitor(keys, null) : _cacheInternal.CreateCacheEntryChangeMonitor(keys, null));
 }