2013-03-23 04:01:52 +06:00
|
|
|
|
using System;
|
2013-12-16 12:51:02 +11:00
|
|
|
|
using System.Web;
|
2013-03-23 04:01:52 +06:00
|
|
|
|
using System.Web.Caching;
|
|
|
|
|
|
using Umbraco.Core.Logging;
|
2013-12-16 12:51:02 +11:00
|
|
|
|
using CacheItemPriority = System.Web.Caching.CacheItemPriority;
|
2013-03-23 04:01:52 +06:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Core.Cache
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// A CacheProvider that wraps the logic of the HttpRuntime.Cache
|
|
|
|
|
|
/// </summary>
|
2013-12-16 12:51:02 +11:00
|
|
|
|
internal class HttpRuntimeCacheProvider : DictionaryCacheProdiverBase, IRuntimeCacheProvider
|
2013-03-23 04:01:52 +06:00
|
|
|
|
{
|
|
|
|
|
|
private readonly System.Web.Caching.Cache _cache;
|
2013-12-16 12:51:02 +11:00
|
|
|
|
private readonly DictionaryCacheWrapper _wrapper;
|
2013-03-23 04:01:52 +06:00
|
|
|
|
private static readonly object Locker = new object();
|
2013-12-16 12:51:02 +11:00
|
|
|
|
|
2013-03-23 04:01:52 +06:00
|
|
|
|
public HttpRuntimeCacheProvider(System.Web.Caching.Cache cache)
|
|
|
|
|
|
{
|
|
|
|
|
|
_cache = cache;
|
2013-12-16 12:51:02 +11:00
|
|
|
|
_wrapper = new DictionaryCacheWrapper(_cache, s => _cache.Get(s.ToString()), o => _cache.Remove(o.ToString()));
|
2013-03-23 04:01:52 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-12-16 12:51:02 +11:00
|
|
|
|
protected override DictionaryCacheWrapper DictionaryCache
|
2013-09-18 10:05:44 +02:00
|
|
|
|
/// Clears all objects in the System.Web.Cache with the System.Type specified that satisfy the predicate
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public override void ClearCacheObjectTypes<T>(Func<string, T, bool> predicate)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
lock (Locker)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (DictionaryEntry c in _cache)
|
|
|
|
|
|
{
|
|
|
|
|
|
var key = c.Key.ToString();
|
|
|
|
|
|
if (_cache[key] != null
|
|
|
|
|
|
&& _cache[key] is T
|
|
|
|
|
|
&& predicate(key, (T)_cache[key]))
|
|
|
|
|
|
{
|
|
|
|
|
|
_cache.Remove(c.Key.ToString());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
|
|
|
|
|
LogHelper.Error<CacheHelper>("Cache clearing error", e);
|
2013-03-23 04:01:52 +06:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
{
|
2013-12-16 12:51:02 +11:00
|
|
|
|
get { return _wrapper; }
|
2013-03-23 04:01:52 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets (and adds if necessary) an item from the cache with all of the default parameters
|
|
|
|
|
|
/// </summary>
|
2013-12-16 12:51:02 +11:00
|
|
|
|
/// <typeparam name="T"></typeparam>
|
2013-03-23 04:01:52 +06:00
|
|
|
|
/// <param name="cacheKey"></param>
|
|
|
|
|
|
/// <param name="getCacheItem"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2013-12-16 12:51:02 +11:00
|
|
|
|
public override T GetCacheItem<T>(string cacheKey, Func<T> getCacheItem)
|
2013-03-23 04:01:52 +06:00
|
|
|
|
{
|
|
|
|
|
|
return GetCacheItem(cacheKey, CacheItemPriority.Normal, null, null, null, getCacheItem, Locker);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets (and adds if necessary) an item from the cache with the specified absolute expiration date (from NOW)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="TT"></typeparam>
|
|
|
|
|
|
/// <param name="cacheKey"></param>
|
|
|
|
|
|
/// <param name="timeout">This will set an absolute expiration from now until the timeout</param>
|
|
|
|
|
|
/// <param name="getCacheItem"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2013-12-16 12:51:02 +11:00
|
|
|
|
public virtual TT GetCacheItem<TT>(string cacheKey,
|
2013-04-03 23:39:51 +06:00
|
|
|
|
TimeSpan? timeout, Func<TT> getCacheItem)
|
2013-03-23 04:01:52 +06:00
|
|
|
|
{
|
|
|
|
|
|
return GetCacheItem(cacheKey, null, timeout, getCacheItem);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets (and adds if necessary) an item from the cache with the specified absolute expiration date (from NOW)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="TT"></typeparam>
|
|
|
|
|
|
/// <param name="cacheKey"></param>
|
|
|
|
|
|
/// <param name="refreshAction"></param>
|
|
|
|
|
|
/// <param name="timeout">This will set an absolute expiration from now until the timeout</param>
|
|
|
|
|
|
/// <param name="getCacheItem"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2013-12-16 12:51:02 +11:00
|
|
|
|
public virtual TT GetCacheItem<TT>(string cacheKey,
|
2013-04-03 23:39:51 +06:00
|
|
|
|
CacheItemRemovedCallback refreshAction, TimeSpan? timeout,
|
2013-03-23 04:01:52 +06:00
|
|
|
|
Func<TT> getCacheItem)
|
|
|
|
|
|
{
|
|
|
|
|
|
return GetCacheItem(cacheKey, CacheItemPriority.Normal, refreshAction, timeout, getCacheItem);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets (and adds if necessary) an item from the cache with the specified absolute expiration date (from NOW)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="TT"></typeparam>
|
|
|
|
|
|
/// <param name="cacheKey"></param>
|
|
|
|
|
|
/// <param name="priority"></param>
|
|
|
|
|
|
/// <param name="refreshAction"></param>
|
|
|
|
|
|
/// <param name="timeout">This will set an absolute expiration from now until the timeout</param>
|
|
|
|
|
|
/// <param name="getCacheItem"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2013-12-16 12:51:02 +11:00
|
|
|
|
public virtual TT GetCacheItem<TT>(string cacheKey,
|
2013-04-03 23:39:51 +06:00
|
|
|
|
CacheItemPriority priority, CacheItemRemovedCallback refreshAction, TimeSpan? timeout,
|
2013-03-23 04:01:52 +06:00
|
|
|
|
Func<TT> getCacheItem)
|
|
|
|
|
|
{
|
|
|
|
|
|
return GetCacheItem(cacheKey, priority, refreshAction, null, timeout, getCacheItem);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets (and adds if necessary) an item from the cache with the specified absolute expiration date (from NOW)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="TT"></typeparam>
|
|
|
|
|
|
/// <param name="cacheKey"></param>
|
|
|
|
|
|
/// <param name="priority"></param>
|
|
|
|
|
|
/// <param name="refreshAction"></param>
|
|
|
|
|
|
/// <param name="cacheDependency"></param>
|
|
|
|
|
|
/// <param name="timeout">This will set an absolute expiration from now until the timeout</param>
|
|
|
|
|
|
/// <param name="getCacheItem"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2013-12-16 12:51:02 +11:00
|
|
|
|
public virtual TT GetCacheItem<TT>(string cacheKey,
|
2013-03-23 04:01:52 +06:00
|
|
|
|
CacheItemPriority priority,
|
|
|
|
|
|
CacheItemRemovedCallback refreshAction,
|
|
|
|
|
|
CacheDependency cacheDependency,
|
2013-04-03 23:39:51 +06:00
|
|
|
|
TimeSpan? timeout,
|
2013-03-23 04:01:52 +06:00
|
|
|
|
Func<TT> getCacheItem)
|
|
|
|
|
|
{
|
|
|
|
|
|
return GetCacheItem(cacheKey, priority, refreshAction, cacheDependency, timeout, getCacheItem, Locker);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets (and adds if necessary) an item from the cache with the specified absolute expiration date (from NOW)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="TT"></typeparam>
|
|
|
|
|
|
/// <param name="cacheKey"></param>
|
|
|
|
|
|
/// <param name="priority"></param>
|
|
|
|
|
|
/// <param name="refreshAction"></param>
|
|
|
|
|
|
/// <param name="cacheDependency"></param>
|
|
|
|
|
|
/// <param name="timeout">This will set an absolute expiration from now until the timeout</param>
|
|
|
|
|
|
/// <param name="getCacheItem"></param>
|
|
|
|
|
|
/// <param name="syncLock"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
private TT GetCacheItem<TT>(string cacheKey,
|
|
|
|
|
|
CacheItemPriority priority, CacheItemRemovedCallback refreshAction,
|
|
|
|
|
|
CacheDependency cacheDependency, TimeSpan? timeout, Func<TT> getCacheItem, object syncLock)
|
|
|
|
|
|
{
|
2013-12-16 12:51:02 +11:00
|
|
|
|
cacheKey = GetCacheKey(cacheKey);
|
|
|
|
|
|
|
|
|
|
|
|
var result = DictionaryCache.Get(cacheKey);
|
2013-03-23 04:01:52 +06:00
|
|
|
|
if (result == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
lock (syncLock)
|
|
|
|
|
|
{
|
2013-12-16 12:51:02 +11:00
|
|
|
|
result = DictionaryCache.Get(cacheKey);
|
2013-03-23 04:01:52 +06:00
|
|
|
|
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<TT>().Result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Inserts an item into the cache, if it already exists in the cache it will be replaced
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="cacheKey"></param>
|
|
|
|
|
|
/// <param name="priority"></param>
|
2013-04-03 22:34:40 +06:00
|
|
|
|
/// <param name="getCacheItem"></param>
|
2013-12-16 12:51:02 +11:00
|
|
|
|
public virtual void InsertCacheItem<T>(string cacheKey,
|
2013-04-03 22:34:40 +06:00
|
|
|
|
CacheItemPriority priority,
|
|
|
|
|
|
Func<T> getCacheItem)
|
|
|
|
|
|
{
|
|
|
|
|
|
InsertCacheItem(cacheKey, priority, null, null, null, getCacheItem);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Inserts an item into the cache, if it already exists in the cache it will be replaced
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="cacheKey"></param>
|
|
|
|
|
|
/// <param name="priority"></param>
|
2013-03-23 04:01:52 +06:00
|
|
|
|
/// <param name="timeout">This will set an absolute expiration from now until the timeout</param>
|
|
|
|
|
|
/// <param name="getCacheItem"></param>
|
2013-12-16 12:51:02 +11:00
|
|
|
|
public virtual void InsertCacheItem<T>(string cacheKey,
|
2013-03-23 04:01:52 +06:00
|
|
|
|
CacheItemPriority priority,
|
2013-04-03 23:39:51 +06:00
|
|
|
|
TimeSpan? timeout,
|
2013-03-23 04:01:52 +06:00
|
|
|
|
Func<T> getCacheItem)
|
|
|
|
|
|
{
|
|
|
|
|
|
InsertCacheItem(cacheKey, priority, null, null, timeout, getCacheItem);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Inserts an item into the cache, if it already exists in the cache it will be replaced
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="cacheKey"></param>
|
|
|
|
|
|
/// <param name="priority"></param>
|
|
|
|
|
|
/// <param name="cacheDependency"></param>
|
|
|
|
|
|
/// <param name="timeout">This will set an absolute expiration from now until the timeout</param>
|
|
|
|
|
|
/// <param name="getCacheItem"></param>
|
2013-12-16 12:51:02 +11:00
|
|
|
|
public virtual void InsertCacheItem<T>(string cacheKey,
|
2013-03-23 04:01:52 +06:00
|
|
|
|
CacheItemPriority priority,
|
|
|
|
|
|
CacheDependency cacheDependency,
|
2013-04-03 23:39:51 +06:00
|
|
|
|
TimeSpan? timeout,
|
2013-03-23 04:01:52 +06:00
|
|
|
|
Func<T> getCacheItem)
|
|
|
|
|
|
{
|
|
|
|
|
|
InsertCacheItem(cacheKey, priority, null, cacheDependency, timeout, getCacheItem);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Inserts an item into the cache, if it already exists in the cache it will be replaced
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="cacheKey"></param>
|
|
|
|
|
|
/// <param name="priority"></param>
|
|
|
|
|
|
/// <param name="refreshAction"></param>
|
|
|
|
|
|
/// <param name="cacheDependency"></param>
|
|
|
|
|
|
/// <param name="timeout">This will set an absolute expiration from now until the timeout</param>
|
|
|
|
|
|
/// <param name="getCacheItem"></param>
|
2013-12-16 12:51:02 +11:00
|
|
|
|
public virtual void InsertCacheItem<T>(string cacheKey,
|
2013-03-23 04:01:52 +06:00
|
|
|
|
CacheItemPriority priority,
|
|
|
|
|
|
CacheItemRemovedCallback refreshAction,
|
|
|
|
|
|
CacheDependency cacheDependency,
|
|
|
|
|
|
TimeSpan? timeout,
|
|
|
|
|
|
Func<T> getCacheItem)
|
|
|
|
|
|
{
|
|
|
|
|
|
object result = getCacheItem();
|
|
|
|
|
|
if (result != null)
|
|
|
|
|
|
{
|
2013-12-16 12:51:02 +11:00
|
|
|
|
cacheKey = GetCacheKey(cacheKey);
|
|
|
|
|
|
|
2013-03-23 04:01:52 +06:00
|
|
|
|
//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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|