2016-05-27 14:26:28 +02:00
|
|
|
|
using System;
|
2020-02-13 07:46:49 +01:00
|
|
|
|
using System.Collections;
|
2016-05-27 14:26:28 +02:00
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Text.RegularExpressions;
|
2021-02-18 11:06:02 +01:00
|
|
|
|
using Umbraco.Extensions;
|
2016-05-27 14:26:28 +02:00
|
|
|
|
|
2021-02-18 11:06:02 +01: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>
|
2019-11-27 14:10:48 +01:00
|
|
|
|
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>
|
2021-12-16 13:44:20 +01:00
|
|
|
|
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
|
|
|
|
|
2019-12-04 13:30:46 +11:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public bool IsAvailable => true;
|
|
|
|
|
|
|
2019-01-17 11:01:23 +01:00
|
|
|
|
/// <inheritdoc />
|
2021-12-16 13:44:20 +01:00
|
|
|
|
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 />
|
2021-12-16 13:44:20 +01:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
2019-11-27 14:10:48 +01: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 />
|
2021-12-16 13:44:20 +01:00
|
|
|
|
public virtual IEnumerable<object?> SearchByKey(string keyStartsWith)
|
2016-05-27 14:26:28 +02:00
|
|
|
|
{
|
2021-12-16 13:44:20 +01: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 />
|
2021-12-16 13:44:20 +01:00
|
|
|
|
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);
|
2021-12-16 13:44:20 +01: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 (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 />
|
2020-03-19 08:53:18 +01:00
|
|
|
|
public virtual void ClearOfType(Type type)
|
2016-05-27 14:26:28 +02:00
|
|
|
|
{
|
2020-03-19 08:53:18 +01: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);
|
2020-03-19 08:53:18 +01:00
|
|
|
|
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
|
|
|
|
}
|
2020-02-13 07:46:49 +01:00
|
|
|
|
|
2021-12-16 13:44:20 +01:00
|
|
|
|
public IEnumerator<KeyValuePair<string, object?>> GetEnumerator() => _items.GetEnumerator();
|
2020-02-13 07:46:49 +01:00
|
|
|
|
|
|
|
|
|
|
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
2016-05-27 14:26:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|