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 #2
0
 internal MemoryCacheEntry RemoveEntry(string key, MemoryCacheEntry entry, CacheEntryRemovedReason reason) {
     MemoryCacheKey cacheKey = new MemoryCacheKey(key);
     MemoryCacheStore store = GetStore(cacheKey);
     return store.Remove(cacheKey, entry, reason);
 }
Example #3
0
 internal MemoryCacheEntry GetEntry(String key) {
     if (IsDisposed) {
         return null;
     }
     MemoryCacheKey cacheKey = new MemoryCacheKey(key);
     MemoryCacheStore store = GetStore(cacheKey);
     return store.Get(cacheKey);
 }
Example #4
0
 private object AddOrGetExistingInternal(string key, object value, CacheItemPolicy policy) {
     if (key == null) {
         throw new ArgumentNullException("key");
     }
     DateTimeOffset absExp = ObjectCache.InfiniteAbsoluteExpiration;
     TimeSpan slidingExp = ObjectCache.NoSlidingExpiration;
     CacheItemPriority priority = CacheItemPriority.Default;
     Collection<ChangeMonitor> changeMonitors = null;
     CacheEntryRemovedCallback removedCallback = null;
     if (policy != null) {
         ValidatePolicy(policy);
         if (policy.UpdateCallback != null) {
             throw new ArgumentException(R.Update_callback_must_be_null, "policy");
         }
         absExp = policy.AbsoluteExpiration;
         slidingExp = policy.SlidingExpiration;
         priority = policy.Priority;
         changeMonitors = policy.ChangeMonitors;
         removedCallback = policy.RemovedCallback;
     }            
     if (IsDisposed) {
         if (changeMonitors != null) {
             foreach (ChangeMonitor monitor in changeMonitors) {
                 if (monitor != null) {
                     monitor.Dispose();
                 }
             }
         }
         return null;            
     }
     MemoryCacheKey cacheKey = new MemoryCacheKey(key);
     MemoryCacheStore store = GetStore(cacheKey);
     MemoryCacheEntry entry = store.AddOrGetExisting(cacheKey, new MemoryCacheEntry(key, value, absExp, slidingExp, priority, changeMonitors, removedCallback, this));
     return (entry != null) ? entry.Value : null;
 }
 internal MemoryCacheEntry Remove(MemoryCacheKey key, MemoryCacheEntry entryToRemove, CacheEntryRemovedReason reason)
 {
     MemoryCacheEntry objA = null;
     lock (this._entriesLock)
     {
         if (this._disposed == 0)
         {
             objA = this._entries[key] as MemoryCacheEntry;
             if ((entryToRemove == null) || object.ReferenceEquals(objA, entryToRemove))
             {
                 if (objA != null)
                 {
                     objA.State = EntryState.RemovingFromCache;
                 }
                 this._entries.Remove(key);
             }
             else
             {
                 objA = null;
             }
         }
     }
     this.RemoveFromCache(objA, reason, false);
     return objA;
 }
 public override void Set(string key, object value, CacheItemPolicy policy, string regionName = null)
 {
     if (regionName != null)
     {
         throw new NotSupportedException(R.RegionName_not_supported);
     }
     if (key == null)
     {
         throw new ArgumentNullException("key");
     }
     DateTimeOffset infiniteAbsoluteExpiration = ObjectCache.InfiniteAbsoluteExpiration;
     TimeSpan noSlidingExpiration = ObjectCache.NoSlidingExpiration;
     CacheItemPriority priority = CacheItemPriority.Default;
     Collection<ChangeMonitor> dependencies = null;
     CacheEntryRemovedCallback removedCallback = null;
     if (policy != null)
     {
         this.ValidatePolicy(policy);
         if (policy.UpdateCallback != null)
         {
             this.Set(key, value, policy.ChangeMonitors, policy.AbsoluteExpiration, policy.SlidingExpiration, policy.UpdateCallback);
             return;
         }
         infiniteAbsoluteExpiration = policy.AbsoluteExpiration;
         noSlidingExpiration = policy.SlidingExpiration;
         priority = policy.Priority;
         dependencies = policy.ChangeMonitors;
         removedCallback = policy.RemovedCallback;
     }
     if (this.IsDisposed)
     {
         if (dependencies != null)
         {
             foreach (ChangeMonitor monitor in dependencies)
             {
                 if (monitor != null)
                 {
                     monitor.Dispose();
                 }
             }
         }
     }
     else
     {
         MemoryCacheKey key2 = new MemoryCacheKey(key);
         this.GetStore(key2).Set(key2, new MemoryCacheEntry(key, value, infiniteAbsoluteExpiration, noSlidingExpiration, priority, dependencies, removedCallback, this));
     }
 }
 internal MemoryCacheStore GetStore(MemoryCacheKey cacheKey)
 {
     int index = Math.Abs(cacheKey.Hash) & this._storeMask;
     return this._stores[index];
 }
 private object AddOrGetExistingInternal(string key, object value, CacheItemPolicy policy)
 {
     if (key == null)
     {
         throw new ArgumentNullException("key");
     }
     DateTimeOffset infiniteAbsoluteExpiration = ObjectCache.InfiniteAbsoluteExpiration;
     TimeSpan noSlidingExpiration = ObjectCache.NoSlidingExpiration;
     CacheItemPriority priority = CacheItemPriority.Default;
     Collection<ChangeMonitor> dependencies = null;
     CacheEntryRemovedCallback removedCallback = null;
     if (policy != null)
     {
         this.ValidatePolicy(policy);
         if (policy.UpdateCallback != null)
         {
             throw new ArgumentException(R.Update_callback_must_be_null, "policy");
         }
         infiniteAbsoluteExpiration = policy.AbsoluteExpiration;
         noSlidingExpiration = policy.SlidingExpiration;
         priority = policy.Priority;
         dependencies = policy.ChangeMonitors;
         removedCallback = policy.RemovedCallback;
     }
     if (this.IsDisposed)
     {
         if (dependencies != null)
         {
             foreach (ChangeMonitor monitor in dependencies)
             {
                 if (monitor != null)
                 {
                     monitor.Dispose();
                 }
             }
         }
         return null;
     }
     MemoryCacheKey key2 = new MemoryCacheKey(key);
     MemoryCacheEntry entry = this.GetStore(key2).AddOrGetExisting(key2, new MemoryCacheEntry(key, value, infiniteAbsoluteExpiration, noSlidingExpiration, priority, dependencies, removedCallback, this));
     if (entry == null)
     {
         return null;
     }
     return entry.Value;
 }
 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;
 }
        int IEqualityComparer.GetHashCode(object obj)
        {
            MemoryCacheKey cacheKey = (MemoryCacheKey)obj;

            return(cacheKey.Hash);
        }
