2013-03-23 04:01:52 +06:00
|
|
|
|
using System;
|
2013-09-25 19:23:41 +10:00
|
|
|
|
using System.Collections;
|
2014-05-22 09:36:08 +02:00
|
|
|
|
using System.Collections.Generic;
|
2013-08-09 11:16:10 +10:00
|
|
|
|
using System.Linq;
|
2014-05-22 09:36:08 +02:00
|
|
|
|
using System.Text.RegularExpressions;
|
2013-08-09 11:16:10 +10:00
|
|
|
|
using System.Threading;
|
2013-08-08 19:46:58 +10:00
|
|
|
|
using System.Web;
|
2013-03-23 04:01:52 +06:00
|
|
|
|
using System.Web.Caching;
|
|
|
|
|
|
using Umbraco.Core.Logging;
|
2013-08-08 19:46:58 +10: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 16:02:34 +11:00
|
|
|
|
internal class HttpRuntimeCacheProvider : DictionaryCacheProviderBase, IRuntimeCacheProvider
|
2013-03-23 04:01:52 +06:00
|
|
|
|
{
|
|
|
|
|
|
private readonly System.Web.Caching.Cache _cache;
|
2013-08-08 19:46:58 +10:00
|
|
|
|
private readonly DictionaryCacheWrapper _wrapper;
|
|
|
|
|
|
|
2013-03-23 04:01:52 +06:00
|
|
|
|
public HttpRuntimeCacheProvider(System.Web.Caching.Cache cache)
|
|
|
|
|
|
{
|
|
|
|
|
|
_cache = cache;
|
2013-08-08 19:46:58 +10:00
|
|
|
|
_wrapper = new DictionaryCacheWrapper(_cache, s => _cache.Get(s.ToString()), o => _cache.Remove(o.ToString()));
|
2013-03-23 04:01:52 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-08-08 19:46:58 +10:00
|
|
|
|
protected override DictionaryCacheWrapper DictionaryCache
|
2013-03-23 04:01:52 +06:00
|
|
|
|
{
|
2013-08-08 19:46:58 +10:00
|
|
|
|
get { return _wrapper; }
|
2013-03-23 04:01:52 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-05-23 10:44:58 +02:00
|
|
|
|
// don't use, _cache enumerates DictionaryEntry items
|
|
|
|
|
|
//
|
|
|
|
|
|
//private IEnumerable<KeyValuePair<string, object>> EnumerateDictionaryCache()
|
|
|
|
|
|
//{
|
|
|
|
|
|
// // DictionaryCache just wraps _cache which has a special enumerator
|
|
|
|
|
|
// var enumerator = _cache.GetEnumerator();
|
|
|
|
|
|
// while (enumerator.MoveNext())
|
|
|
|
|
|
// {
|
|
|
|
|
|
// var key = enumerator.Key as string;
|
|
|
|
|
|
// if (key == null) continue;
|
|
|
|
|
|
// yield return new KeyValuePair<string, object>(key, enumerator.Value);
|
|
|
|
|
|
// }
|
|
|
|
|
|
//}
|
2014-05-22 09:36:08 +02:00
|
|
|
|
|
|
|
|
|
|
public override void ClearAllCache()
|
|
|
|
|
|
{
|
|
|
|
|
|
using (new WriteLock(Locker))
|
|
|
|
|
|
{
|
2014-05-23 10:44:58 +02:00
|
|
|
|
// remove null values as well!
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var entry in _cache.Cast<DictionaryEntry>()
|
|
|
|
|
|
.Where(x => x.Key is string
|
|
|
|
|
|
&& ((string)x.Key).StartsWith(CacheItemPrefix) /* && x.Value != null */))
|
|
|
|
|
|
_cache.Remove((string)entry.Key);
|
2014-05-22 09:36:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void ClearCacheItem(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
using (new WriteLock(Locker))
|
|
|
|
|
|
{
|
|
|
|
|
|
var cacheKey = GetCacheKey(key);
|
|
|
|
|
|
_cache.Remove(cacheKey);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void ClearCacheObjectTypes(string typeName)
|
|
|
|
|
|
{
|
|
|
|
|
|
var typeName2 = typeName;
|
|
|
|
|
|
using (new WriteLock(Locker))
|
|
|
|
|
|
{
|
2014-05-23 10:44:58 +02:00
|
|
|
|
foreach (var entry in _cache.Cast<DictionaryEntry>()
|
|
|
|
|
|
.Where(x => x.Key is string
|
|
|
|
|
|
&& ((string)x.Key).StartsWith(CacheItemPrefix)
|
2014-05-22 09:36:08 +02:00
|
|
|
|
&& x.Value != null
|
|
|
|
|
|
&& x.Value.GetType().ToString().InvariantEquals(typeName2)))
|
2014-05-23 10:44:58 +02:00
|
|
|
|
_cache.Remove((string)entry.Key);
|
2014-05-22 09:36:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void ClearCacheObjectTypes<T>()
|
|
|
|
|
|
{
|
2014-05-23 10:44:58 +02:00
|
|
|
|
// note: compare on exact type, don't use "is"
|
2014-05-22 09:36:08 +02:00
|
|
|
|
|
2014-05-23 10:44:58 +02:00
|
|
|
|
var typeOfT = typeof(T);
|
2014-05-22 09:36:08 +02:00
|
|
|
|
using (new WriteLock(Locker))
|
|
|
|
|
|
{
|
2014-05-23 10:44:58 +02:00
|
|
|
|
foreach (var entry in _cache.Cast<DictionaryEntry>()
|
|
|
|
|
|
.Where(x => x.Key is string
|
|
|
|
|
|
&& ((string)x.Key).StartsWith(CacheItemPrefix)
|
|
|
|
|
|
&& x.Value != null
|
|
|
|
|
|
&& x.Value.GetType() == typeOfT))
|
|
|
|
|
|
//&& x.Value is T))
|
|
|
|
|
|
_cache.Remove((string)entry.Key);
|
2014-05-22 09:36:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-09-18 10:05:44 +02:00
|
|
|
|
public override void ClearCacheObjectTypes<T>(Func<string, T, bool> predicate)
|
|
|
|
|
|
{
|
2014-05-23 10:44:58 +02:00
|
|
|
|
// note: compare on exact type, don't use "is"
|
2014-05-22 09:36:08 +02:00
|
|
|
|
|
2014-05-23 10:44:58 +02:00
|
|
|
|
var typeOfT = typeof(T);
|
2013-09-18 10:05:44 +02:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2014-05-22 09:36:08 +02:00
|
|
|
|
using (new WriteLock(Locker))
|
2013-09-18 10:05:44 +02:00
|
|
|
|
{
|
2014-05-23 10:44:58 +02:00
|
|
|
|
foreach (var entry in _cache.Cast<DictionaryEntry>()
|
|
|
|
|
|
.Where(x => x.Key is string
|
|
|
|
|
|
&& ((string)x.Key).StartsWith(CacheItemPrefix)
|
|
|
|
|
|
&& x.Value.GetType() == typeOfT // false if x.Value is null
|
|
|
|
|
|
//&& x.Value is T
|
|
|
|
|
|
&& predicate((string)x.Key, (T) x.Value)))
|
|
|
|
|
|
_cache.Remove((string)entry.Key);
|
2013-09-18 10:05:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
|
{
|
2014-05-22 09:36:08 +02:00
|
|
|
|
// oops, what is this?!
|
2013-09-18 10:05:44 +02:00
|
|
|
|
LogHelper.Error<CacheHelper>("Cache clearing error", e);
|
2013-03-23 04:01:52 +06:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-05-22 09:36:08 +02:00
|
|
|
|
public override void ClearCacheByKeySearch(string keyStartsWith)
|
|
|
|
|
|
{
|
|
|
|
|
|
using (new WriteLock(Locker))
|
|
|
|
|
|
{
|
2014-05-23 10:44:58 +02:00
|
|
|
|
foreach (var entry in _cache.Cast<DictionaryEntry>()
|
|
|
|
|
|
.Where(x => x.Key is string
|
|
|
|
|
|
&& ((string)x.Key).InvariantStartsWith(string.Format("{0}-{1}", CacheItemPrefix, keyStartsWith))))
|
|
|
|
|
|
_cache.Remove((string)entry.Key);
|
2014-05-22 09:36:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void ClearCacheByKeyExpression(string regexString)
|
|
|
|
|
|
{
|
|
|
|
|
|
var plen = CacheItemPrefix.Length + 1; // string.Format("{0}-", CacheItemPrefix)
|
|
|
|
|
|
using (new WriteLock(Locker))
|
|
|
|
|
|
{
|
2014-05-23 10:44:58 +02:00
|
|
|
|
foreach (var entry in _cache.Cast<DictionaryEntry>()
|
|
|
|
|
|
.Where(x => x.Key is string
|
|
|
|
|
|
&& ((string)x.Key).StartsWith(CacheItemPrefix)
|
|
|
|
|
|
&& Regex.IsMatch(((string)x.Key).Substring(plen), regexString)))
|
|
|
|
|
|
_cache.Remove((string)entry.Key);
|
2014-05-22 09:36:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override IEnumerable<object> GetCacheItemsByKeySearch(string keyStartsWith)
|
|
|
|
|
|
{
|
2014-05-23 10:44:58 +02:00
|
|
|
|
using (new ReadLock(Locker))
|
|
|
|
|
|
{
|
|
|
|
|
|
return _cache.Cast<DictionaryEntry>()
|
|
|
|
|
|
.Where(x => x.Key is string
|
|
|
|
|
|
&& ((string)x.Key).InvariantStartsWith(string.Format("{0}-{1}", CacheItemPrefix, keyStartsWith)))
|
|
|
|
|
|
.Select(x => ((Lazy<object>)x.Value).Value)
|
|
|
|
|
|
.Where(x => x != null) // backward compat, don't store null values in the cache
|
|
|
|
|
|
.ToList();
|
|
|
|
|
|
}
|
2014-05-22 09:36:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override IEnumerable<object> GetCacheItemsByKeyExpression(string regexString)
|
|
|
|
|
|
{
|
|
|
|
|
|
var plen = CacheItemPrefix.Length + 1; // string.Format("{0}-", CacheItemPrefix)
|
2014-05-23 10:44:58 +02:00
|
|
|
|
using (new ReadLock(Locker))
|
|
|
|
|
|
{
|
|
|
|
|
|
return _cache.Cast<DictionaryEntry>()
|
|
|
|
|
|
.Where(x => x.Key is string
|
|
|
|
|
|
&& ((string) x.Key).StartsWith(CacheItemPrefix)
|
|
|
|
|
|
&& Regex.IsMatch(((string) x.Key).Substring(plen), regexString))
|
|
|
|
|
|
.Select(x => ((Lazy<object>) x.Value).Value)
|
|
|
|
|
|
.Where(x => x != null) // backward compat, don't store null values in the cache
|
|
|
|
|
|
.ToList();
|
|
|
|
|
|
}
|
2014-05-22 09:36:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2013-03-23 04:01:52 +06:00
|
|
|
|
/// 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-08-08 20:25:26 +10:00
|
|
|
|
public override object GetCacheItem(string cacheKey, Func<object> getCacheItem)
|
2013-03-23 04:01:52 +06:00
|
|
|
|
{
|
2013-08-09 11:16:10 +10:00
|
|
|
|
return GetCacheItem(cacheKey, getCacheItem, null, dependentFiles: null);
|
2013-03-23 04:01:52 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2013-08-09 11:16:10 +10: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-08-09 11:16:10 +10:00
|
|
|
|
/// <param name="timeout"></param>
|
|
|
|
|
|
/// <param name="isSliding"></param>
|
2013-03-23 04:01:52 +06:00
|
|
|
|
/// <param name="priority"></param>
|
2013-08-09 11:16:10 +10:00
|
|
|
|
/// <param name="removedCallback"></param>
|
|
|
|
|
|
/// <param name="dependency"></param>
|
2013-03-23 04:01:52 +06:00
|
|
|
|
/// <returns></returns>
|
2013-08-09 11:16:10 +10: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-08-08 19:46:58 +10:00
|
|
|
|
cacheKey = GetCacheKey(cacheKey);
|
|
|
|
|
|
|
2014-05-16 16:48:31 +02:00
|
|
|
|
// NOTE - because we don't know what getCacheItem does, how long it will take and whether it will hang,
|
|
|
|
|
|
// getCacheItem should run OUTSIDE of the global application lock else we run into lock contention and
|
|
|
|
|
|
// nasty performance issues.
|
|
|
|
|
|
|
|
|
|
|
|
// So.... we insert a Lazy<object> in the cache while holding the global application lock, and then rely
|
|
|
|
|
|
// on the Lazy lock to ensure that getCacheItem runs once and everybody waits on it, while the global
|
|
|
|
|
|
// application lock has been released.
|
|
|
|
|
|
|
2014-05-23 10:44:58 +02:00
|
|
|
|
// Note that the Lazy execution may produce a null value.
|
|
|
|
|
|
// Must make sure (for backward compatibility) that we pretend they are not in the cache.
|
|
|
|
|
|
// So if we find an entry in the cache that already has its value created and is null,
|
|
|
|
|
|
// pretend it was not there. If value is not already created, wait... and return null, that's
|
|
|
|
|
|
// what prior code did.
|
|
|
|
|
|
|
|
|
|
|
|
// So... the null value _will_ be in the cache but never returned
|
2014-05-16 16:48:31 +02:00
|
|
|
|
|
|
|
|
|
|
Lazy<object> result;
|
|
|
|
|
|
|
2013-08-09 11:16:10 +10:00
|
|
|
|
using (var lck = new UpgradeableReadLock(Locker))
|
2013-03-23 04:01:52 +06:00
|
|
|
|
{
|
2014-05-23 10:44:58 +02:00
|
|
|
|
result = _cache.Get(cacheKey) as Lazy<object>; // null if key not found
|
|
|
|
|
|
if (result == null || (result.IsValueCreated && result.Value == null))
|
2013-03-23 04:01:52 +06:00
|
|
|
|
{
|
2013-08-09 11:16:10 +10:00
|
|
|
|
lck.UpgradeToWriteLock();
|
|
|
|
|
|
|
2014-05-16 16:48:31 +02:00
|
|
|
|
result = new Lazy<object>(getCacheItem);
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|
2014-05-16 16:48:31 +02:00
|
|
|
|
|
|
|
|
|
|
return result.Value;
|
2013-03-23 04:01:52 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-08-09 11:16:10 +10: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-08-09 11:16:10 +10: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-08-09 11:16:10 +10: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-08-09 11:16:10 +10:00
|
|
|
|
/// <param name="timeout"></param>
|
|
|
|
|
|
/// <param name="isSliding"></param>
|
2013-03-23 04:01:52 +06:00
|
|
|
|
/// <param name="priority"></param>
|
2013-08-09 11:16:10 +10: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
|
|
|
|
{
|
2014-05-16 16:48:31 +02:00
|
|
|
|
// NOTE - here also we must insert a Lazy<object> but we can evaluate it right now
|
2014-05-23 10:44:58 +02:00
|
|
|
|
// and make sure we don't store a null value.
|
2014-05-16 16:48:31 +02:00
|
|
|
|
|
|
|
|
|
|
var result = new Lazy<object>(getCacheItem);
|
2014-05-22 09:36:08 +02:00
|
|
|
|
var value = result.Value; // force evaluation now
|
2014-05-23 10:44:58 +02:00
|
|
|
|
if (value == null) return; // do not store null values (backward compat)
|
2013-08-09 11:16:10 +10:00
|
|
|
|
|
|
|
|
|
|
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-08-09 11:16:10 +10: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-08-09 11:16:10 +10:00
|
|
|
|
CacheDependency dependency = null;
|
|
|
|
|
|
if (dependentFiles != null && dependentFiles.Any())
|
2013-03-23 04:01:52 +06:00
|
|
|
|
{
|
2013-08-09 11:16:10 +10:00
|
|
|
|
dependency = new CacheDependency(dependentFiles);
|
2013-03-23 04:01:52 +06:00
|
|
|
|
}
|
2013-08-09 11:16:10 +10:00
|
|
|
|
InsertCacheItem(cacheKey, getCacheItem, timeout, isSliding, priority, removedCallback, dependency);
|
2013-03-23 04:01:52 +06:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|