예제 #1
0
        public static void Add(String key, object value, TimeSpan?slidingExpiration, DateTime?absoluteExpiration)
        {
            if (String.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentException("Invalid cache key");
            }
            if (value == null)
            {
                throw new ArgumentNullException("Value is not null");
            }

            if (MemoryCache.Default[key] == null)
            {
                lock (_locker)
                {
                    if (MemoryCache.Default[key] == null)
                    {
                        CacheEntryChangeMonitor monitor = MemoryCache.Default.CreateCacheEntryChangeMonitor(new[] { key });


                        var _policy = new CacheItemPolicy
                        {
                            AbsoluteExpiration =
                                absoluteExpiration.HasValue ? absoluteExpiration.Value : DateTime.MaxValue,
                            SlidingExpiration = slidingExpiration.HasValue ? slidingExpiration.Value : TimeSpan.FromSeconds(60),
                            Priority          = System.Runtime.Caching.CacheItemPriority.Default
                        };
                        _policy.ChangeMonitors.Add(monitor);

                        MemoryCache.Default.Add(key, value, _policy);
                    }
                }
            }
        }
예제 #2
0
        public static bool AddBothExpirations(this ObjectCache instance, string key, object value, DateTimeOffset absoluteExpiration, TimeSpan slidingExpiration, CacheEntryRemovedCallback removedDelegate = null)
        {
            if (instance == null)
            {
                throw new ArgumentNullException("instance");
            }

            string slidingKey = _slidingKeyPrefix + key;

            CacheItemPolicy slidingPolicy = new CacheItemPolicy
            {
                SlidingExpiration = slidingExpiration
            };

            instance.Set(slidingKey, _placeholder, slidingPolicy);

            CacheEntryChangeMonitor monitor = instance.CreateCacheEntryChangeMonitor(new string[] {
                slidingKey
            });

            CacheItemPolicy itemPolicy = new CacheItemPolicy
            {
                AbsoluteExpiration = absoluteExpiration,
                RemovedCallback    = (x) => {
                    if (removedDelegate == null)
                    {
                        return;
                    }

                    //HACK: ChangeMonitorChanged is given due to one of the depedent
                    //monitors for an item being removed due to expiry, capture this and re-send as expired.
                    if (x.RemovedReason == CacheEntryRemovedReason.ChangeMonitorChanged)
                    {
                        x = new CacheEntryRemovedArguments(x.Source, CacheEntryRemovedReason.Expired, x.CacheItem);
                    }

                    removedDelegate(x);
                }
            };

            itemPolicy.ChangeMonitors.Add(monitor);

            return(instance.Add(key, value, itemPolicy));
        }
예제 #3
0
        //Private Methods...
        private CacheItemPolicy GetCachePolicy(string key, Nullable <TimeSpan> time = null)
        {
            key = GetAbsoluteKey(key);
            string[] absKey = new string[] { key };

            MemoryCache.Default.Add(key,
                                    new object(),
                                    time.HasValue ? DateTime.UtcNow.AddTicks(time.Value.Ticks) : DateTime.UtcNow.AddHours(1) //default it to only 1 hour.
                                    );

            CacheEntryChangeMonitor monitor = MemoryCache.Default.CreateCacheEntryChangeMonitor(absKey);

            CacheItemPolicy policy = new CacheItemPolicy();

            policy.ChangeMonitors.Add(monitor);
            policy.SlidingExpiration = !time.HasValue ? new TimeSpan(1, 0, 0) : time.Value; //default it to only 1 hour

            return(policy);
        }
