Core.Cache - add new method to remove items from cache

This commit is contained in:
Stephan
2013-09-18 10:05:44 +02:00
parent 95df28bf64
commit 761354dbf5
5 changed files with 49 additions and 32 deletions

View File

@@ -15,6 +15,7 @@ namespace Umbraco.Core.Cache
public abstract void ClearCacheItem(string key);
public abstract void ClearCacheObjectTypes(string typeName);
public abstract void ClearCacheObjectTypes<T>();
public abstract void ClearCacheObjectTypes<T>(Func<string, T, bool> predicate);
public abstract void ClearCacheByKeySearch(string keyStartsWith);
public abstract void ClearCacheByKeyExpression(string regexString);
public abstract IEnumerable<T> GetCacheItemsByKeySearch<T>(string keyStartsWith);

View File

@@ -103,6 +103,33 @@ namespace Umbraco.Core.Cache
}
}
/// <summary>
/// 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);
}
}
/// <summary>
/// Clears all cache items that starts with the key passed.
/// </summary>

View File

@@ -23,6 +23,10 @@ namespace Umbraco.Core.Cache
{
}
public override void ClearCacheObjectTypes<T>(Func<string, T, bool> predicate)
{
}
public override void ClearCacheByKeySearch(string keyStartsWith)
{
}

View File

@@ -27,50 +27,27 @@ namespace Umbraco.Core.Cache
public override void ClearCacheObjectTypes(string typeName)
{
foreach (var key in _staticCache.Keys)
{
if (_staticCache[key] != null
&& _staticCache[key].GetType().ToString().InvariantEquals(typeName))
{
object val;
_staticCache.TryRemove(key, out val);
}
}
_staticCache.RemoveAll(kvp => kvp.Value.GetType().ToString().InvariantEquals(typeName));
}
public override void ClearCacheObjectTypes<T>()
{
foreach (var key in _staticCache.Keys)
{
if (_staticCache[key] != null
&& _staticCache[key].GetType() == typeof(T))
{
object val;
_staticCache.TryRemove(key, out val);
}
}
_staticCache.RemoveAll(kvp => kvp.Value is T);
}
public override void ClearCacheObjectTypes<T>(Func<string, T, bool> predicate)
{
_staticCache.RemoveAll(kvp => kvp.Value is T && predicate(kvp.Key, (T)kvp.Value));
}
public override void ClearCacheByKeySearch(string keyStartsWith)
{
foreach (var key in _staticCache.Keys)
{
if (key.InvariantStartsWith(keyStartsWith))
{
ClearCacheItem(key);
}
}
_staticCache.RemoveAll(kvp => kvp.Key.InvariantStartsWith(keyStartsWith));
}
public override void ClearCacheByKeyExpression(string regexString)
{
foreach (var key in _staticCache.Keys)
{
if (Regex.IsMatch(key, regexString))
{
ClearCacheItem(key);
}
}
_staticCache.RemoveAll(kvp => Regex.IsMatch(kvp.Key, regexString));
}
public override IEnumerable<T> GetCacheItemsByKeySearch<T>(string keyStartsWith)

View File

@@ -110,6 +110,14 @@ namespace Umbraco.Core
}
}
internal void ClearStaticCacheObjectTypes<T>(Func<string, T, bool> predicate)
{
if (_enableCache)
_staticCache.ClearCacheObjectTypes(predicate);
else
_nullStaticCache.ClearCacheObjectTypes(predicate);
}
/// <summary>
/// Clears all static cache items that starts with the key passed.
/// </summary>