Пример #1
0
        // private members

        private void AddToCache(MemoryCacheEntry entry)
        {
            // add outside of lock
            if (entry != null)
            {
                if (entry.HasExpiration())
                {
                    _expires.Add(entry);
                }

                if (entry.HasUsage() &&
                    (!entry.HasExpiration() || entry.UtcAbsExp - DateTime.UtcNow >= CacheUsage.MIN_LIFETIME_FOR_USAGE))
                {
                    _usage.Add(entry);
                }

                entry.State = EntryState.AddedToCache;
                entry.CallNotifyOnChanged();
                if (_perfCounters != null)
                {
                    _perfCounters.Increment(PerfCounterName.Entries);
                    _perfCounters.Increment(PerfCounterName.Turnover);
                }
            }
        }
Пример #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);
        }
Пример #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);
            }
        }
Пример #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);
        }
Пример #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 members        

        private void AddToCache(MemoryCacheEntry entry) {
            // add outside of lock
            if (entry != null) {
                if (entry.HasExpiration()) {
                    _expires.Add(entry);
                }

                if (entry.HasUsage()
                    && (!entry.HasExpiration() || entry.UtcAbsExp - DateTime.UtcNow >= CacheUsage.MIN_LIFETIME_FOR_USAGE)) {
                    _usage.Add(entry);
                }

                entry.State = EntryState.AddedToCache;
                entry.CallNotifyOnChanged();
                if (_perfCounters != null) {
                    _perfCounters.Increment(PerfCounterName.Entries);
                    _perfCounters.Increment(PerfCounterName.Turnover);
                }
            }
        }
 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;
 }
Пример #8
0
        // private members

        private void AddToCache(MemoryCacheEntry entry)
        {
            // add outside of lock
            if (entry == null)
            {
                return;
            }

            if (entry.HasExpiration())
            {
                _expires.Add(entry);
            }

            if (entry.HasUsage() &&
                (!entry.HasExpiration() || entry.UtcAbsExp - DateTime.UtcNow >= CacheUsage.MIN_LIFETIME_FOR_USAGE))
            {
                _usage.Add(entry);
            }

            // One last sanity check to be sure we didn't fall victim to an Add concurrency
            if (!entry.CompareExchangeState(EntryState.AddedToCache, EntryState.AddingToCache))
            {
                if (entry.InExpires())
                {
                    _expires.Remove(entry);
                }

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

            entry.CallNotifyOnChanged();
            if (_perfCounters != null)
            {
                _perfCounters.Increment(PerfCounterName.Entries);
                _perfCounters.Increment(PerfCounterName.Turnover);
            }
        }
        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);
            }
            // Dev10 861163: 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);
            }
        }
 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);
     
     // Dev10 861163: 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;
 }
 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);
     }
 }