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

97 lines
2.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.Text.RegularExpressions;
namespace Umbraco.Core.Cache
{
2019-01-17 11:01:23 +01:00
/// <summary>
/// Implements <see cref="IAppCache"/> on top of a concurrent dictionary.
/// </summary>
2019-01-18 08:29:16 +01:00
public class DictionaryAppCache : IAppCache
2016-05-27 14:26:28 +02:00
{
2019-01-17 11:01:23 +01:00
/// <summary>
/// Gets the internal items dictionary, for tests only!
/// </summary>
internal readonly ConcurrentDictionary<string, object> Items = new ConcurrentDictionary<string, object>();
2016-05-27 14:26:28 +02:00
2019-01-17 11:01:23 +01:00
/// <inheritdoc />
public virtual object Get(string key)
2016-05-27 14:26:28 +02:00
{
2019-01-21 15:39:19 +01:00
return Items.TryGetValue(key, out var value) ? value : null;
2016-05-27 14:26:28 +02:00
}
2019-01-17 11:01:23 +01:00
/// <inheritdoc />
public virtual object Get(string key, Func<object> factory)
2016-05-27 14:26:28 +02:00
{
2019-01-17 11:01:23 +01:00
return Items.GetOrAdd(key, _ => factory());
2016-05-27 14:26:28 +02:00
}
2019-01-17 11:01:23 +01:00
/// <inheritdoc />
public virtual IEnumerable<object> SearchByKey(string keyStartsWith)
2016-05-27 14:26:28 +02:00
{
2019-01-17 11:01:23 +01:00
var items = new List<object>();
foreach (var (key, value) in Items)
if (key.InvariantStartsWith(keyStartsWith))
items.Add(value);
return items;
2016-05-27 14:26:28 +02:00
}
2019-01-17 11:01:23 +01:00
/// <inheritdoc />
public IEnumerable<object> SearchByRegex(string regex)
2016-05-27 14:26:28 +02:00
{
2019-01-17 11:01:23 +01:00
var compiled = new Regex(regex, RegexOptions.Compiled);
var items = new List<object>();
foreach (var (key, value) in Items)
if (compiled.IsMatch(key))
items.Add(value);
return items;
2016-05-27 14:26:28 +02:00
}
2019-01-17 11:01:23 +01:00
/// <inheritdoc />
public virtual void Clear()
2016-05-27 14:26:28 +02:00
{
2019-01-17 11:01:23 +01:00
Items.Clear();
2016-05-27 14:26:28 +02:00
}
2019-01-17 11:01:23 +01:00
/// <inheritdoc />
public virtual void Clear(string key)
2016-05-27 14:26:28 +02:00
{
2019-01-17 11:01:23 +01:00
Items.TryRemove(key, out _);
2016-05-27 14:26:28 +02:00
}
2019-01-17 11:01:23 +01:00
/// <inheritdoc />
public virtual void ClearOfType(string typeName)
2016-05-27 14:26:28 +02:00
{
2019-01-17 11:01:23 +01:00
Items.RemoveAll(kvp => kvp.Value != null && kvp.Value.GetType().ToString().InvariantEquals(typeName));
2016-05-27 14:26:28 +02:00
}
2019-01-17 11:01:23 +01:00
/// <inheritdoc />
public virtual void ClearOfType<T>()
2016-05-27 14:26:28 +02:00
{
2019-01-17 11:01:23 +01:00
var typeOfT = typeof(T);
Items.RemoveAll(kvp => kvp.Value != null && kvp.Value.GetType() == typeOfT);
2016-05-27 14:26:28 +02:00
}
2019-01-17 11:01:23 +01:00
/// <inheritdoc />
public virtual void ClearOfType<T>(Func<string, T, bool> predicate)
2016-05-27 14:26:28 +02:00
{
2019-01-17 11:01:23 +01:00
var typeOfT = typeof(T);
Items.RemoveAll(kvp => kvp.Value != null && kvp.Value.GetType() == typeOfT && predicate(kvp.Key, (T)kvp.Value));
2016-05-27 14:26:28 +02:00
}
2019-01-17 11:01:23 +01:00
/// <inheritdoc />
public virtual void ClearByKey(string keyStartsWith)
2016-05-27 14:26:28 +02:00
{
2019-01-17 11:01:23 +01:00
Items.RemoveAll(kvp => kvp.Key.InvariantStartsWith(keyStartsWith));
2016-05-27 14:26:28 +02:00
}
2019-01-17 11:01:23 +01:00
/// <inheritdoc />
public virtual void ClearByRegex(string regex)
2016-05-27 14:26:28 +02:00
{
2019-01-17 11:01:23 +01:00
var compiled = new Regex(regex, RegexOptions.Compiled);
Items.RemoveAll(kvp => compiled.IsMatch(kvp.Key));
2016-05-27 14:26:28 +02:00
}
}
}