2018-06-29 19:52:40 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
using Umbraco.Core.Composing;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Core.Cache
|
|
|
|
|
|
{
|
2019-01-17 11:01:23 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Provides a base class to fast, dictionary-based <see cref="IAppCache"/> implementations.
|
|
|
|
|
|
/// </summary>
|
2019-11-08 14:26:06 +11:00
|
|
|
|
public abstract class FastDictionaryAppCacheBase : IAppCache
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
|
|
|
|
|
// prefix cache keys so we know which one are ours
|
|
|
|
|
|
protected const string CacheItemPrefix = "umbrtmche";
|
|
|
|
|
|
|
2019-01-17 11:01:23 +01:00
|
|
|
|
#region IAppCache
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
2019-01-17 11:01:23 +01:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public virtual object Get(string key)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2019-01-17 11:01:23 +01:00
|
|
|
|
key = GetCacheKey(key);
|
|
|
|
|
|
Lazy<object> result;
|
|
|
|
|
|
try
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2019-01-17 11:01:23 +01:00
|
|
|
|
EnterReadLock();
|
|
|
|
|
|
result = GetEntry(key) as Lazy<object>; // null if key not found
|
|
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
ExitReadLock();
|
|
|
|
|
|
}
|
2019-11-07 19:16:45 +11:00
|
|
|
|
return result == null ? null : SafeLazy.GetSafeLazyValue(result); // return exceptions as null
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-17 11:01:23 +01:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public abstract object Get(string key, Func<object> factory);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
2019-01-17 11:01:23 +01:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public virtual IEnumerable<object> SearchByKey(string keyStartsWith)
|
|
|
|
|
|
{
|
|
|
|
|
|
var plen = CacheItemPrefix.Length + 1;
|
2020-10-26 14:26:49 +01:00
|
|
|
|
IEnumerable<KeyValuePair<object, object>> entries;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
try
|
|
|
|
|
|
{
|
2019-01-17 11:01:23 +01:00
|
|
|
|
EnterReadLock();
|
|
|
|
|
|
entries = GetDictionaryEntries()
|
|
|
|
|
|
.Where(x => ((string)x.Key).Substring(plen).InvariantStartsWith(keyStartsWith))
|
|
|
|
|
|
.ToArray(); // evaluate while locked
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
2019-01-17 11:01:23 +01:00
|
|
|
|
finally
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2019-01-17 11:01:23 +01:00
|
|
|
|
ExitReadLock();
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
2019-01-17 11:01:23 +01:00
|
|
|
|
|
|
|
|
|
|
return entries
|
2019-11-07 19:16:45 +11:00
|
|
|
|
.Select(x => SafeLazy.GetSafeLazyValue((Lazy<object>)x.Value)) // return exceptions as null
|
2019-01-17 11:01:23 +01:00
|
|
|
|
.Where(x => x != null); // backward compat, don't store null values in the cache
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-17 11:01:23 +01:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public virtual IEnumerable<object> SearchByRegex(string regex)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2019-01-17 11:01:23 +01:00
|
|
|
|
const string prefix = CacheItemPrefix + "-";
|
|
|
|
|
|
var compiled = new Regex(regex, RegexOptions.Compiled);
|
|
|
|
|
|
var plen = prefix.Length;
|
2020-10-26 14:26:49 +01:00
|
|
|
|
IEnumerable<KeyValuePair<object, object>> entries;
|
2019-01-17 11:01:23 +01:00
|
|
|
|
try
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2019-01-17 11:01:23 +01:00
|
|
|
|
EnterReadLock();
|
|
|
|
|
|
entries = GetDictionaryEntries()
|
|
|
|
|
|
.Where(x => compiled.IsMatch(((string)x.Key).Substring(plen)))
|
|
|
|
|
|
.ToArray(); // evaluate while locked
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
2019-01-17 11:01:23 +01:00
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
ExitReadLock();
|
|
|
|
|
|
}
|
|
|
|
|
|
return entries
|
2019-11-07 19:16:45 +11:00
|
|
|
|
.Select(x => SafeLazy.GetSafeLazyValue((Lazy<object>)x.Value)) // return exceptions as null
|
2019-01-22 18:03:39 -05:00
|
|
|
|
.Where(x => x != null); // backward compatible, don't store null values in the cache
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-17 11:01:23 +01:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public virtual void Clear()
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
EnterWriteLock();
|
|
|
|
|
|
foreach (var entry in GetDictionaryEntries()
|
|
|
|
|
|
.ToArray())
|
|
|
|
|
|
RemoveEntry((string) entry.Key);
|
|
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
ExitWriteLock();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-17 11:01:23 +01:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public virtual void Clear(string key)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
|
|
|
|
|
var cacheKey = GetCacheKey(key);
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
EnterWriteLock();
|
|
|
|
|
|
RemoveEntry(cacheKey);
|
|
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
ExitWriteLock();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-17 11:01:23 +01:00
|
|
|
|
/// <inheritdoc />
|
2020-03-19 08:53:18 +01:00
|
|
|
|
public virtual void ClearOfType(Type type)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (type == null) return;
|
|
|
|
|
|
var isInterface = type.IsInterface;
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
EnterWriteLock();
|
|
|
|
|
|
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
|
|
|
|
|
|
// get non-created as NonCreatedValue & exceptions as null
|
2019-11-07 19:16:45 +11:00
|
|
|
|
var value = SafeLazy.GetSafeLazyValue((Lazy<object>) x.Value, true);
|
2018-06-29 19:52:40 +02: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));
|
|
|
|
|
|
})
|
|
|
|
|
|
.ToArray())
|
|
|
|
|
|
RemoveEntry((string) entry.Key);
|
|
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
ExitWriteLock();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-17 11:01:23 +01:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public virtual void ClearOfType<T>()
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
|
|
|
|
|
var typeOfT = typeof(T);
|
|
|
|
|
|
var isInterface = typeOfT.IsInterface;
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
EnterWriteLock();
|
|
|
|
|
|
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"
|
|
|
|
|
|
// get non-created as NonCreatedValue & exceptions as null
|
2019-11-07 19:16:45 +11:00
|
|
|
|
var value = SafeLazy.GetSafeLazyValue((Lazy<object>) x.Value, true);
|
2018-06-29 19:52:40 +02: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));
|
|
|
|
|
|
})
|
|
|
|
|
|
.ToArray())
|
|
|
|
|
|
RemoveEntry((string) entry.Key);
|
|
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
ExitWriteLock();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-17 11:01:23 +01:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public virtual void ClearOfType<T>(Func<string, T, bool> predicate)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
|
|
|
|
|
var typeOfT = typeof(T);
|
|
|
|
|
|
var isInterface = typeOfT.IsInterface;
|
|
|
|
|
|
var plen = CacheItemPrefix.Length + 1;
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
EnterWriteLock();
|
|
|
|
|
|
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"
|
|
|
|
|
|
// get non-created as NonCreatedValue & exceptions as null
|
2019-11-07 19:16:45 +11:00
|
|
|
|
var value = SafeLazy.GetSafeLazyValue((Lazy<object>) x.Value, true);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
if (value == null) return true;
|
|
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
}))
|
|
|
|
|
|
RemoveEntry((string) entry.Key);
|
|
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
ExitWriteLock();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-17 11:01:23 +01:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public virtual void ClearByKey(string keyStartsWith)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
|
|
|
|
|
var plen = CacheItemPrefix.Length + 1;
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
EnterWriteLock();
|
|
|
|
|
|
foreach (var entry in GetDictionaryEntries()
|
|
|
|
|
|
.Where(x => ((string)x.Key).Substring(plen).InvariantStartsWith(keyStartsWith))
|
|
|
|
|
|
.ToArray())
|
|
|
|
|
|
RemoveEntry((string) entry.Key);
|
|
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
ExitWriteLock();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-17 11:01:23 +01:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public virtual void ClearByRegex(string regex)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2019-01-17 11:01:23 +01:00
|
|
|
|
var compiled = new Regex(regex, RegexOptions.Compiled);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
var plen = CacheItemPrefix.Length + 1;
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
EnterWriteLock();
|
|
|
|
|
|
foreach (var entry in GetDictionaryEntries()
|
2019-01-17 11:01:23 +01:00
|
|
|
|
.Where(x => compiled.IsMatch(((string)x.Key).Substring(plen)))
|
2018-06-29 19:52:40 +02:00
|
|
|
|
.ToArray())
|
|
|
|
|
|
RemoveEntry((string) entry.Key);
|
|
|
|
|
|
}
|
|
|
|
|
|
finally
|
|
|
|
|
|
{
|
|
|
|
|
|
ExitWriteLock();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
2019-01-17 11:01:23 +01:00
|
|
|
|
#region Dictionary
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
2019-01-17 11:01:23 +01:00
|
|
|
|
// manipulate the underlying cache entries
|
|
|
|
|
|
// these *must* be called from within the appropriate locks
|
|
|
|
|
|
// and use the full prefixed cache keys
|
2020-10-26 14:26:49 +01:00
|
|
|
|
protected abstract IEnumerable<KeyValuePair<object, object>> GetDictionaryEntries();
|
2019-01-17 11:01:23 +01:00
|
|
|
|
protected abstract void RemoveEntry(string key);
|
|
|
|
|
|
protected abstract object GetEntry(string key);
|
|
|
|
|
|
|
|
|
|
|
|
// read-write lock the underlying cache
|
|
|
|
|
|
//protected abstract IDisposable ReadLock { get; }
|
|
|
|
|
|
//protected abstract IDisposable WriteLock { get; }
|
|
|
|
|
|
|
|
|
|
|
|
protected abstract void EnterReadLock();
|
|
|
|
|
|
protected abstract void ExitReadLock();
|
|
|
|
|
|
protected abstract void EnterWriteLock();
|
|
|
|
|
|
protected abstract void ExitWriteLock();
|
|
|
|
|
|
|
|
|
|
|
|
protected string GetCacheKey(string key)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2019-01-17 11:01:23 +01:00
|
|
|
|
return $"{CacheItemPrefix}-{key}";
|
|
|
|
|
|
}
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
2019-11-27 14:10:48 +01:00
|
|
|
|
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|