using System;
using System.Web;
using System.Web.Caching;
using Umbraco.Core.Logging;
using CacheItemPriority = System.Web.Caching.CacheItemPriority;
namespace Umbraco.Core.Cache
{
///
/// A CacheProvider that wraps the logic of the HttpRuntime.Cache
///
internal class HttpRuntimeCacheProvider : DictionaryCacheProdiverBase, IRuntimeCacheProvider
{
private readonly System.Web.Caching.Cache _cache;
private readonly DictionaryCacheWrapper _wrapper;
private static readonly object Locker = new object();
public HttpRuntimeCacheProvider(System.Web.Caching.Cache cache)
{
_cache = cache;
_wrapper = new DictionaryCacheWrapper(_cache, s => _cache.Get(s.ToString()), o => _cache.Remove(o.ToString()));
}
protected override DictionaryCacheWrapper DictionaryCache
{
get { return _wrapper; }
}
///
/// Gets (and adds if necessary) an item from the cache with all of the default parameters
///
///
///
///
public override object GetCacheItem(string cacheKey, Func getCacheItem)
{
return GetCacheItem(cacheKey, CacheItemPriority.Normal, null, null, null, getCacheItem, Locker);
}
///
/// Gets (and adds if necessary) an item from the cache with the specified absolute expiration date (from NOW)
///
///
///
/// This will set an absolute expiration from now until the timeout
///
///
public virtual TT GetCacheItem(string cacheKey,
TimeSpan? timeout, Func getCacheItem)
{
return GetCacheItem(cacheKey, null, timeout, getCacheItem);
}
///
/// Gets (and adds if necessary) an item from the cache with the specified absolute expiration date (from NOW)
///
///
///
///
/// This will set an absolute expiration from now until the timeout
///
///
public virtual TT GetCacheItem(string cacheKey,
CacheItemRemovedCallback refreshAction, TimeSpan? timeout,
Func getCacheItem)
{
return GetCacheItem(cacheKey, CacheItemPriority.Normal, refreshAction, timeout, getCacheItem);
}
///
/// Gets (and adds if necessary) an item from the cache with the specified absolute expiration date (from NOW)
///
///
///
///
///
/// This will set an absolute expiration from now until the timeout
///
///
public virtual TT GetCacheItem(string cacheKey,
CacheItemPriority priority, CacheItemRemovedCallback refreshAction, TimeSpan? timeout,
Func getCacheItem)
{
return GetCacheItem(cacheKey, priority, refreshAction, null, timeout, getCacheItem);
}
///
/// Gets (and adds if necessary) an item from the cache with the specified absolute expiration date (from NOW)
///
///
///
///
///
///
/// This will set an absolute expiration from now until the timeout
///
///
public virtual TT GetCacheItem(string cacheKey,
CacheItemPriority priority,
CacheItemRemovedCallback refreshAction,
CacheDependency cacheDependency,
TimeSpan? timeout,
Func getCacheItem)
{
return GetCacheItem(cacheKey, priority, refreshAction, cacheDependency, timeout, getCacheItem, Locker);
}
///
/// Gets (and adds if necessary) an item from the cache with the specified absolute expiration date (from NOW)
///
///
///
///
///
///
/// This will set an absolute expiration from now until the timeout
///
///
///
private TT GetCacheItem(string cacheKey,
CacheItemPriority priority, CacheItemRemovedCallback refreshAction,
CacheDependency cacheDependency, TimeSpan? timeout, Func getCacheItem, object syncLock)
{
cacheKey = GetCacheKey(cacheKey);
var result = DictionaryCache.Get(cacheKey);
if (result == null)
{
lock (syncLock)
{
result = DictionaryCache.Get(cacheKey);
if (result == null)
{
result = getCacheItem();
if (result != null)
{
//we use Insert instead of add if for some crazy reason there is now a cache with the cache key in there, it will just overwrite it.
_cache.Insert(cacheKey, result, cacheDependency,
timeout == null ? System.Web.Caching.Cache.NoAbsoluteExpiration : DateTime.Now.Add(timeout.Value),
TimeSpan.Zero, priority, refreshAction);
}
}
}
}
return result.TryConvertTo().Result;
}
///
/// Inserts an item into the cache, if it already exists in the cache it will be replaced
///
///
///
///
///
public virtual void InsertCacheItem(string cacheKey,
CacheItemPriority priority,
Func getCacheItem)
{
InsertCacheItem(cacheKey, priority, null, null, null, getCacheItem);
}
///
/// Inserts an item into the cache, if it already exists in the cache it will be replaced
///
///
///
///
/// This will set an absolute expiration from now until the timeout
///
public virtual void InsertCacheItem(string cacheKey,
CacheItemPriority priority,
TimeSpan? timeout,
Func getCacheItem)
{
InsertCacheItem(cacheKey, priority, null, null, timeout, getCacheItem);
}
///
/// Inserts an item into the cache, if it already exists in the cache it will be replaced
///
///
///
///
///
/// This will set an absolute expiration from now until the timeout
///
public virtual void InsertCacheItem(string cacheKey,
CacheItemPriority priority,
CacheDependency cacheDependency,
TimeSpan? timeout,
Func getCacheItem)
{
InsertCacheItem(cacheKey, priority, null, cacheDependency, timeout, getCacheItem);
}
///
/// Inserts an item into the cache, if it already exists in the cache it will be replaced
///
///
///
///
///
///
/// This will set an absolute expiration from now until the timeout
///
public virtual void InsertCacheItem(string cacheKey,
CacheItemPriority priority,
CacheItemRemovedCallback refreshAction,
CacheDependency cacheDependency,
TimeSpan? timeout,
Func getCacheItem)
{
object result = getCacheItem();
if (result != null)
{
cacheKey = GetCacheKey(cacheKey);
//we use Insert instead of add if for some crazy reason there is now a cache with the cache key in there, it will just overwrite it.
_cache.Insert(cacheKey, result, cacheDependency,
timeout == null ? System.Web.Caching.Cache.NoAbsoluteExpiration : DateTime.Now.Add(timeout.Value),
TimeSpan.Zero, priority, refreshAction);
}
}
}
}