2013-05-13 19:31:27 -10:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
using System.Web.Caching;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Core.Cache
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// A cache provider that statically caches everything in an in memory dictionary
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
internal class StaticCacheProvider : CacheProviderBase
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly ConcurrentDictionary<string, object> _staticCache = new ConcurrentDictionary<string, object>();
|
|
|
|
|
|
|
|
|
|
|
|
public override void ClearAllCache()
|
|
|
|
|
|
{
|
|
|
|
|
|
_staticCache.Clear();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void ClearCacheItem(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
object val;
|
|
|
|
|
|
_staticCache.TryRemove(key, out val);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void ClearCacheObjectTypes(string typeName)
|
|
|
|
|
|
{
|
2013-09-18 11:47:37 +02:00
|
|
|
|
_staticCache.RemoveAll(kvp => kvp.Value != null && kvp.Value.GetType().ToString().InvariantEquals(typeName));
|
2013-05-13 19:31:27 -10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void ClearCacheObjectTypes<T>()
|
|
|
|
|
|
{
|
2013-09-18 11:33:26 +02:00
|
|
|
|
var typeOfT = typeof (T);
|
2013-09-18 11:47:37 +02:00
|
|
|
|
_staticCache.RemoveAll(kvp => kvp.Value != null && kvp.Value.GetType() == typeOfT);
|
2013-09-18 10:05:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void ClearCacheObjectTypes<T>(Func<string, T, bool> predicate)
|
|
|
|
|
|
{
|
2013-09-18 11:33:26 +02:00
|
|
|
|
var typeOfT = typeof(T);
|
2013-09-18 11:47:37 +02:00
|
|
|
|
_staticCache.RemoveAll(kvp => kvp.Value != null && kvp.Value.GetType() == typeOfT && predicate(kvp.Key, (T)kvp.Value));
|
2013-05-13 19:31:27 -10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void ClearCacheByKeySearch(string keyStartsWith)
|
|
|
|
|
|
{
|
2013-09-18 10:05:44 +02:00
|
|
|
|
_staticCache.RemoveAll(kvp => kvp.Key.InvariantStartsWith(keyStartsWith));
|
2013-05-13 19:31:27 -10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override void ClearCacheByKeyExpression(string regexString)
|
|
|
|
|
|
{
|
2013-09-18 10:05:44 +02:00
|
|
|
|
_staticCache.RemoveAll(kvp => Regex.IsMatch(kvp.Key, regexString));
|
2013-05-13 19:31:27 -10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override IEnumerable<T> GetCacheItemsByKeySearch<T>(string keyStartsWith)
|
|
|
|
|
|
{
|
|
|
|
|
|
return (from KeyValuePair<string, object> c in _staticCache
|
|
|
|
|
|
where c.Key.InvariantStartsWith(keyStartsWith)
|
|
|
|
|
|
select c.Value.TryConvertTo<T>()
|
|
|
|
|
|
into attempt
|
|
|
|
|
|
where attempt.Success
|
|
|
|
|
|
select attempt.Result).ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override T GetCacheItem<T>(string cacheKey)
|
|
|
|
|
|
{
|
|
|
|
|
|
var result = _staticCache[cacheKey];
|
|
|
|
|
|
if (result == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return default(T);
|
|
|
|
|
|
}
|
|
|
|
|
|
return result.TryConvertTo<T>().Result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override T GetCacheItem<T>(string cacheKey, Func<T> getCacheItem)
|
|
|
|
|
|
{
|
2013-09-18 10:46:34 +02:00
|
|
|
|
return (T)_staticCache.GetOrAdd(cacheKey, key => getCacheItem());
|
2013-05-13 19:31:27 -10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|