public override Object AddOrGetExisting(String key, Object value, CacheItemPolicy policy, String regionName = null)
		{
			if (value == null)
			{
				this.Delete(key);
				return (null);
			}
			else
			{
				var item = this.Get(key);

				if (item == null)
				{
					CacheItemRemovedCallback callback = (k, v, r) => policy.RemovedCallback(new CacheEntryRemovedArguments(this, (CacheEntryRemovedReason)(Int32)r, new CacheItem(k, v, regionName)));
					this.Add(key, value, policy.AbsoluteExpiration.LocalDateTime, policy.SlidingExpiration, (System.Web.Caching.CacheItemPriority)(Int32)policy.Priority, callback);
				}

				return (item);
			}
		}
Example #2
0
        private object AddOrInsert(
            Func<string, object, CacheDependency, DateTime, TimeSpan, Web.Caching.CacheItemPriority, CacheItemRemovedCallback, object> action,
            string key, object value, CacheItemPolicy policy, string regionName = null
            )
        {
            var fileNames = new List<string>();
            foreach (var monitor in policy.ChangeMonitors) {
                var fileMonitor = monitor as FileChangeMonitor;
                if (fileMonitor == null)
                    throw new NotImplementedException();

                fileNames.AddRange(fileMonitor.FilePaths);
            }

            var dependency = fileNames.Count > 0
                           ? new CacheDependency(fileNames.ToArray())
                           : null;

            var callback = (CacheItemRemovedCallback)null;
            if (policy.RemovedCallback != null) {
                callback = (removedKey, removedValue, reason) => policy.RemovedCallback(
                    new CacheEntryRemovedArguments(this, reasonMap[reason], new CacheItem(removedKey, removedValue))
                );
            }

            return action(
                key, value, dependency,
                policy.AbsoluteExpiration != ObjectCache.InfiniteAbsoluteExpiration ? policy.AbsoluteExpiration.UtcDateTime : Cache.NoAbsoluteExpiration,
                policy.SlidingExpiration != ObjectCache.NoSlidingExpiration ? policy.SlidingExpiration : Cache.NoSlidingExpiration,
                policy.Priority == Runtime.Caching.CacheItemPriority.NotRemovable
                    ? Web.Caching.CacheItemPriority.NotRemovable
                    : Web.Caching.CacheItemPriority.Default,
                callback
            );
        }