Files
Umbraco-CMS/src/Umbraco.Core/Cache/DictionaryCacheProviderBase.cs
T

255 lines
10 KiB
C#
Raw Normal View History

2013-08-12 15:06:12 +02:00
using System;
2014-05-27 12:16:41 +02:00
using System.Collections;
2013-08-12 15:06:12 +02:00
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Umbraco.Core.Cache
{
internal abstract class DictionaryCacheProviderBase : ICacheProvider
2013-08-12 15:06:12 +02:00
{
2014-05-27 12:16:41 +02:00
// prefix cache keys so we know which one are ours
2013-08-12 15:06:12 +02:00
protected const string CacheItemPrefix = "umbrtmche";
2014-06-28 12:09:54 +02:00
// an object that represent a value that has not been created yet
protected internal static readonly object ValueNotCreated = new object();
2014-05-27 12:16:41 +02:00
// manupulate the underlying cache entries
// these *must* be called from within the appropriate locks
// and use the full prefixed cache keys
protected abstract IEnumerable<DictionaryEntry> GetDictionaryEntries();
protected abstract void RemoveEntry(string key);
protected abstract object GetEntry(string key);
2014-06-28 12:09:54 +02:00
// read-write lock the underlying cache
protected abstract IDisposable ReadLock { get; }
protected abstract IDisposable WriteLock { get; }
2013-08-12 15:06:12 +02:00
protected string GetCacheKey(string key)
{
return string.Format("{0}-{1}", CacheItemPrefix, key);
}
2014-05-27 12:16:41 +02:00
protected internal static Lazy<object> GetSafeLazy(Func<object> getCacheItem)
2014-06-17 18:42:00 +02:00
{
// try to generate the value and if it fails,
// wrap in an ExceptionHolder - would be much simpler
// to just use lazy.IsValueFaulted alas that field is
// internal
return new Lazy<object>(() =>
2014-06-17 18:42:00 +02:00
{
try
{
return getCacheItem();
}
catch (Exception e)
{
return new ExceptionHolder(e);
}
});
}
protected internal static object GetSafeLazyValue(Lazy<object> lazy, bool onlyIfValueIsCreated = false)
{
// if onlyIfValueIsCreated, do not trigger value creation
// must return something, though, to differenciate from null values
if (onlyIfValueIsCreated && lazy.IsValueCreated == false) return ValueNotCreated;
// if execution has thrown then lazy.IsValueCreated is false
// and lazy.IsValueFaulted is true (but internal) so we use our
// own exception holder (see Lazy<T> source code) to return null
if (lazy.Value is ExceptionHolder) return null;
// we have a value and execution has not thrown so returning
2015-05-21 16:01:05 +02:00
// here does not throw - unless we're re-entering, take care of it
try
{
return lazy.Value;
}
catch (InvalidOperationException e)
{
throw new InvalidOperationException("The method that computes a value for the cache has tried to read that value from the cache.", e);
}
}
internal class ExceptionHolder
{
public ExceptionHolder(Exception e)
2014-06-17 18:42:00 +02:00
{
Exception = e;
2014-06-17 18:42:00 +02:00
}
public Exception Exception { get; private set; }
2014-06-17 18:42:00 +02:00
}
2014-05-27 12:16:41 +02:00
#region Clear
public virtual void ClearAllCache()
{
2014-06-28 12:09:54 +02:00
using (WriteLock)
2014-05-27 12:16:41 +02:00
{
foreach (var entry in GetDictionaryEntries()
.ToArray())
RemoveEntry((string) entry.Key);
}
}
public virtual void ClearCacheItem(string key)
{
2014-06-28 12:09:54 +02:00
var cacheKey = GetCacheKey(key);
using (WriteLock)
2014-05-27 12:16:41 +02:00
{
RemoveEntry(cacheKey);
}
}
public virtual void ClearCacheObjectTypes(string typeName)
{
2015-01-30 18:28:20 +01:00
var type = TypeFinder.GetTypeByName(typeName);
2015-01-30 10:51:50 +01:00
if (type == null) return;
var isInterface = type.IsInterface;
2014-06-28 12:09:54 +02:00
using (WriteLock)
2014-05-27 12:16:41 +02:00
{
foreach (var entry in GetDictionaryEntries()
.Where(x =>
{
// entry.Value is Lazy<object> and not null, its value may be null
// remove null values as well, does not hurt
2014-06-28 12:09:54 +02:00
// get non-created as NonCreatedValue & exceptions as null
var value = GetSafeLazyValue((Lazy<object>)x.Value, true);
2015-01-30 10:51:50 +01:00
// if T is an interface remove anything that implements that interface
// otherwise remove exact types (not inherited types)
return value == null || (isInterface ? (type.IsInstanceOfType(value)) : (value.GetType() == type));
2014-05-27 12:16:41 +02:00
})
.ToArray())
RemoveEntry((string) entry.Key);
}
}
public virtual void ClearCacheObjectTypes<T>()
{
var typeOfT = typeof(T);
2015-01-30 10:51:50 +01:00
var isInterface = typeOfT.IsInterface;
2014-06-28 12:09:54 +02:00
using (WriteLock)
2014-05-27 12:16:41 +02:00
{
foreach (var entry in GetDictionaryEntries()
.Where(x =>
{
// entry.Value is Lazy<object> and not null, its value may be null
// remove null values as well, does not hurt
// compare on exact type, don't use "is"
2014-06-28 12:09:54 +02:00
// get non-created as NonCreatedValue & exceptions as null
var value = GetSafeLazyValue((Lazy<object>)x.Value, true);
2015-01-30 10:51:50 +01:00
// if T is an interface remove anything that implements that interface
// otherwise remove exact types (not inherited types)
return value == null || (isInterface ? (value is T) : (value.GetType() == typeOfT));
2014-05-27 12:16:41 +02:00
})
.ToArray())
RemoveEntry((string) entry.Key);
}
}
public virtual void ClearCacheObjectTypes<T>(Func<string, T, bool> predicate)
{
var typeOfT = typeof(T);
2015-01-30 10:51:50 +01:00
var isInterface = typeOfT.IsInterface;
2014-05-27 12:16:41 +02:00
var plen = CacheItemPrefix.Length + 1;
2014-06-28 12:09:54 +02:00
using (WriteLock)
2014-05-27 12:16:41 +02:00
{
foreach (var entry in GetDictionaryEntries()
.Where(x =>
{
// entry.Value is Lazy<object> and not null, its value may be null
// remove null values as well, does not hurt
// compare on exact type, don't use "is"
2014-06-28 12:09:54 +02:00
// get non-created as NonCreatedValue & exceptions as null
var value = GetSafeLazyValue((Lazy<object>)x.Value, true);
2014-05-27 12:16:41 +02:00
if (value == null) return true;
2015-01-30 10:51:50 +01:00
// if T is an interface remove anything that implements that interface
// otherwise remove exact types (not inherited types)
return (isInterface ? (value is T) : (value.GetType() == typeOfT))
// run predicate on the 'public key' part only, ie without prefix
&& predicate(((string) x.Key).Substring(plen), (T) value);
2014-05-27 12:16:41 +02:00
}))
RemoveEntry((string) entry.Key);
}
}
public virtual void ClearCacheByKeySearch(string keyStartsWith)
{
var plen = CacheItemPrefix.Length + 1;
2014-06-28 12:09:54 +02:00
using (WriteLock)
2014-05-27 12:16:41 +02:00
{
foreach (var entry in GetDictionaryEntries()
.Where(x => ((string)x.Key).Substring(plen).InvariantStartsWith(keyStartsWith))
.ToArray())
RemoveEntry((string) entry.Key);
}
}
public virtual void ClearCacheByKeyExpression(string regexString)
{
var plen = CacheItemPrefix.Length + 1;
2014-06-28 12:09:54 +02:00
using (WriteLock)
2014-05-27 12:16:41 +02:00
{
foreach (var entry in GetDictionaryEntries()
.Where(x => Regex.IsMatch(((string)x.Key).Substring(plen), regexString))
.ToArray())
RemoveEntry((string) entry.Key);
}
}
#endregion
#region Get
public virtual IEnumerable<object> GetCacheItemsByKeySearch(string keyStartsWith)
{
var plen = CacheItemPrefix.Length + 1;
2014-06-28 12:09:54 +02:00
IEnumerable<DictionaryEntry> entries;
using (ReadLock)
2014-05-27 12:16:41 +02:00
{
2014-06-28 12:09:54 +02:00
entries = GetDictionaryEntries()
2014-05-27 12:16:41 +02:00
.Where(x => ((string)x.Key).Substring(plen).InvariantStartsWith(keyStartsWith))
2014-06-28 12:09:54 +02:00
.ToArray(); // evaluate while locked
2014-05-27 12:16:41 +02:00
}
2014-06-28 12:09:54 +02:00
return entries
.Select(x => GetSafeLazyValue((Lazy<object>)x.Value)) // return exceptions as null
.Where(x => x != null); // backward compat, don't store null values in the cache
2014-05-27 12:16:41 +02:00
}
public virtual IEnumerable<object> GetCacheItemsByKeyExpression(string regexString)
{
const string prefix = CacheItemPrefix + "-";
var plen = prefix.Length;
2014-06-28 12:09:54 +02:00
IEnumerable<DictionaryEntry> entries;
using (ReadLock)
2014-05-27 12:16:41 +02:00
{
2014-06-28 12:09:54 +02:00
entries = GetDictionaryEntries()
2014-05-27 12:16:41 +02:00
.Where(x => Regex.IsMatch(((string)x.Key).Substring(plen), regexString))
2014-06-28 12:09:54 +02:00
.ToArray(); // evaluate while locked
2014-05-27 12:16:41 +02:00
}
2014-06-28 12:09:54 +02:00
return entries
.Select(x => GetSafeLazyValue((Lazy<object>)x.Value)) // return exceptions as null
.Where(x => x != null); // backward compat, don't store null values in the cache
2014-05-27 12:16:41 +02:00
}
public virtual object GetCacheItem(string cacheKey)
{
cacheKey = GetCacheKey(cacheKey);
2014-06-28 12:09:54 +02:00
Lazy<object> result;
using (ReadLock)
2014-05-27 12:16:41 +02:00
{
2014-06-28 12:09:54 +02:00
result = GetEntry(cacheKey) as Lazy<object>; // null if key not found
2014-05-27 12:16:41 +02:00
}
2014-06-28 12:09:54 +02:00
return result == null ? null : GetSafeLazyValue(result); // return exceptions as null
2014-05-27 12:16:41 +02:00
}
public abstract object GetCacheItem(string cacheKey, Func<object> getCacheItem);
#endregion
2013-08-12 15:06:12 +02:00
}
}