2013-03-23 04:01:52 +06:00
|
|
|
|
using System;
|
2013-12-16 13:19:10 +11:00
|
|
|
|
using System.Collections;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Threading;
|
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
|
|
|
|
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-12-16 13:19:10 +11:00
|
|
|
|
{
|
|
|
|
|
|
get { return _wrapper; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
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>
|
|
|
|
|
|
/// Gets (and adds if necessary) an item from the cache with all of the default parameters
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="cacheKey"></param>
|
|
|
|
|
|
/// <param name="getCacheItem"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2013-12-16 13:19:10 +11:00
|
|
|
|
public override object GetCacheItem(string cacheKey, Func<object> getCacheItem)
|
2013-03-23 04:01:52 +06:00
|
|
|
|
{
|
2013-12-16 13:19:10 +11:00
|
|
|
|
return GetCacheItem(cacheKey, getCacheItem, null, dependentFiles: null);
|
2013-03-23 04:01:52 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2013-12-16 13:19:10 +11:00
|
|
|
|
/// This overload is here for legacy purposes
|
2013-03-23 04:01:52 +06:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="cacheKey"></param>
|
|
|
|
|
|
/// <param name="getCacheItem"></param>
|
2013-12-16 13:19:10 +11:00
|
|
|
|
/// <param name="timeout"></param>
|
|
|
|
|
|
/// <param name="isSliding"></param>
|
2013-03-23 04:01:52 +06:00
|
|
|
|
/// <param name="priority"></param>
|
2013-12-16 13:19:10 +11:00
|
|
|
|
/// <param name="removedCallback"></param>
|
|
|
|
|
|
/// <param name="dependency"></param>
|
2013-03-23 04:01:52 +06:00
|
|
|
|
/// <returns></returns>
|
2013-12-16 13:19:10 +11:00
|
|
|
|
internal object GetCacheItem(string cacheKey, Func<object> getCacheItem, TimeSpan? timeout, bool isSliding = false, CacheItemPriority priority = CacheItemPriority.Normal, CacheItemRemovedCallback removedCallback = null, CacheDependency dependency = null)
|
2013-03-23 04:01:52 +06:00
|
|
|
|
{
|
2013-12-16 12:51:02 +11:00
|
|
|
|
cacheKey = GetCacheKey(cacheKey);
|
|
|
|
|
|
|
2013-12-16 13:19:10 +11:00
|
|
|
|
using (var lck = new UpgradeableReadLock(Locker))
|
2013-03-23 04:01:52 +06:00
|
|
|
|
{
|
2013-12-16 13:19:10 +11:00
|
|
|
|
var result = DictionaryCache.Get(cacheKey);
|
|
|
|
|
|
if (result == null)
|
2013-03-23 04:01:52 +06:00
|
|
|
|
{
|
2013-12-16 13:19:10 +11:00
|
|
|
|
lck.UpgradeToWriteLock();
|
|
|
|
|
|
|
|
|
|
|
|
result = getCacheItem();
|
|
|
|
|
|
if (result != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var absolute = isSliding ? System.Web.Caching.Cache.NoAbsoluteExpiration : (timeout == null ? System.Web.Caching.Cache.NoAbsoluteExpiration : DateTime.Now.Add(timeout.Value));
|
|
|
|
|
|
var sliding = isSliding == false ? System.Web.Caching.Cache.NoSlidingExpiration : (timeout ?? System.Web.Caching.Cache.NoSlidingExpiration);
|
|
|
|
|
|
|
|
|
|
|
|
_cache.Insert(cacheKey, result, dependency, absolute, sliding, priority, removedCallback);
|
2013-03-23 04:01:52 +06:00
|
|
|
|
}
|
2013-12-16 13:19:10 +11:00
|
|
|
|
|
2013-03-23 04:01:52 +06:00
|
|
|
|
}
|
2013-12-16 13:19:10 +11:00
|
|
|
|
return result;
|
2013-03-23 04:01:52 +06:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-12-16 13:19:10 +11:00
|
|
|
|
public object GetCacheItem(string cacheKey, Func<object> getCacheItem, TimeSpan? timeout, bool isSliding = false, CacheItemPriority priority = CacheItemPriority.Normal, CacheItemRemovedCallback removedCallback = null, string[] dependentFiles = null)
|
2013-04-03 22:34:40 +06:00
|
|
|
|
{
|
2013-12-16 13:19:10 +11:00
|
|
|
|
CacheDependency dependency = null;
|
|
|
|
|
|
if (dependentFiles != null && dependentFiles.Any())
|
|
|
|
|
|
{
|
|
|
|
|
|
dependency = new CacheDependency(dependentFiles);
|
|
|
|
|
|
}
|
|
|
|
|
|
return GetCacheItem(cacheKey, getCacheItem, timeout, isSliding, priority, removedCallback, dependency);
|
2013-04-03 22:34:40 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2013-12-16 13:19:10 +11:00
|
|
|
|
/// This overload is here for legacy purposes
|
2013-04-03 22:34:40 +06:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="cacheKey"></param>
|
2013-03-23 04:01:52 +06:00
|
|
|
|
/// <param name="getCacheItem"></param>
|
2013-12-16 13:19:10 +11:00
|
|
|
|
/// <param name="timeout"></param>
|
|
|
|
|
|
/// <param name="isSliding"></param>
|
2013-03-23 04:01:52 +06:00
|
|
|
|
/// <param name="priority"></param>
|
2013-12-16 13:19:10 +11:00
|
|
|
|
/// <param name="removedCallback"></param>
|
|
|
|
|
|
/// <param name="dependency"></param>
|
|
|
|
|
|
internal void InsertCacheItem(string cacheKey, Func<object> getCacheItem, TimeSpan? timeout = null, bool isSliding = false, CacheItemPriority priority = CacheItemPriority.Normal, CacheItemRemovedCallback removedCallback = null, CacheDependency dependency = null)
|
2013-03-23 04:01:52 +06:00
|
|
|
|
{
|
2013-12-16 13:19:10 +11:00
|
|
|
|
var result = getCacheItem();
|
|
|
|
|
|
if (result == null) return;
|
|
|
|
|
|
|
|
|
|
|
|
cacheKey = GetCacheKey(cacheKey);
|
|
|
|
|
|
|
|
|
|
|
|
var absolute = isSliding ? System.Web.Caching.Cache.NoAbsoluteExpiration : (timeout == null ? System.Web.Caching.Cache.NoAbsoluteExpiration : DateTime.Now.Add(timeout.Value));
|
|
|
|
|
|
var sliding = isSliding == false ? System.Web.Caching.Cache.NoSlidingExpiration : (timeout ?? System.Web.Caching.Cache.NoSlidingExpiration);
|
|
|
|
|
|
|
|
|
|
|
|
_cache.Insert(cacheKey, result, dependency, absolute, sliding, priority, removedCallback);
|
2013-03-23 04:01:52 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-12-16 13:19:10 +11:00
|
|
|
|
public void InsertCacheItem(string cacheKey, Func<object> getCacheItem, TimeSpan? timeout = null, bool isSliding = false, CacheItemPriority priority = CacheItemPriority.Normal, CacheItemRemovedCallback removedCallback = null, string[] dependentFiles = null)
|
2013-03-23 04:01:52 +06:00
|
|
|
|
{
|
2013-12-16 13:19:10 +11:00
|
|
|
|
CacheDependency dependency = null;
|
|
|
|
|
|
if (dependentFiles != null && dependentFiles.Any())
|
2013-03-23 04:01:52 +06:00
|
|
|
|
{
|
2013-12-16 13:19:10 +11:00
|
|
|
|
dependency = new CacheDependency(dependentFiles);
|
2013-03-23 04:01:52 +06:00
|
|
|
|
}
|
2013-12-16 13:19:10 +11:00
|
|
|
|
InsertCacheItem(cacheKey, getCacheItem, timeout, isSliding, priority, removedCallback, dependency);
|
2013-03-23 04:01:52 +06:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|