コード例 #1
0
ファイル: MemoryCache.cs プロジェクト: pedrobsaila/runtime
        public override void Set(string key, object value, CacheItemPolicy policy, string regionName = null)
        {
            if (regionName != null)
            {
                throw new NotSupportedException(SR.RegionName_not_supported);
            }
            if (key == null)
            {
                throw new ArgumentNullException(nameof(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();
                        }
                    }
                }

                IsDisposedOrThrow();

                return;
            }
            MemoryCacheKey   cacheKey = new MemoryCacheKey(key);
            MemoryCacheStore store    = GetStore(cacheKey);

            store.Set(cacheKey, new MemoryCacheEntry(key, value, absExp, slidingExp, priority, changeMonitors, removedCallback, this));
        }
コード例 #2
0
        internal void Set(string key,
                          object value,
                          Collection <ChangeMonitor> changeMonitors,
                          DateTimeOffset absoluteExpiration,
                          TimeSpan slidingExpiration,
                          CacheEntryUpdateCallback onUpdateCallback)
        {
            if (key == null)
            {
                throw new ArgumentNullException(nameof(key));
            }
            if (changeMonitors == null &&
                absoluteExpiration == ObjectCache.InfiniteAbsoluteExpiration &&
                slidingExpiration == ObjectCache.NoSlidingExpiration)
            {
                throw new ArgumentException(SR.Invalid_argument_combination);
            }
            if (onUpdateCallback == null)
            {
                throw new ArgumentNullException(nameof(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);
        }