예제 #4
0
        void InitForMemoryCache(bool isPublic, string[] filenamesArg, string[] cachekeysArg, CacheDependency dependency, DateTime utcStart)
        {
            bool dispose = true;

            try {
                MemCache memCache = HttpRuntime.InternalCache as MemCache;
                _bits            = new SafeBitVector32(0);
                _utcLastModified = DateTime.MinValue;
                IList <String> files = filenamesArg;
                IList <String> keys  = cachekeysArg;
                if (dependency != null)
                {
                    ReadOnlyCollection <string> filePaths = (dependency._fileChangeMonitor != null) ? dependency._fileChangeMonitor.FilePaths : null;
                    ReadOnlyCollection <string> cacheKeys = (dependency._entryChangeMonitor != null) ? dependency._entryChangeMonitor.CacheKeys : null;
                    if (filePaths != null || filenamesArg != null)
                    {
                        if (filePaths == null)
                        {
                            files = filenamesArg;
                        }
                        else if (filenamesArg == null)
                        {
                            files = filePaths;
                        }
                        else
                        {
                            files = new List <String>(filenamesArg.Length + filePaths.Count);
                            foreach (string f in filenamesArg)
                            {
                                files.Add(f);
                            }
                            foreach (string f in filePaths)
                            {
                                files.Add(f);
                            }
                        }
                    }
                    if (cacheKeys != null || cachekeysArg != null)
                    {
                        if (cacheKeys == null)
                        {
                            keys = cachekeysArg;
                        }
                        else if (cachekeysArg == null)
                        {
                            keys = cacheKeys;
                        }
                        else
                        {
                            keys = new List <String>(cachekeysArg.Length + cacheKeys.Count);
                            foreach (string f in cachekeysArg)
                            {
                                keys.Add(f);
                            }
                            foreach (string f in cacheKeys)
                            {
                                keys.Add(f);
                            }
                        }
                    }
                }

                _fileChangeMonitor  = (files != null) ? new HostFileChangeMonitor(files) : null;
                _entryChangeMonitor = (keys != null) ? memCache.CreateCacheEntryChangeMonitor(keys, isPublic) : null;

                string uniqueId = null;

                if (_fileChangeMonitor != null)
                {
                    _utcLastModified = _fileChangeMonitor.LastModified.UtcDateTime;
                    uniqueId         = _fileChangeMonitor.UniqueId;
                    _fileChangeMonitor.NotifyOnChanged(new OnChangedCallback(OnChangedCallback));
                }
                if (_entryChangeMonitor != null)
                {
                    DateTime utcLastModified = _entryChangeMonitor.LastModified.UtcDateTime;
                    if (utcLastModified > _utcLastModified)
                    {
                        _utcLastModified = utcLastModified;
                    }
                    uniqueId += _entryChangeMonitor.UniqueId;
                    _entryChangeMonitor.NotifyOnChanged(new OnChangedCallback(OnChangedCallback));
                }

                _uniqueID = uniqueId;
#if DBG
                _isUniqueIDInitialized = true;
#endif
                // check if file has changed since the start time
                if (utcStart < DateTime.MaxValue)
                {
                    if (_utcLastModified > utcStart &&
                        !(_utcLastModified - DateTime.UtcNow > FUTURE_FILETIME_BUFFER))        // See VSWhidbey 400917
                    {
                        _bits[CHANGED] = true;
                    }
                }

                _bits[BASE_INIT] = true;
                if (dependency != null && dependency._bits[CHANGED])
                {
                    _bits[CHANGED] = true;
                }
                if (_bits[WANTS_DISPOSE] || _bits[CHANGED])
                {
                    Debug.Trace("CacheDependencyInit", "WANTS_DISPOSE or CHANGED.  InitForMemoryCache calling DisposeInternal");
                    DisposeInternal();
                }
                dispose = false;
            }
            finally {
                if (dispose)
                {
                    _bits[BASE_INIT] = true;
                    Debug.Trace("CacheDependencyInit", "\n\nERROR in CacheDependency.InitForMemoryCache, calling DisposeInternal");
                    DisposeInternal();
                }
            }
        }
 public ChangeMonitorDetail(CacheEntryChangeMonitor changeMonitor)
 {
     RegionName   = changeMonitor.RegionName;
     LastModified = changeMonitor.LastModified;
     CacheKeys    = changeMonitor.CacheKeys;
 }