Пример #1
0
        protected override void PurgeCache(object sender, System.Timers.ElapsedEventArgs e)
        {
            ReaderWriterLock readWriteLock = new ReaderWriterLock();

            System.Threading.Thread.CurrentThread.Priority = ThreadPriority.BelowNormal; // This process is not critical as according to add or update.

            if (!Monitor.TryEnter(isPurging))
            {
                return;
            }

            try
            {
                readWriteLock.AcquireWriterLock(MAX_LOCK_WAIT);

                try
                {
                    List <object> expiredCacheObjects = new List <object>();

                    foreach (TimedCacheKey loopExpiredCacheObject in timedStorage.Keys)
                    {
                        if (loopExpiredCacheObject.ExpirationDate < e.SignalTime)
                        {
                            expiredCacheObjects.Add(loopExpiredCacheObject);
                        }
                        else
                        {
                            break;
                        }
                    }

                    foreach (object key in expiredCacheObjects)
                    {
                        TimedCacheKey timedCacheKey = timedStorageIndex[key];
                        timedStorageIndex.Remove(timedCacheKey.Key);

                        timedStorage.Remove(timedCacheKey);
                    }
                }
                finally
                {
                    readWriteLock.ReleaseWriterLock();
                }
            }
            finally
            {
                Monitor.Exit(isPurging);
            }
        }
Пример #2
0
        protected override bool Add(object key, object value, TimeSpan slidingExpirationTime)
        {
            if (timedStorageIndex.ContainsKey(key))
            {
                return(false);
            }
            else
            {
                TimedCacheKey cacheKey = new TimedCacheKey(key, slidingExpirationTime);

                timedStorage.Add(cacheKey, value);
                timedStorageIndex.Add(key, cacheKey);

                return(true);
            }
        }
Пример #3
0
        protected override object GetTimed(object key)
        {
            if (timedStorageIndex.ContainsKey(key))
            {
                Object        cacheVal;
                TimedCacheKey cacheKey = timedStorageIndex[key];

                cacheVal = timedStorage[cacheKey];

                timedStorage.Remove(cacheKey);
                cacheKey.Accessed();

                timedStorage.Add(cacheKey, cacheVal);

                return(cacheVal);
            }
            else
            {
                return(null);
            }
        }
Пример #4
0
        protected override bool Update(object key, object value, DateTime expirationDate)
        {
            if (untimedStorage.ContainsKey(key))
            {
                untimedStorage.Remove(key);
            }
            else if (timedStorageIndex.ContainsKey(key))
            {
                timedStorage.Remove(timedStorageIndex[key]);
                timedStorageIndex.Remove(key);
            }
            else
            {
                return(false);
            }

            TimedCacheKey cacheKey = new TimedCacheKey(key, expirationDate);

            timedStorage.Add(cacheKey, value);
            timedStorageIndex.Add(key, cacheKey);

            return(true);
        }