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

112 lines
3.4 KiB
C#
Raw Normal View History

2016-05-27 14:26:28 +02:00
using System;
using System.Collections;
2016-05-27 14:26:28 +02:00
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Umbraco.Extensions;
2016-05-27 14:26:28 +02:00
namespace Umbraco.Cms.Core.Cache
2016-05-27 14:26:28 +02:00
{
2019-01-17 11:01:23 +01:00
/// <summary>
/// Implements <see cref="IAppCache"/> on top of a concurrent dictionary.
/// </summary>
public class DictionaryAppCache : IRequestCache
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>
private readonly ConcurrentDictionary<string, object?> _items = new ConcurrentDictionary<string, object?>();
2019-11-07 18:57:40 +11:00
public int Count => _items.Count;
2016-05-27 14:26:28 +02:00
/// <inheritdoc />
public bool IsAvailable => true;
2019-01-17 11:01:23 +01:00
/// <inheritdoc />
public virtual object? Get(string key)
2016-05-27 14:26:28 +02:00
{
2019-11-07 18:57:40 +11: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-11-07 18:57:40 +11:00
return _items.GetOrAdd(key, _ => factory());
2016-05-27 14:26:28 +02:00
}
public bool Set(string key, object value) => _items.TryAdd(key, value);
public bool Remove(string key) => _items.TryRemove(key, out _);
2019-01-17 11:01:23 +01:00
/// <inheritdoc />
public virtual IEnumerable<object?> SearchByKey(string keyStartsWith)
2016-05-27 14:26:28 +02:00
{
var items = new List<object?>();
2019-11-07 18:57:40 +11:00
foreach (var (key, value) in _items)
2019-01-17 11:01:23 +01:00
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?>();
2019-11-07 18:57:40 +11:00
foreach (var (key, value) in _items)
2019-01-17 11:01:23 +01:00
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-11-07 18:57:40 +11: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-11-07 18:57:40 +11: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(Type type)
2016-05-27 14:26:28 +02:00
{
_items.RemoveAll(kvp => kvp.Value != null && kvp.Value.GetType() == type);
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);
ClearOfType(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);
2019-11-07 18:57:40 +11:00
_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-11-07 18:57:40 +11: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);
2019-11-07 18:57:40 +11:00
_items.RemoveAll(kvp => compiled.IsMatch(kvp.Key));
2016-05-27 14:26:28 +02:00
}
public IEnumerator<KeyValuePair<string, object?>> GetEnumerator() => _items.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
2016-05-27 14:26:28 +02:00
}
}