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;
|
2017-05-30 15:56:27 +02:00
|
|
|
|
using Umbraco.Core.Composing;
|
2013-08-12 15:06:12 +02:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Core.Cache
|
|
|
|
|
|
{
|
2013-12-16 16:21:38 +11:00
|
|
|
|
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
|
|
|
|
|
|
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
|
2015-05-19 09:16:05 +02:00
|
|
|
|
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; }
|
|
|
|
|
|
|
2014-05-27 12:16:41 +02:00
|
|
|
|
protected string GetCacheKey(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
return string.Format("{0}-{1}", CacheItemPrefix, key);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-05-19 09:16:05 +02:00
|
|
|
|
protected internal static Lazy<object> GetSafeLazy(Func<object> getCacheItem)
|
2014-06-17 18:42:00 +02:00
|
|
|
|
{
|
2015-05-19 09:16:05 +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
|
|
|
|
{
|
2015-05-19 09:16:05 +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);
|
|
|
|
|
|
}
|
2015-05-19 09:16:05 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal class ExceptionHolder
|
|
|
|
|
|
{
|
|
|
|
|
|
public ExceptionHolder(Exception e)
|
2014-06-17 18:42:00 +02:00
|
|
|
|
{
|
2015-05-19 09:16:05 +02:00
|
|
|
|
Exception = e;
|
2014-06-17 18:42:00 +02:00
|
|
|
|
}
|
2015-05-19 09:16:05 +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
|
|
|
|
|
|
|
2013-08-12 15:06:12 +02:00
|
|
|
|
public virtual void ClearAllCache()
|
|
|
|
|
|
{
|
2014-06-28 12:09:54 +02:00
|
|
|
|
using (WriteLock)
|
2013-08-12 15:06:12 +02:00
|
|
|
|
{
|
2014-05-27 12:16:41 +02:00
|
|
|
|
foreach (var entry in GetDictionaryEntries()
|
|
|
|
|
|
.ToArray())
|
|
|
|
|
|
RemoveEntry((string) entry.Key);
|
2013-08-12 15:06:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public virtual void ClearCacheItem(string key)
|
|
|
|
|
|
{
|
2014-06-28 12:09:54 +02:00
|
|
|
|
var cacheKey = GetCacheKey(key);
|
|
|
|
|
|
using (WriteLock)
|
2013-08-12 15:06:12 +02:00
|
|
|
|
{
|
2014-05-27 12:16:41 +02:00
|
|
|
|
RemoveEntry(cacheKey);
|
2013-08-12 15:06:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
2013-08-12 15:06:12 +02:00
|
|
|
|
{
|
2014-05-27 12:16:41 +02:00
|
|
|
|
foreach (var entry in GetDictionaryEntries()
|
|
|
|
|
|
.Where(x =>
|
2013-09-18 12:27:38 +02:00
|
|
|
|
{
|
2014-05-27 12:16:41 +02:00
|
|
|
|
// 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));
|
2013-09-18 12:27:38 +02:00
|
|
|
|
})
|
2014-05-27 12:16:41 +02:00
|
|
|
|
.ToArray())
|
|
|
|
|
|
RemoveEntry((string) entry.Key);
|
2013-08-12 15:06:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2013-09-18 12:27:38 +02:00
|
|
|
|
|
|
|
|
|
|
public virtual void ClearCacheObjectTypes<T>()
|
|
|
|
|
|
{
|
2014-05-27 12:16:41 +02:00
|
|
|
|
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)
|
2013-09-18 12:27:38 +02:00
|
|
|
|
{
|
2014-05-27 12:16:41 +02:00
|
|
|
|
foreach (var entry in GetDictionaryEntries()
|
|
|
|
|
|
.Where(x =>
|
2013-09-18 12:27:38 +02:00
|
|
|
|
{
|
2014-05-27 12:16:41 +02:00
|
|
|
|
// 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-29 16:40:52 +11:00
|
|
|
|
|
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));
|
2013-09-18 12:27:38 +02:00
|
|
|
|
})
|
2014-05-27 12:16:41 +02:00
|
|
|
|
.ToArray())
|
|
|
|
|
|
RemoveEntry((string) entry.Key);
|
2013-09-18 12:27:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public virtual void ClearCacheObjectTypes<T>(Func<string, T, bool> predicate)
|
|
|
|
|
|
{
|
2014-05-27 12:16:41 +02:00
|
|
|
|
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)
|
2013-09-18 12:27:38 +02:00
|
|
|
|
{
|
2014-05-27 12:16:41 +02:00
|
|
|
|
foreach (var entry in GetDictionaryEntries()
|
|
|
|
|
|
.Where(x =>
|
2013-09-18 12:27:38 +02:00
|
|
|
|
{
|
2014-05-27 12:16:41 +02:00
|
|
|
|
// 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-29 16:40:52 +11:00
|
|
|
|
|
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))
|
2015-01-29 16:40:52 +11:00
|
|
|
|
// 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);
|
2013-09-18 12:27:38 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-08-12 15:06:12 +02:00
|
|
|
|
public virtual void ClearCacheByKeySearch(string keyStartsWith)
|
|
|
|
|
|
{
|
2014-05-27 12:16:41 +02:00
|
|
|
|
var plen = CacheItemPrefix.Length + 1;
|
2014-06-28 12:09:54 +02:00
|
|
|
|
using (WriteLock)
|
2013-08-12 15:06:12 +02:00
|
|
|
|
{
|
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);
|
2013-08-12 15:06:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public virtual void ClearCacheByKeyExpression(string regexString)
|
|
|
|
|
|
{
|
2014-05-27 12:16:41 +02:00
|
|
|
|
var plen = CacheItemPrefix.Length + 1;
|
2014-06-28 12:09:54 +02:00
|
|
|
|
using (WriteLock)
|
2013-08-12 15:06:12 +02:00
|
|
|
|
{
|
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);
|
2013-08-12 15:06:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2014-05-27 12:16:41 +02:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Get
|
|
|
|
|
|
|
2013-08-12 15:06:12 +02:00
|
|
|
|
public virtual IEnumerable<object> GetCacheItemsByKeySearch(string keyStartsWith)
|
|
|
|
|
|
{
|
2014-05-27 12:16:41 +02:00
|
|
|
|
var plen = CacheItemPrefix.Length + 1;
|
2014-06-28 12:09:54 +02:00
|
|
|
|
IEnumerable<DictionaryEntry> entries;
|
|
|
|
|
|
using (ReadLock)
|
2014-05-22 09:36:08 +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-22 09:36:08 +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
|
2013-08-12 15:06:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-05-22 09:36:08 +02:00
|
|
|
|
public virtual IEnumerable<object> GetCacheItemsByKeyExpression(string regexString)
|
2014-04-17 18:10:42 +10:00
|
|
|
|
{
|
2014-05-27 12:16:41 +02:00
|
|
|
|
const string prefix = CacheItemPrefix + "-";
|
|
|
|
|
|
var plen = prefix.Length;
|
2014-06-28 12:09:54 +02:00
|
|
|
|
IEnumerable<DictionaryEntry> entries;
|
|
|
|
|
|
using (ReadLock)
|
2014-04-17 18:10:42 +10: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-22 09:36:08 +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-04-17 18:10:42 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-08-12 15:06:12 +02:00
|
|
|
|
public virtual object GetCacheItem(string cacheKey)
|
|
|
|
|
|
{
|
2014-05-27 12:16:41 +02:00
|
|
|
|
cacheKey = GetCacheKey(cacheKey);
|
2014-06-28 12:09:54 +02:00
|
|
|
|
Lazy<object> result;
|
|
|
|
|
|
using (ReadLock)
|
2014-05-22 09:36:08 +02:00
|
|
|
|
{
|
2014-06-28 12:09:54 +02:00
|
|
|
|
result = GetEntry(cacheKey) as Lazy<object>; // null if key not found
|
2014-05-22 09:36:08 +02:00
|
|
|
|
}
|
2014-06-28 12:09:54 +02:00
|
|
|
|
return result == null ? null : GetSafeLazyValue(result); // return exceptions as null
|
2013-08-12 15:06:12 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2014-05-27 12:16:41 +02:00
|
|
|
|
public abstract object GetCacheItem(string cacheKey, Func<object> getCacheItem);
|
2013-08-12 15:06:12 +02:00
|
|
|
|
|
2014-05-27 12:16:41 +02:00
|
|
|
|
#endregion
|
2013-08-12 15:06:12 +02:00
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|