Example #11
0
        // DevDiv Bugs 162763:
        // Add a an event that fires *before* an item is evicted from the ASP.NET Cache
        internal void Set(string key,
                          object value,
                          Collection <ChangeMonitor> changeMonitors,
                          DateTimeOffset absoluteExpiration,
                          TimeSpan slidingExpiration,
                          CacheEntryUpdateCallback onUpdateCallback)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            if (changeMonitors == null &&
                absoluteExpiration == ObjectCache.InfiniteAbsoluteExpiration &&
                slidingExpiration == ObjectCache.NoSlidingExpiration)
            {
                throw new ArgumentException(R.Invalid_argument_combination);
            }
            if (onUpdateCallback == null)
            {
                throw new ArgumentNullException("onUpdateCallback");
            }
            if (IsDisposed)
            {
                if (changeMonitors != null)
                {
                    foreach (ChangeMonitor monitor in changeMonitors)
                    {
                        if (monitor != null)
                        {
                            monitor.Dispose();
                        }
                    }
                }
                return;
            }
            // Insert updatable cache entry
            MemoryCacheKey   cacheKey   = new MemoryCacheKey(key);
            MemoryCacheStore store      = GetStore(cacheKey);
            MemoryCacheEntry cacheEntry = new MemoryCacheEntry(key,
                                                               value,
                                                               ObjectCache.InfiniteAbsoluteExpiration,
                                                               ObjectCache.NoSlidingExpiration,
                                                               CacheItemPriority.NotRemovable,
                                                               null,
                                                               null,
                                                               this);

            store.Set(cacheKey, cacheEntry);

            // Ensure the sentinel depends on its updatable entry
            string[]      cacheKeys          = { key };
            ChangeMonitor expensiveObjectDep = CreateCacheEntryChangeMonitor(cacheKeys);

            if (changeMonitors == null)
            {
                changeMonitors = new Collection <ChangeMonitor>();
            }
            changeMonitors.Add(expensiveObjectDep);

            // Insert sentinel entry for the updatable cache entry
            MemoryCacheKey   sentinelCacheKey   = new MemoryCacheKey("OnUpdateSentinel" + key);
            MemoryCacheStore sentinelStore      = GetStore(sentinelCacheKey);
            MemoryCacheEntry sentinelCacheEntry = new MemoryCacheEntry(sentinelCacheKey.Key,
                                                                       new SentinelEntry(key, expensiveObjectDep, onUpdateCallback),
                                                                       absoluteExpiration,
                                                                       slidingExpiration,
                                                                       CacheItemPriority.NotRemovable,
                                                                       changeMonitors,
                                                                       s_sentinelRemovedCallback,
                                                                       this);

            sentinelStore.Set(sentinelCacheKey, sentinelCacheEntry);
            cacheEntry.ConfigureUpdateSentinel(sentinelStore, sentinelCacheEntry);
        }
        internal MemoryCacheEntry RemoveEntry(string key, MemoryCacheEntry entry, CacheEntryRemovedReason reason)
        {
            MemoryCacheKey key2 = new MemoryCacheKey(key);

            return(this.GetStore(key2).Remove(key2, entry, reason));
        }
        internal MemoryCacheStore GetStore(MemoryCacheKey cacheKey)
        {
            int index = Math.Abs(cacheKey.Hash) & this._storeMask;

            return(this._stores[index]);
        }
 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);
     }
 }
