2021-10-19 23:11:54 +11:00
|
|
|
using System;
|
2019-01-17 11:01:23 +01:00
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text.RegularExpressions;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Extensions;
|
2019-01-17 11:01:23 +01:00
|
|
|
|
2021-02-18 11:06:02 +01:00
|
|
|
namespace Umbraco.Cms.Core.Cache
|
2019-01-17 11:01:23 +01:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Implements a fast <see cref="IAppCache"/> on top of a concurrent dictionary.
|
|
|
|
|
/// </summary>
|
2019-11-08 14:26:06 +11:00
|
|
|
public class FastDictionaryAppCache : IAppCache
|
2019-01-17 11:01:23 +01:00
|
|
|
{
|
2019-11-11 16:07:47 +11:00
|
|
|
|
2019-01-17 11:01:23 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the internal items dictionary, for tests only!
|
|
|
|
|
/// </summary>
|
2021-12-16 13:44:20 +01:00
|
|
|
private readonly ConcurrentDictionary<string, Lazy<object?>> _items = new ConcurrentDictionary<string, Lazy<object?>>();
|
2019-01-17 11:01:23 +01:00
|
|
|
|
2021-10-19 23:11:54 +11:00
|
|
|
public IEnumerable<string> Keys => _items.Keys;
|
|
|
|
|
|
2019-11-08 14:51:20 +11:00
|
|
|
public int Count => _items.Count;
|
2019-11-08 14:26:06 +11:00
|
|
|
|
2019-01-17 11:01:23 +01:00
|
|
|
/// <inheritdoc />
|
2021-12-16 13:44:20 +01:00
|
|
|
public object? Get(string cacheKey)
|
2019-01-17 11:01:23 +01:00
|
|
|
{
|
2019-11-08 14:51:20 +11:00
|
|
|
_items.TryGetValue(cacheKey, out var result); // else null
|
2021-12-16 13:44:20 +01:00
|
|
|
return result == null ? null : SafeLazy.GetSafeLazyValue(result!); // return exceptions as null
|
2019-01-17 11:01:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2021-12-16 13:44:20 +01:00
|
|
|
public object? Get(string cacheKey, Func<object?> getCacheItem)
|
2019-01-17 11:01:23 +01:00
|
|
|
{
|
2019-11-08 14:51:20 +11:00
|
|
|
var result = _items.GetOrAdd(cacheKey, k => SafeLazy.GetSafeLazy(getCacheItem));
|
2019-01-17 11:01:23 +01:00
|
|
|
|
|
|
|
|
var value = result.Value; // will not throw (safe lazy)
|
2019-11-07 19:16:45 +11:00
|
|
|
if (!(value is SafeLazy.ExceptionHolder eh))
|
2019-01-17 11:01:23 +01:00
|
|
|
return value;
|
|
|
|
|
|
|
|
|
|
// and... it's in the cache anyway - so contrary to other cache providers,
|
|
|
|
|
// which would trick with GetSafeLazyValue, we need to remove by ourselves,
|
|
|
|
|
// in order NOT to cache exceptions
|
|
|
|
|
|
2019-11-08 14:51:20 +11:00
|
|
|
_items.TryRemove(cacheKey, out result);
|
2019-01-17 11:01:23 +01:00
|
|
|
eh.Exception.Throw(); // throw once!
|
|
|
|
|
return null; // never reached
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2021-12-16 13:44:20 +01:00
|
|
|
public IEnumerable<object?> SearchByKey(string keyStartsWith)
|
2019-01-17 11:01:23 +01:00
|
|
|
{
|
2019-11-08 14:51:20 +11:00
|
|
|
return _items
|
2019-01-17 11:01:23 +01:00
|
|
|
.Where(kvp => kvp.Key.InvariantStartsWith(keyStartsWith))
|
2019-11-07 19:16:45 +11:00
|
|
|
.Select(kvp => SafeLazy.GetSafeLazyValue(kvp.Value))
|
2019-01-17 11:01:23 +01:00
|
|
|
.Where(x => x != null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2021-12-16 13:44:20 +01:00
|
|
|
public IEnumerable<object?> SearchByRegex(string regex)
|
2019-01-17 11:01:23 +01:00
|
|
|
{
|
|
|
|
|
var compiled = new Regex(regex, RegexOptions.Compiled);
|
2019-11-08 14:51:20 +11:00
|
|
|
return _items
|
2019-01-17 11:01:23 +01:00
|
|
|
.Where(kvp => compiled.IsMatch(kvp.Key))
|
2019-11-07 19:16:45 +11:00
|
|
|
.Select(kvp => SafeLazy.GetSafeLazyValue(kvp.Value))
|
2019-01-17 11:01:23 +01:00
|
|
|
.Where(x => x != null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public void Clear()
|
|
|
|
|
{
|
2019-11-08 14:51:20 +11:00
|
|
|
_items.Clear();
|
2019-01-17 11:01:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public void Clear(string key)
|
|
|
|
|
{
|
2019-11-08 14:51:20 +11:00
|
|
|
_items.TryRemove(key, out _);
|
2019-01-17 11:01:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2020-03-19 08:53:18 +01:00
|
|
|
public void ClearOfType(Type type)
|
2019-01-17 11:01:23 +01:00
|
|
|
{
|
|
|
|
|
if (type == null) return;
|
|
|
|
|
var isInterface = type.IsInterface;
|
|
|
|
|
|
2019-11-08 14:51:20 +11:00
|
|
|
foreach (var kvp in _items
|
2019-01-17 11:01:23 +01:00
|
|
|
.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(x.Value, true);
|
2019-01-17 11:01:23 +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));
|
|
|
|
|
}))
|
2019-11-08 14:51:20 +11:00
|
|
|
_items.TryRemove(kvp.Key, out _);
|
2019-01-17 11:01:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public void ClearOfType<T>()
|
|
|
|
|
{
|
|
|
|
|
var typeOfT = typeof(T);
|
|
|
|
|
var isInterface = typeOfT.IsInterface;
|
|
|
|
|
|
2019-11-08 14:51:20 +11:00
|
|
|
foreach (var kvp in _items
|
2019-01-17 11:01:23 +01:00
|
|
|
.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(x.Value, true);
|
2019-01-17 11:01:23 +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));
|
|
|
|
|
}))
|
2019-11-08 14:51:20 +11:00
|
|
|
_items.TryRemove(kvp.Key, out _);
|
2019-01-17 11:01:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public void ClearOfType<T>(Func<string, T, bool> predicate)
|
|
|
|
|
{
|
|
|
|
|
var typeOfT = typeof(T);
|
|
|
|
|
var isInterface = typeOfT.IsInterface;
|
|
|
|
|
|
2019-11-08 14:51:20 +11:00
|
|
|
foreach (var kvp in _items
|
2019-01-17 11:01:23 +01:00
|
|
|
.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(x.Value, true);
|
2019-01-17 11:01:23 +01: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(x.Key, (T)value);
|
|
|
|
|
}))
|
2019-11-08 14:51:20 +11:00
|
|
|
_items.TryRemove(kvp.Key, out _);
|
2019-01-17 11:01:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public void ClearByKey(string keyStartsWith)
|
|
|
|
|
{
|
2019-11-08 14:51:20 +11:00
|
|
|
foreach (var ikvp in _items
|
2019-01-17 11:01:23 +01:00
|
|
|
.Where(kvp => kvp.Key.InvariantStartsWith(keyStartsWith)))
|
2019-11-08 14:51:20 +11:00
|
|
|
_items.TryRemove(ikvp.Key, out _);
|
2019-01-17 11:01:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public void ClearByRegex(string regex)
|
|
|
|
|
{
|
|
|
|
|
var compiled = new Regex(regex, RegexOptions.Compiled);
|
2019-11-08 14:51:20 +11:00
|
|
|
foreach (var ikvp in _items
|
2019-01-17 11:01:23 +01:00
|
|
|
.Where(kvp => compiled.IsMatch(kvp.Key)))
|
2019-11-08 14:51:20 +11:00
|
|
|
_items.TryRemove(ikvp.Key, out _);
|
2019-01-17 11:01:23 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|