Files
Umbraco-CMS/src/Umbraco.Core/Cache/DictionaryCacheProvider.cs

148 lines
5.9 KiB
C#
Raw Normal View History

2016-05-27 14:26:28 +02:00
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
2017-05-30 15:56:27 +02:00
using Umbraco.Core.Composing;
2016-05-27 14:26:28 +02:00
namespace Umbraco.Core.Cache
{
2017-09-29 15:51:33 +02:00
internal class DictionaryCacheProvider : ICacheProvider
2016-05-27 14:26:28 +02:00
{
private readonly ConcurrentDictionary<string, Lazy<object>> _items
= new ConcurrentDictionary<string, Lazy<object>>();
2017-09-29 15:51:33 +02:00
// for tests
internal ConcurrentDictionary<string, Lazy<object>> Items => _items;
2016-05-27 14:26:28 +02:00
public void ClearAllCache()
{
_items.Clear();
}
public void ClearCacheItem(string key)
{
2017-09-29 15:51:33 +02:00
_items.TryRemove(key, out _);
2016-05-27 14:26:28 +02:00
}
public void ClearCacheObjectTypes(string typeName)
{
var type = TypeFinder.GetTypeByName(typeName);
if (type == null) return;
var isInterface = type.IsInterface;
foreach (var kvp in _items
.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
var value = DictionaryCacheProviderBase.GetSafeLazyValue(x.Value, true);
// 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));
}))
2017-09-29 15:51:33 +02:00
_items.TryRemove(kvp.Key, out _);
2016-05-27 14:26:28 +02:00
}
public void ClearCacheObjectTypes<T>()
{
var typeOfT = typeof(T);
var isInterface = typeOfT.IsInterface;
foreach (var kvp in _items
.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
var value = DictionaryCacheProviderBase.GetSafeLazyValue(x.Value, true);
// 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));
}))
2017-09-29 15:51:33 +02:00
_items.TryRemove(kvp.Key, out _);
2016-05-27 14:26:28 +02:00
}
public void ClearCacheObjectTypes<T>(Func<string, T, bool> predicate)
{
var typeOfT = typeof(T);
var isInterface = typeOfT.IsInterface;
foreach (var kvp in _items
.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
var value = DictionaryCacheProviderBase.GetSafeLazyValue(x.Value, true);
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);
}))
2017-09-29 15:51:33 +02:00
_items.TryRemove(kvp.Key, out _);
2016-05-27 14:26:28 +02:00
}
public void ClearCacheByKeySearch(string keyStartsWith)
{
foreach (var ikvp in _items
.Where(kvp => kvp.Key.InvariantStartsWith(keyStartsWith)))
2017-09-29 15:51:33 +02:00
_items.TryRemove(ikvp.Key, out _);
2016-05-27 14:26:28 +02:00
}
public void ClearCacheByKeyExpression(string regexString)
{
foreach (var ikvp in _items
.Where(kvp => Regex.IsMatch(kvp.Key, regexString)))
2017-09-29 15:51:33 +02:00
_items.TryRemove(ikvp.Key, out _);
2016-05-27 14:26:28 +02:00
}
public IEnumerable<object> GetCacheItemsByKeySearch(string keyStartsWith)
{
return _items
.Where(kvp => kvp.Key.InvariantStartsWith(keyStartsWith))
.Select(kvp => DictionaryCacheProviderBase.GetSafeLazyValue(kvp.Value))
.Where(x => x != null);
}
public IEnumerable<object> GetCacheItemsByKeyExpression(string regexString)
{
return _items
.Where(kvp => Regex.IsMatch(kvp.Key, regexString))
.Select(kvp => DictionaryCacheProviderBase.GetSafeLazyValue(kvp.Value))
.Where(x => x != null);
}
public object GetCacheItem(string cacheKey)
{
2017-09-29 15:51:33 +02:00
_items.TryGetValue(cacheKey, out var result); // else null
2016-05-27 14:26:28 +02:00
return result == null ? null : DictionaryCacheProviderBase.GetSafeLazyValue(result); // return exceptions as null
}
public object GetCacheItem(string cacheKey, Func<object> getCacheItem)
{
var result = _items.GetOrAdd(cacheKey, k => DictionaryCacheProviderBase.GetSafeLazy(getCacheItem));
var value = result.Value; // will not throw (safe lazy)
2017-09-29 15:51:33 +02:00
if (!(value is DictionaryCacheProviderBase.ExceptionHolder eh))
2016-05-27 14:26:28 +02: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
_items.TryRemove(cacheKey, out result);
2018-01-26 17:55:20 +01:00
eh.Exception.Throw(); // throw once!
return null; // never reached
2016-05-27 14:26:28 +02:00
}
}
}