Exemplo n.º 1
0
        // 'updatePerfCounters' defaults to true since this method is called by all Get() operations
        // to update both the performance counters and the sliding expiration. Callers that perform
        // nested sliding expiration updates (like a MemoryCacheEntry touching its update sentinel)
        // can pass false to prevent these from unintentionally showing up in the perf counters.
        internal void UpdateExpAndUsage(MemoryCacheEntry entry, bool updatePerfCounters = true)
        {
            if (entry != null)
            {
                if (entry.InUsage() || entry.SlidingExp > TimeSpan.Zero)
                {
                    DateTime utcNow = DateTime.UtcNow;
                    entry.UpdateSlidingExp(utcNow, _expires);
                    entry.UpdateUsage(utcNow, _usage);
                }

                // If this entry has an update sentinel, the sliding expiration is actually associated
                // with that sentinel, not with this entry. We need to update the sentinel's sliding expiration to
                // keep the sentinel from expiring, which in turn would force a removal of this entry from the cache.
                entry.UpdateSlidingExpForUpdateSentinel();

                if (updatePerfCounters && _perfCounters != null && _countersSupported)
                {
                    _perfCounters.Increment(CounterName.Hits);
                    _perfCounters.Increment(CounterName.HitRatio);
                    _perfCounters.Increment(CounterName.HitRatioBase);
                }
            }
            else
            {
                if (updatePerfCounters && _perfCounters != null && _countersSupported)
                {
                    _perfCounters.Increment(CounterName.Misses);
                    _perfCounters.Increment(CounterName.HitRatioBase);
                }
            }
        }
Exemplo n.º 2
0
        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);
                }

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

                entry.State = EntryState.RemovedFromCache;
                if (!delayRelease)
                {
                    entry.Release(_cache, reason);
                }
                if (_perfCounters != null && _countersSupported)
                {
                    _perfCounters.Decrement(CounterName.Entries);
                    _perfCounters.Increment(CounterName.Turnover);
                }
            }
        }
Exemplo n.º 3
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);
            }
        }
        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);
                }
            }
        }
Exemplo n.º 5
0
 private void UpdateExpAndUsage(MemoryCacheEntry entry)
 {
     if (entry != null)
     {
         if (entry.InUsage() || (entry.SlidingExp > TimeSpan.Zero))
         {
             DateTime utcNow = DateTime.UtcNow;
             entry.UpdateSlidingExp(utcNow, this._expires);
             entry.UpdateUsage(utcNow, this._usage);
         }
         if (this._perfCounters != null)
         {
             this._perfCounters.Increment(PerfCounterName.Hits);
             this._perfCounters.Increment(PerfCounterName.HitRatio);
             this._perfCounters.Increment(PerfCounterName.HitRatioBase);
         }
     }
     else if (this._perfCounters != null)
     {
         this._perfCounters.Increment(PerfCounterName.Misses);
         this._perfCounters.Increment(PerfCounterName.HitRatioBase);
     }
 }
Exemplo n.º 6
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);
         }
     }
 }
        // 'updatePerfCounters' defaults to true since this method is called by all Get() operations
        // to update both the performance counters and the sliding expiration. Callers that perform
        // nested sliding expiration updates (like a MemoryCacheEntry touching its update sentinel)
        // can pass false to prevent these from unintentionally showing up in the perf counters.
        internal void UpdateExpAndUsage(MemoryCacheEntry entry, bool updatePerfCounters = true) {
            if (entry != null) {
                if (entry.InUsage() || entry.SlidingExp > TimeSpan.Zero) {
                    DateTime utcNow = DateTime.UtcNow;
                    entry.UpdateSlidingExp(utcNow, _expires);
                    entry.UpdateUsage(utcNow, _usage);
                }

                // DevDiv #67021: If this entry has an update sentinel, the sliding expiration is actually associated
                // with that sentinel, not with this entry. We need to update the sentinel's sliding expiration to
                // keep the sentinel from expiring, which in turn would force a removal of this entry from the cache.
                entry.UpdateSlidingExpForUpdateSentinel();

                if (updatePerfCounters && _perfCounters != null) {
                    _perfCounters.Increment(PerfCounterName.Hits);
                    _perfCounters.Increment(PerfCounterName.HitRatio);
                    _perfCounters.Increment(PerfCounterName.HitRatioBase);
                }
            }
            else {
                if (updatePerfCounters && _perfCounters != null) {
                    _perfCounters.Increment(PerfCounterName.Misses);
                    _perfCounters.Increment(PerfCounterName.HitRatioBase);
                }
            }
        }
 private void UpdateExpAndUsage(MemoryCacheEntry entry)
 {
     if (entry != null)
     {
         if (entry.InUsage() || (entry.SlidingExp > TimeSpan.Zero))
         {
             DateTime utcNow = DateTime.UtcNow;
             entry.UpdateSlidingExp(utcNow, this._expires);
             entry.UpdateUsage(utcNow, this._usage);
         }
         if (this._perfCounters != null)
         {
             this._perfCounters.Increment(PerfCounterName.Hits);
             this._perfCounters.Increment(PerfCounterName.HitRatio);
             this._perfCounters.Increment(PerfCounterName.HitRatioBase);
         }
     }
     else if (this._perfCounters != null)
     {
         this._perfCounters.Increment(PerfCounterName.Misses);
         this._perfCounters.Increment(PerfCounterName.HitRatioBase);
     }
 }
 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);
         }
     }
 }