private void RemoveFromCache(MemoryCacheEntry entry, CacheEntryRemovedReason reason, bool delayRelease = false)
        {
            // release outside of lock
            if (entry != null)
            {
                if (entry.InExpires())
                {
                    _expires.Remove(entry);
                }

                if (entry.InUsage())
                {
                    _usage.Remove(entry);
                }

                Dbg.Assert(entry.State == EntryState.RemovingFromCache, "entry.State = EntryState.RemovingFromCache");

                entry.State = EntryState.RemovedFromCache;
                if (!delayRelease)
                {
                    entry.Release(_cache, reason);
                }
                if (_perfCounters != null)
                {
                    _perfCounters.Decrement(PerfCounterName.Entries);
                    _perfCounters.Increment(PerfCounterName.Turnover);
                }
            }
        }
Example #2
0
        internal MemoryCacheEntry AddOrGetExisting(MemoryCacheKey key, MemoryCacheEntry entry)
        {
            if (_useInsertBlock && entry.HasUsage())
            {
                WaitInsertBlock();
            }
            MemoryCacheEntry existingEntry     = null;
            MemoryCacheEntry toBeReleasedEntry = null;
            bool             added             = false;

            lock (_entriesLock)
            {
                if (_disposed == 0)
                {
                    existingEntry = _entries[key] as MemoryCacheEntry;
                    // has it expired?
                    if (existingEntry != null && existingEntry.UtcAbsExp <= DateTime.UtcNow)
                    {
                        toBeReleasedEntry       = existingEntry;
                        toBeReleasedEntry.State = EntryState.RemovingFromCache;
                        existingEntry           = null;
                    }
                    // can we add entry to the cache?
                    if (existingEntry == null)
                    {
                        entry.State   = EntryState.AddingToCache;
                        added         = true;
                        _entries[key] = entry;
                    }
                }
            }
            // release outside of lock
            RemoveFromCache(toBeReleasedEntry, CacheEntryRemovedReason.Expired, delayRelease: true);
            if (added)
            {
                // add outside of lock
                AddToCache(entry);
            }
            // update outside of lock
            UpdateExpAndUsage(existingEntry);

            // Call Release after the new entry has been completely added so
            // that the CacheItemRemovedCallback can take a dependency on the newly inserted item.
            if (toBeReleasedEntry != null)
            {
                toBeReleasedEntry.Release(_cache, CacheEntryRemovedReason.Expired);
            }
            return(existingEntry);
        }
Example #3
0
        internal void Set(MemoryCacheKey key, MemoryCacheEntry entry)
        {
            if (_useInsertBlock && entry.HasUsage())
            {
                WaitInsertBlock();
            }
            MemoryCacheEntry existingEntry = null;
            bool             added         = false;

            lock (_entriesLock)
            {
                if (_disposed == 0)
                {
                    existingEntry = _entries[key] as MemoryCacheEntry;
                    if (existingEntry != null)
                    {
                        existingEntry.State = EntryState.RemovingFromCache;
                    }
                    entry.State   = EntryState.AddingToCache;
                    added         = true;
                    _entries[key] = entry;
                }
            }

            CacheEntryRemovedReason reason = CacheEntryRemovedReason.Removed;

            if (existingEntry != null)
            {
                if (existingEntry.UtcAbsExp <= DateTime.UtcNow)
                {
                    reason = CacheEntryRemovedReason.Expired;
                }
                RemoveFromCache(existingEntry, reason, delayRelease: true);
            }
            if (added)
            {
                AddToCache(entry);
            }

            // Call Release after the new entry has been completely added so
            // that the CacheItemRemovedCallback can take a dependency on the newly inserted item.
            if (existingEntry != null)
            {
                existingEntry.Release(_cache, reason);
            }
        }