Example #15
0
 public override void Set(string key, object value, CacheItemPolicy policy, string regionName = null) {
     if (regionName != null) {
         throw new NotSupportedException(R.RegionName_not_supported);
     }
     if (key == null) {
         throw new ArgumentNullException("key");
     }
     DateTimeOffset absExp = ObjectCache.InfiniteAbsoluteExpiration;
     TimeSpan slidingExp = ObjectCache.NoSlidingExpiration;
     CacheItemPriority priority = CacheItemPriority.Default;
     Collection<ChangeMonitor> changeMonitors = null;
     CacheEntryRemovedCallback removedCallback = null;
     if (policy != null) {
         ValidatePolicy(policy);
         if (policy.UpdateCallback != null) {
             Set(key, value, policy.ChangeMonitors, policy.AbsoluteExpiration, policy.SlidingExpiration, policy.UpdateCallback);
             return;
         }
         absExp = policy.AbsoluteExpiration;
         slidingExp = policy.SlidingExpiration;
         priority = policy.Priority;
         changeMonitors = policy.ChangeMonitors;
         removedCallback = policy.RemovedCallback;
     }
     if (IsDisposed) {
         if (changeMonitors != null) {
             foreach (ChangeMonitor monitor in changeMonitors) {
                 if (monitor != null) {
                     monitor.Dispose();
                 }
             }
         }
         return;
     }            
     MemoryCacheKey cacheKey = new MemoryCacheKey(key);
     MemoryCacheStore store = GetStore(cacheKey);
     store.Set(cacheKey, new MemoryCacheEntry(key, value, absExp, slidingExp, priority, changeMonitors, removedCallback, this));
 }
 internal MemoryCacheEntry Get(MemoryCacheKey key) {
     MemoryCacheEntry entry = _entries[key] as MemoryCacheEntry;
     // has it expired?
     if (entry != null && entry.UtcAbsExp <= DateTime.UtcNow) {
         Remove(key, entry, CacheEntryRemovedReason.Expired);
         entry = null;
     }
     // update outside of lock
     UpdateExpAndUsage(entry);
     return entry;
 }
