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-08-08 19:46:58 +10:00
|
|
|
|
internal class StaticCacheProvider : ICacheProvider
|
2013-05-13 19:31:27 -10:00
|
|
|
|
{
|
2013-08-08 19:46:58 +10:00
|
|
|
|
internal readonly ConcurrentDictionary<string, object> StaticCache = new ConcurrentDictionary<string, object>();
|
2013-05-13 19:31:27 -10:00
|
|
|
|
|
2013-08-08 19:46:58 +10:00
|
|
|
|
public virtual void ClearAllCache()
|
2013-05-13 19:31:27 -10:00
|
|
|
|
{
|
2013-08-08 19:46:58 +10:00
|
|
|
|
StaticCache.Clear();
|
2013-05-13 19:31:27 -10:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-08-08 19:46:58 +10:00
|
|
|
|
public virtual void ClearCacheItem(string key)
|
2013-05-13 19:31:27 -10:00
|
|
|
|
{
|
|
|
|
|
|
object val;
|
2013-08-08 19:46:58 +10:00
|
|
|
|
StaticCache.TryRemove(key, out val);
|
2013-05-13 19:31:27 -10:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-08-08 19:46:58 +10:00
|
|
|
|
public virtual void ClearCacheObjectTypes(string typeName)
|
2013-05-13 19:31:27 -10:00
|
|
|
|
{
|
2013-08-08 19:46:58 +10:00
|
|
|
|
foreach (var key in StaticCache.Keys)
|
2013-05-13 19:31:27 -10:00
|
|
|
|
{
|
2013-08-08 19:46:58 +10:00
|
|
|
|
if (StaticCache[key] != null
|
|
|
|
|
|
&& StaticCache[key].GetType().ToString().InvariantEquals(typeName))
|
2013-05-13 19:31:27 -10:00
|
|
|
|
{
|
|
|
|
|
|
object val;
|
2013-08-08 19:46:58 +10:00
|
|
|
|
StaticCache.TryRemove(key, out val);
|
2013-05-13 19:31:27 -10:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2013-08-08 20:25:26 +10:00
|
|
|
|
}
|
2013-05-13 19:31:27 -10:00
|
|
|
|
|
2013-08-08 19:46:58 +10:00
|
|
|
|
public virtual void ClearCacheByKeySearch(string keyStartsWith)
|
2013-05-13 19:31:27 -10:00
|
|
|
|
{
|
2013-08-08 19:46:58 +10:00
|
|
|
|
foreach (var key in StaticCache.Keys)
|
2013-05-13 19:31:27 -10:00
|
|
|
|
{
|
|
|
|
|
|
if (key.InvariantStartsWith(keyStartsWith))
|
|
|
|
|
|
{
|
|
|
|
|
|
ClearCacheItem(key);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-08-08 19:46:58 +10:00
|
|
|
|
public virtual void ClearCacheByKeyExpression(string regexString)
|
2013-05-13 19:31:27 -10:00
|
|
|
|
{
|
2013-08-08 19:46:58 +10:00
|
|
|
|
foreach (var key in StaticCache.Keys)
|
2013-05-13 19:31:27 -10:00
|
|
|
|
{
|
|
|
|
|
|
if (Regex.IsMatch(key, regexString))
|
|
|
|
|
|
{
|
|
|
|
|
|
ClearCacheItem(key);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-08-08 20:25:26 +10:00
|
|
|
|
public virtual IEnumerable<object> GetCacheItemsByKeySearch(string keyStartsWith)
|
2013-05-13 19:31:27 -10:00
|
|
|
|
{
|
2013-08-08 19:46:58 +10:00
|
|
|
|
return (from KeyValuePair<string, object> c in StaticCache
|
2013-05-13 19:31:27 -10:00
|
|
|
|
where c.Key.InvariantStartsWith(keyStartsWith)
|
2013-08-08 20:25:26 +10:00
|
|
|
|
select c.Value).ToList();
|
2013-05-13 19:31:27 -10:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-08-08 20:25:26 +10:00
|
|
|
|
public virtual object GetCacheItem(string cacheKey)
|
2013-05-13 19:31:27 -10:00
|
|
|
|
{
|
2013-08-08 19:46:58 +10:00
|
|
|
|
var result = StaticCache[cacheKey];
|
2013-08-08 20:25:26 +10:00
|
|
|
|
return result;
|
2013-05-13 19:31:27 -10:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-08-08 20:25:26 +10:00
|
|
|
|
public virtual object GetCacheItem(string cacheKey, Func<object> getCacheItem)
|
2013-05-13 19:31:27 -10:00
|
|
|
|
{
|
2013-08-08 20:25:26 +10:00
|
|
|
|
return StaticCache.GetOrAdd(cacheKey, getCacheItem());
|
2013-05-13 19:31:27 -10:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|