예제 #1
0
        long DoRemoveExpiredItems(bool needLock)
        {
            long count  = 0;
            bool locked = false;

            try {
                if (needLock)
                {
                    cache_lock.EnterWriteLock();
                    locked = true;
                }

                if (timedItems == null)
                {
                    return(0);
                }

                long             now   = DateTime.UtcNow.Ticks;
                MemoryCacheEntry entry = timedItems.Peek();

                while (entry != null)
                {
                    if (entry.Disabled)
                    {
                        timedItems.Dequeue();
                        entry = timedItems.Peek();
                        continue;
                    }

                    if (entry.ExpiresAt > now)
                    {
                        break;
                    }

                    timedItems.Dequeue();

                    if (entry.IsSliding && entry.ExpiresAtNext > now)
                    {
                        entry.ResetSlidingExpiry();
                        timedItems.Enqueue(entry);
                        break;
                    }

                    count++;
                    DoRemoveEntry(entry, true, entry.Key, CacheEntryRemovedReason.Expired);
                    entry = timedItems.Peek();
                }

                if (entry == null)
                {
                    timedItems = null;
                    expirationTimer.Dispose();
                    expirationTimer = null;
                }

                return(count);
            } finally {
                if (locked)
                {
                    cache_lock.ExitWriteLock();
                }
            }
        }