Example #17
0
        // DevDiv Bugs 162763: 
        // Add a an event that fires *before* an item is evicted from the ASP.NET Cache
        internal void Set(string key, 
                          object value,
                          Collection<ChangeMonitor> changeMonitors,
                          DateTimeOffset absoluteExpiration,
                          TimeSpan slidingExpiration,
                          CacheEntryUpdateCallback onUpdateCallback) {
            if (key == null) {
                throw new ArgumentNullException("key");
            }
            if (changeMonitors == null
                && absoluteExpiration == ObjectCache.InfiniteAbsoluteExpiration 
                && slidingExpiration == ObjectCache.NoSlidingExpiration) {
                throw new ArgumentException(R.Invalid_argument_combination);
            }
            if (onUpdateCallback == null) {
                throw new ArgumentNullException("onUpdateCallback");
            }
            if (IsDisposed) {
                if (changeMonitors != null) {
                    foreach (ChangeMonitor monitor in changeMonitors) {
                        if (monitor != null) {
                            monitor.Dispose();
                        }
                    }
                }
                return;
            }
            // Insert updatable cache entry
            MemoryCacheKey cacheKey = new MemoryCacheKey(key);
            MemoryCacheStore store = GetStore(cacheKey);
            MemoryCacheEntry cacheEntry = new MemoryCacheEntry(key, 
                                                               value, 
                                                               ObjectCache.InfiniteAbsoluteExpiration, 
                                                               ObjectCache.NoSlidingExpiration, 
                                                               CacheItemPriority.NotRemovable, 
                                                               null,
                                                               null, 
                                                               this);
            store.Set(cacheKey, cacheEntry);

            // Ensure the sentinel depends on its updatable entry
            string[] cacheKeys = { key };
            ChangeMonitor expensiveObjectDep = CreateCacheEntryChangeMonitor(cacheKeys);
            if (changeMonitors == null) {
                changeMonitors = new Collection<ChangeMonitor>();
            }
            changeMonitors.Add(expensiveObjectDep);

            // Insert sentinel entry for the updatable cache entry 
            MemoryCacheKey sentinelCacheKey = new MemoryCacheKey("OnUpdateSentinel" + key);
            MemoryCacheStore sentinelStore = GetStore(sentinelCacheKey);
            MemoryCacheEntry sentinelCacheEntry = new MemoryCacheEntry(sentinelCacheKey.Key,
                                                                       new SentinelEntry(key, expensiveObjectDep, onUpdateCallback),
                                                                       absoluteExpiration, 
                                                                       slidingExpiration,
                                                                       CacheItemPriority.NotRemovable, 
                                                                       changeMonitors,
                                                                       s_sentinelRemovedCallback, 
                                                                       this);
            sentinelStore.Set(sentinelCacheKey, sentinelCacheEntry);
            cacheEntry.ConfigureUpdateSentinel(sentinelStore, sentinelCacheEntry);
        }
 internal MemoryCacheEntry Remove(MemoryCacheKey key, MemoryCacheEntry entryToRemove, CacheEntryRemovedReason reason) {
     MemoryCacheEntry entry = null;
     lock (_entriesLock) {
         if (_disposed == 0) {
             // get current entry
             entry = _entries[key] as MemoryCacheEntry;
             // remove if it matches the entry to be removed (but always remove if entryToRemove is null)
             if (entryToRemove == null || Object.ReferenceEquals(entry, entryToRemove)) {
                 // Dev10 865887: MemoryCache.Remove("\ue637\ud22a\u3e17") causes NullReferenceEx
                 if (entry != null) {
                     entry.State = EntryState.RemovingFromCache;
                     _entries.Remove(key);
                 }
             }
             else {
                 entry = null;
             }
         }
     }
     // release outside of lock
     RemoveFromCache(entry, reason);
     return entry;
 }
 internal MemoryCacheEntry GetEntry(string key)
 {
     if (this.IsDisposed)
     {
         return null;
     }
     MemoryCacheKey key2 = new MemoryCacheKey(key);
     return this.GetStore(key2).Get(key2);
 }
        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 RemoveEntry(string key, MemoryCacheEntry entry, CacheEntryRemovedReason reason)
 {
     MemoryCacheKey key2 = new MemoryCacheKey(key);
     return this.GetStore(key2).Remove(key2, entry, reason);
 }
