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>
|
2013-12-16 12:51:02 +11:00
|
|
|
|
internal class StaticCacheProvider : ICacheProvider
|
2013-05-13 19:31:27 -10:00
|
|
|
|
{
|
2013-12-16 12:51:02 +11:00
|
|
|
|
internal readonly ConcurrentDictionary<string, object> StaticCache = new ConcurrentDictionary<string, object>();
|
2013-05-13 19:31:27 -10:00
|
|
|
|
|
2013-12-16 12:51:02 +11:00
|
|
|
|
public virtual void ClearAllCache()
|
2013-05-13 19:31:27 -10:00
|
|
|
|
{
|
2013-12-16 12:51:02 +11:00
|
|
|
|
StaticCache.Clear();
|
2013-05-13 19:31:27 -10:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-12-16 12:51:02 +11:00
|
|
|
|
public virtual void ClearCacheItem(string key)
|
2013-05-13 19:31:27 -10:00
|
|
|
|
{
|
|
|
|
|
|
object val;
|
2013-12-16 12:51:02 +11:00
|
|
|
|
StaticCache.TryRemove(key, out val);
|
2013-05-13 19:31:27 -10:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-12-16 12:51:02 +11:00
|
|
|
|
public virtual void ClearCacheObjectTypes(string typeName)
|
2013-05-13 19:31:27 -10:00
|
|
|
|
{
|
2013-12-16 12:51:02 +11:00
|
|
|
|
foreach (var key in StaticCache.Keys)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (StaticCache[key] != null
|
|
|
|
|
|
&& StaticCache[key].GetType().ToString().InvariantEquals(typeName))
|
|
|
|
|
|
{
|
|
|
|
|
|
object val;
|
|
|
|
|
|
StaticCache.TryRemove(key, out val);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2013-09-18 10:05:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-12-16 12:51:02 +11:00
|
|
|
|
public virtual void ClearCacheObjectTypes<T>()
|
2013-09-18 10:05:44 +02:00
|
|
|
|
{
|
2013-12-16 12:51:02 +11:00
|
|
|
|
foreach (var key in StaticCache.Keys)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (StaticCache[key] != null
|
|
|
|
|
|
&& StaticCache[key].GetType() == typeof(T))
|
|
|
|
|
|
{
|
|
|
|
|
|
object val;
|
|
|
|
|
|
StaticCache.TryRemove(key, out val);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2013-05-13 19:31:27 -10:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-12-16 12:51:02 +11:00
|
|
|
|
public virtual void ClearCacheByKeySearch(string keyStartsWith)
|
2013-05-13 19:31:27 -10:00
|
|
|
|
{
|
2013-12-16 12:51:02 +11:00
|
|
|
|
foreach (var key in StaticCache.Keys)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (key.InvariantStartsWith(keyStartsWith))
|
|
|
|
|
|
{
|
|
|
|
|
|
ClearCacheItem(key);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2013-05-13 19:31:27 -10:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-12-16 12:51:02 +11:00
|
|
|
|
public virtual void ClearCacheByKeyExpression(string regexString)
|
2013-05-13 19:31:27 -10:00
|
|
|
|
{
|
2013-12-16 12:51:02 +11:00
|
|
|
|
foreach (var key in StaticCache.Keys)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Regex.IsMatch(key, regexString))
|
|
|
|
|
|
{
|
|
|
|
|
|
ClearCacheItem(key);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2013-05-13 19:31:27 -10:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-12-16 12:51:02 +11:00
|
|
|
|
public virtual IEnumerable<T> GetCacheItemsByKeySearch<T>(string keyStartsWith)
|
2013-05-13 19:31:27 -10:00
|
|
|
|
{
|
2013-12-16 12:51:02 +11:00
|
|
|
|
return (from KeyValuePair<string, object> c in StaticCache
|
2013-05-13 19:31:27 -10:00
|
|
|
|
where c.Key.InvariantStartsWith(keyStartsWith)
|
|
|
|
|
|
select c.Value.TryConvertTo<T>()
|
|
|
|
|
|
into attempt
|
|
|
|
|
|
where attempt.Success
|
|
|
|
|
|
select attempt.Result).ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-12-16 12:51:02 +11:00
|
|
|
|
public virtual T GetCacheItem<T>(string cacheKey)
|
2013-05-13 19:31:27 -10:00
|
|
|
|
{
|
2013-12-16 12:51:02 +11:00
|
|
|
|
var result = StaticCache[cacheKey];
|
2013-05-13 19:31:27 -10:00
|
|
|
|
if (result == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return default(T);
|
|
|
|
|
|
}
|
|
|
|
|
|
return result.TryConvertTo<T>().Result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-12-16 12:51:02 +11:00
|
|
|
|
public virtual T GetCacheItem<T>(string cacheKey, Func<T> getCacheItem)
|
2013-05-13 19:31:27 -10:00
|
|
|
|
{
|
2013-12-16 12:51:02 +11:00
|
|
|
|
return (T)StaticCache.GetOrAdd(cacheKey, getCacheItem());
|
2013-05-13 19:31:27 -10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|