Ejemplo n.º 1
0
        /// <summary>
        /// Remove an entry with a given key. Notifies subscriber, if there is any,
        /// of the removed item.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="regionName"></param>
        /// <returns></returns>
        public override object Remove(string key, string regionName = null)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            var        cacheKey   = new CacheKey(key);
            CacheEntry cacheValue = null;

            // remove the item, return if not found.
            lock (_locker)
            {
                var mruEntry = MruManager.GetItem(cacheKey);
                if (mruEntry == null)
                {
                    return(null);
                }
                // get the Store's Current Key containing the TimeStamp!
                cacheKey   = (CacheKey)mruEntry.Key;
                cacheValue = mruEntry.Value as CacheEntry;
                MruManager.Remove(cacheKey);
            }
            // Notify the subscriber of the removed item.
            if (CacheEntrySetRemovedCallback != null)
            {
                CacheEntrySetRemovedCallback(new[]
                {
                    new CacheEntryRemovedArguments(this, CacheEntryRemovedReason.Removed, cacheValue.Convert(cacheKey))
                });
            }
            return(cacheValue);
        }
Ejemplo n.º 2
0
        public override void Set(string key, object value,
                                 CacheItemPolicy policy, string regionName = null)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (policy == null)
            {
                policy = DefaultPolicy;
            }

            var now   = GetCurrentDate(0);
            var entry = new CacheEntry(policy, now)
            {
                Value = value
            };
            var cacheKey = new CacheKey(key)
            {
                TimeStamp = now
            };

            lock (_locker)
            {
                var e = MruManager.GetItem(cacheKey);
                if (e == null)
                {
                    MruManager.Add(cacheKey, entry);
                    return;
                }
                entry       = e.Value as CacheEntry;
                entry.Value = value;
                // if priotiry is not removable, just update the store w/ value
                if (entry.NonExpiring)
                {
                    return;
                }

                if (!entry.IsExpired(now))
                {
                    // slide the expire time...
                    if (entry.SlidingExpiration > 0)
                    {
                        entry.ExpirationTime = GetCurrentDate(entry.SlidingExpiration);
                    }
                    return;
                }
                // adjust policy (set to slide every 5 mins) of the expired item to accomodate this update...
                if (entry.SlidingExpiration == 0)
                {
                    entry.SlidingExpiration = TimeSpan.TicksPerMinute * 5;
                }
                entry.ExpirationTime = GetCurrentDate(entry.SlidingExpiration);
            }
        }
Ejemplo n.º 3
0
        public override CacheItem GetCacheItem(string key, string regionName = null)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            MruItem mruEntry = null;
            var     cacheKey = new CacheKey(key);

            // code block to get item from store and if found, return it if not expired.
            Func <bool> block = (() =>
            {
                lock (_locker)
                {
                    // try to get from the store
                    mruEntry = MruManager.GetItem(cacheKey);
                    if (mruEntry != null)
                    {
                        cacheKey = (CacheKey)mruEntry.Key;
                        if (IsNotExpired(cacheKey, (CacheEntry)mruEntry.Value, false))
                        {
                            return(true);
                        }
                    }
                    return(false);
                }
            });

            if (block())
            {
                return(new CacheItem(key, mruEntry.Value));
            }

            #region try to update the Cache entry if it is expired by calling the Update callback.
            if (CacheEntrySetUpdateCallback == null)
            {
                return(null);
            }

            CacheEntrySetUpdateCallback(new CacheEntryUpdateArguments[] { new CacheEntryUpdateArguments(this, CacheEntryRemovedReason.Expired, key, null) });
            // try to get from the store a 2nd time and see if item got updated & no longer expired...
            if (block())
            {
                return(new CacheItem(key, mruEntry.Value));
            }
            #endregion
            return(null);
        }
Ejemplo n.º 4
0
        public override bool Contains(string key, string regionName = null)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            var  cacheKey = new CacheKey(key);
            bool b;

            lock (_locker)
            {
                // try to get from the store
                var mruEntry = MruManager.GetItem(cacheKey);
                if (mruEntry == null)
                {
                    return(false);
                }

                cacheKey = (CacheKey)mruEntry.Key;
                b        = IsNotExpired(cacheKey, (CacheEntry)mruEntry.Value, false);
                if (b || CacheEntrySetUpdateCallback == null)
                {
                    return(b);
                }
            }
            // try to update the Cache entry if it is expired by calling the Update callback.
            CacheEntrySetUpdateCallback(new CacheEntryUpdateArguments[]
                                        { new CacheEntryUpdateArguments(this, CacheEntryRemovedReason.Expired, key, null) });
            lock (_locker)
            {
                // try to get from the store a 2nd time and see if item got updated & no longer expired...
                var mruEntry = MruManager.GetItem(cacheKey);
                if (mruEntry != null)
                {
                    cacheKey = (CacheKey)mruEntry.Key;
                    b        = IsNotExpired(cacheKey, (CacheEntry)mruEntry.Value, false);
                }
                return(b);
            }
        }