Example #22
0
        // private and internal

        internal MemoryCacheStore GetStore(MemoryCacheKey cacheKey) {
            // Dev10 865907: Math.Abs throws OverflowException for Int32.MinValue
            int hashCode = cacheKey.Hash;
            if (hashCode < 0) {
                hashCode = (hashCode == Int32.MinValue) ? 0 : -hashCode;
            }
            int idx = hashCode & _storeMask;
            return _stores[idx];
        }
 internal void Set(string key, object value, Collection<ChangeMonitor> changeMonitors, DateTimeOffset absoluteExpiration, TimeSpan slidingExpiration, CacheEntryUpdateCallback onUpdateCallback)
 {
     if (key == null)
     {
         throw new ArgumentNullException("key");
     }
     if (((changeMonitors == null) && (absoluteExpiration == ObjectCache.InfiniteAbsoluteExpiration)) && (slidingExpiration == ObjectCache.NoSlidingExpiration))
     {
         throw new ArgumentException(R.Invalid_argument_combination);
     }
     if (onUpdateCallback == null)
     {
         throw new ArgumentNullException("onUpdateCallback");
     }
     if (this.IsDisposed)
     {
         if (changeMonitors != null)
         {
             foreach (ChangeMonitor monitor in changeMonitors)
             {
                 if (monitor != null)
                 {
                     monitor.Dispose();
                 }
             }
         }
     }
     else
     {
         MemoryCacheKey key2 = new MemoryCacheKey(key);
         this.GetStore(key2).Set(key2, new MemoryCacheEntry(key, value, ObjectCache.InfiniteAbsoluteExpiration, ObjectCache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null, null, this));
         string[] keys = new string[] { key };
         ChangeMonitor item = this.CreateCacheEntryChangeMonitor(keys, null);
         if (changeMonitors == null)
         {
             changeMonitors = new Collection<ChangeMonitor>();
         }
         changeMonitors.Add(item);
         key2 = new MemoryCacheKey("OnUpdateSentinel" + key);
         this.GetStore(key2).Set(key2, new MemoryCacheEntry(key2.Key, new SentinelEntry(key, item, onUpdateCallback), absoluteExpiration, slidingExpiration, CacheItemPriority.NotRemovable, changeMonitors, s_sentinelRemovedCallback, this));
     }
 }
 internal MemoryCacheEntry Get(MemoryCacheKey key)
 {
     MemoryCacheEntry entryToRemove = this._entries[key] as MemoryCacheEntry;
     if ((entryToRemove != null) && (entryToRemove.UtcAbsExp <= DateTime.UtcNow))
     {
         this.Remove(key, entryToRemove, CacheEntryRemovedReason.Expired);
         entryToRemove = null;
     }
     this.UpdateExpAndUsage(entryToRemove);
     return entryToRemove;
 }