Example #4
0
        internal MemoryCacheEntry AddOrGetExisting(MemoryCacheKey key, MemoryCacheEntry entry)
        {
            if (this._useInsertBlock && entry.HasUsage())
            {
                this.WaitInsertBlock();
            }
            MemoryCacheEntry entry2 = null;
            MemoryCacheEntry entry3 = null;
            bool             flag   = false;

            lock (this._entriesLock)
            {
                if (this._disposed == 0)
                {
                    entry2 = this._entries[key] as MemoryCacheEntry;
                    if ((entry2 != null) && (entry.UtcAbsExp <= DateTime.UtcNow))
                    {
                        entry3       = entry2;
                        entry3.State = EntryState.RemovingFromCache;
                        entry2       = null;
                    }
                    if (entry2 == null)
                    {
                        entry.State        = EntryState.AddingToCache;
                        flag               = true;
                        this._entries[key] = entry;
                    }
                }
            }
            bool delayRelease = true;

            this.RemoveFromCache(entry3, CacheEntryRemovedReason.Expired, delayRelease);
            if (flag)
            {
                this.AddToCache(entry);
            }
            this.UpdateExpAndUsage(entry2);
            if (entry3 != null)
            {
                entry3.Release(this._cache, CacheEntryRemovedReason.Expired);
            }
            return(entry2);
        }
Example #5
0
        internal void Set(MemoryCacheKey key, MemoryCacheEntry entry)
        {
            if (this._useInsertBlock && entry.HasUsage())
            {
                this.WaitInsertBlock();
            }
            MemoryCacheEntry entry2 = null;
            bool             flag   = false;

            lock (this._entriesLock)
            {
                if (this._disposed == 0)
                {
                    entry2 = this._entries[key] as MemoryCacheEntry;
                    if (entry2 != null)
                    {
                        entry2.State = EntryState.RemovingFromCache;
                    }
                    entry.State        = EntryState.AddingToCache;
                    flag               = true;
                    this._entries[key] = entry;
                }
            }
            CacheEntryRemovedReason removed = CacheEntryRemovedReason.Removed;

            if (entry2 != null)
            {
                if (entry2.UtcAbsExp <= DateTime.UtcNow)
                {
                    removed = CacheEntryRemovedReason.Expired;
                }
                bool delayRelease = true;
                this.RemoveFromCache(entry2, removed, delayRelease);
            }
            if (flag)
            {
                this.AddToCache(entry);
            }
            if (entry2 != null)
            {
                entry2.Release(this._cache, removed);
            }
        }
        private void RemoveFromCache(MemoryCacheEntry entry, CacheEntryRemovedReason reason, bool delayRelease = false) {
            // release outside of lock
            if (entry != null) {
                if (entry.InExpires()) {
                    _expires.Remove(entry);
                }

                if (entry.InUsage()) {
                    _usage.Remove(entry);
                }

                Dbg.Assert(entry.State == EntryState.RemovingFromCache, "entry.State = EntryState.RemovingFromCache");

                entry.State = EntryState.RemovedFromCache;
                if (!delayRelease) {
                    entry.Release(_cache, reason);
                }
                if (_perfCounters != null) {
                    _perfCounters.Decrement(PerfCounterName.Entries);
                    _perfCounters.Increment(PerfCounterName.Turnover);
                }
            }
        }
Example #7
0
 private void RemoveFromCache(MemoryCacheEntry entry, CacheEntryRemovedReason reason, bool delayRelease = false)
 {
     if (entry != null)
     {
         if (entry.InExpires())
         {
             this._expires.Remove(entry);
         }
         if (entry.InUsage())
         {
             this._usage.Remove(entry);
         }
         entry.State = EntryState.RemovedFromCache;
         if (!delayRelease)
         {
             entry.Release(this._cache, reason);
         }
         if (this._perfCounters != null)
         {
             this._perfCounters.Decrement(PerfCounterName.Entries);
             this._perfCounters.Increment(PerfCounterName.Turnover);
         }
     }
 }
 private void RemoveFromCache(MemoryCacheEntry entry, CacheEntryRemovedReason reason, bool delayRelease = false)
 {
     if (entry != null)
     {
         if (entry.InExpires())
         {
             this._expires.Remove(entry);
         }
         if (entry.InUsage())
         {
             this._usage.Remove(entry);
         }
         entry.State = EntryState.RemovedFromCache;
         if (!delayRelease)
         {
             entry.Release(this._cache, reason);
         }
         if (this._perfCounters != null)
         {
             this._perfCounters.Decrement(PerfCounterName.Entries);
             this._perfCounters.Increment(PerfCounterName.Turnover);
         }
     }
 }