* Run code cleanup * Start manual cleanup after dotnet format * Finish up manual pass * Fix up missed warnings * Fix after merge * Update src/Umbraco.Core/Cache/ContentTypeCacheRefresher.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Cache/DataTypeCacheRefresher.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Cache/DeepCloneAppCache.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Cache/DomainCacheRefresher.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Cache/FastDictionaryAppCacheBase.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Cache/FastDictionaryAppCacheBase.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Cache/FastDictionaryAppCacheBase.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Cache/FastDictionaryAppCacheBase.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Cache/FastDictionaryAppCacheBase.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Cache/MacroCacheRefresher.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Cache/MediaCacheRefresher.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Cache/MemberCacheRefresher.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Cache/MemberGroupCacheRefresher.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Composing/OrderedCollectionBuilderBase.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Configuration/Models/RequestHandlerSettings.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Composing/SetCollectionBuilderBase.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Composing/WeightedCollectionBuilderBase.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/ContentApps/ContentAppFactoryCollectionBuilder.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Dashboards/DashboardCollectionBuilder.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/StringExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/StringExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/StringExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/StringExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/StringExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/StringExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/StringExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/ContentTypeRefreshedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/ContentTypeSavedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/ContentTypeSavingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/DictionaryCacheRefresherNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/DataTypeCacheRefresherNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/ContentUnpublishingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/DictionaryItemDeletingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/DictionaryItemSavedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/DictionaryItemSavingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/DomainCacheRefresherNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/LanguageCacheRefresherNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MacroCacheRefresherNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaCacheRefresherNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaEmptiedRecycleBinNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaEmptyingRecycleBinNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaMovedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaMovedToRecycleBinNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/StringExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/StringExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/StringExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/StringExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/StringExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/StringExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaMovedToRecycleBinNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaMovingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaMovingToRecycleBinNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaTreeChangeNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaTreeChangeNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaTypeChangedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaTypeDeletingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaTypeMovedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaTypeMovingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaTypeMovingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MediaTypeRefreshedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MemberCacheRefresherNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MemberGroupCacheRefresherNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MemberGroupDeletingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MemberGroupSavedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MemberGroupSavingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MemberTypeChangedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MemberTypeDeletedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MemberTypeDeletingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MemberTypeMovedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MemberTypeMovedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MemberTypeMovingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MemberTypeMovingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MemberTypeRefreshedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MemberTypeSavingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/StringExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/UriExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MovedToRecycleBinNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MovedToRecycleBinNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MovingToRecycleBinNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/MovingToRecycleBinNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/PartialViewDeletingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Extensions/UriExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/IO/PhysicalFileSystem.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/IO/PhysicalFileSystem.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/IO/ShadowWrapper.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Logging/DisposableTimer.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Install/InstallSteps/UpgradeStep.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Mapping/MapDefinitionCollectionBuilder.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Media/EmbedProviders/EmbedProvidersCollectionBuilder.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/PartialViewSavedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Models/ContentEditing/ContentVariationDisplay.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/PartialViewSavingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/PublicAccessCacheRefresherNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/PublicAccessEntryDeletedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/PublicAccessEntryDeletingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/PublicAccessEntryDeletingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/PublicAccessEntrySavedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/PublicAccessEntrySavingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/PublicAccessEntrySavingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/RelationTypeCacheRefresherNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/RelationTypeDeletingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/RelationTypeSavedNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/RelationTypeSavingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/StatefulNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/StylesheetDeletingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Notifications/StylesheetSavingNotification.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Models/DeepCloneHelper.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Models/File.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Models/PropertyGroupExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Update src/Umbraco.Core/Models/PropertyGroupExtensions.cs Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> * Fix based on review * Fix after merge Signed-off-by: Zeegaan <nge@umbraco.dk> Co-authored-by: Nikolaj Geisle <niko737@edu.ucl.dk> Co-authored-by: Mole <nikolajlauridsen@protonmail.ch> Co-authored-by: Zeegaan <nge@umbraco.dk>
291 lines
9.3 KiB
C#
291 lines
9.3 KiB
C#
using System.Text.RegularExpressions;
|
|
using Umbraco.Extensions;
|
|
|
|
namespace Umbraco.Cms.Core.Cache;
|
|
|
|
/// <summary>
|
|
/// Provides a base class to fast, dictionary-based <see cref="IAppCache" /> implementations.
|
|
/// </summary>
|
|
public abstract class FastDictionaryAppCacheBase : IAppCache
|
|
{
|
|
// prefix cache keys so we know which one are ours
|
|
protected const string CacheItemPrefix = "umbrtmche";
|
|
|
|
#region IAppCache
|
|
|
|
/// <inheritdoc />
|
|
public virtual object? Get(string key)
|
|
{
|
|
key = GetCacheKey(key);
|
|
Lazy<object?>? result;
|
|
try
|
|
{
|
|
EnterReadLock();
|
|
result = GetEntry(key) as Lazy<object?>; // null if key not found
|
|
}
|
|
finally
|
|
{
|
|
ExitReadLock();
|
|
}
|
|
|
|
return result == null ? null : SafeLazy.GetSafeLazyValue(result); // return exceptions as null
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public abstract object? Get(string key, Func<object?> factory);
|
|
|
|
/// <inheritdoc />
|
|
public virtual IEnumerable<object> SearchByKey(string keyStartsWith)
|
|
{
|
|
var plen = CacheItemPrefix.Length + 1;
|
|
IEnumerable<KeyValuePair<object, object>> entries;
|
|
try
|
|
{
|
|
EnterReadLock();
|
|
entries = GetDictionaryEntries()
|
|
.Where(x => ((string)x.Key).Substring(plen).InvariantStartsWith(keyStartsWith))
|
|
.ToArray(); // evaluate while locked
|
|
}
|
|
finally
|
|
{
|
|
ExitReadLock();
|
|
}
|
|
|
|
return entries
|
|
.Select(x => SafeLazy.GetSafeLazyValue((Lazy<object?>)x.Value)) // return exceptions as null
|
|
.Where(x => x != null)!; // backward compat, don't store null values in the cache
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public virtual IEnumerable<object?> SearchByRegex(string regex)
|
|
{
|
|
const string prefix = CacheItemPrefix + "-";
|
|
var compiled = new Regex(regex, RegexOptions.Compiled);
|
|
var plen = prefix.Length;
|
|
IEnumerable<KeyValuePair<object, object>> entries;
|
|
try
|
|
{
|
|
EnterReadLock();
|
|
entries = GetDictionaryEntries()
|
|
.Where(x => compiled.IsMatch(((string)x.Key).Substring(plen)))
|
|
.ToArray(); // evaluate while locked
|
|
}
|
|
finally
|
|
{
|
|
ExitReadLock();
|
|
}
|
|
|
|
return entries
|
|
.Select(x => SafeLazy.GetSafeLazyValue((Lazy<object?>)x.Value)) // return exceptions as null
|
|
.Where(x => x != null); // backward compatible, don't store null values in the cache
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public virtual void Clear()
|
|
{
|
|
try
|
|
{
|
|
EnterWriteLock();
|
|
foreach (KeyValuePair<object, object> entry in GetDictionaryEntries().ToArray())
|
|
{
|
|
RemoveEntry((string)entry.Key);
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
ExitWriteLock();
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public virtual void Clear(string key)
|
|
{
|
|
var cacheKey = GetCacheKey(key);
|
|
try
|
|
{
|
|
EnterWriteLock();
|
|
RemoveEntry(cacheKey);
|
|
}
|
|
finally
|
|
{
|
|
ExitWriteLock();
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public virtual void ClearOfType(Type? type)
|
|
{
|
|
if (type == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var isInterface = type.IsInterface;
|
|
try
|
|
{
|
|
EnterWriteLock();
|
|
foreach (KeyValuePair<object, object> entry in GetDictionaryEntries()
|
|
.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 = SafeLazy.GetSafeLazyValue((Lazy<object?>)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);
|
|
})
|
|
.ToArray())
|
|
{
|
|
RemoveEntry((string)entry.Key);
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
ExitWriteLock();
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public virtual void ClearOfType<T>()
|
|
{
|
|
Type typeOfT = typeof(T);
|
|
var isInterface = typeOfT.IsInterface;
|
|
try
|
|
{
|
|
EnterWriteLock();
|
|
foreach (KeyValuePair<object, object> entry in GetDictionaryEntries()
|
|
.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 = SafeLazy.GetSafeLazyValue((Lazy<object?>)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);
|
|
})
|
|
.ToArray())
|
|
{
|
|
RemoveEntry((string)entry.Key);
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
ExitWriteLock();
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public virtual void ClearOfType<T>(Func<string, T, bool> predicate)
|
|
{
|
|
Type typeOfT = typeof(T);
|
|
var isInterface = typeOfT.IsInterface;
|
|
var plen = CacheItemPrefix.Length + 1;
|
|
try
|
|
{
|
|
EnterWriteLock();
|
|
foreach (KeyValuePair<object, object> entry in GetDictionaryEntries()
|
|
.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 = SafeLazy.GetSafeLazyValue((Lazy<object?>)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(((string) x.Key).Substring(plen), (T) value);
|
|
}))
|
|
{
|
|
RemoveEntry((string)entry.Key);
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
ExitWriteLock();
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public virtual void ClearByKey(string keyStartsWith)
|
|
{
|
|
var plen = CacheItemPrefix.Length + 1;
|
|
try
|
|
{
|
|
EnterWriteLock();
|
|
foreach (KeyValuePair<object, object> entry in GetDictionaryEntries()
|
|
.Where(x => ((string)x.Key).Substring(plen).InvariantStartsWith(keyStartsWith))
|
|
.ToArray())
|
|
{
|
|
RemoveEntry((string)entry.Key);
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
ExitWriteLock();
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public virtual void ClearByRegex(string regex)
|
|
{
|
|
var compiled = new Regex(regex, RegexOptions.Compiled);
|
|
var plen = CacheItemPrefix.Length + 1;
|
|
try
|
|
{
|
|
EnterWriteLock();
|
|
foreach (KeyValuePair<object, object> entry in GetDictionaryEntries()
|
|
.Where(x => compiled.IsMatch(((string)x.Key).Substring(plen)))
|
|
.ToArray())
|
|
{
|
|
RemoveEntry((string)entry.Key);
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
ExitWriteLock();
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Dictionary
|
|
|
|
// manipulate the underlying cache entries
|
|
// these *must* be called from within the appropriate locks
|
|
// and use the full prefixed cache keys
|
|
protected abstract IEnumerable<KeyValuePair<object, object>> GetDictionaryEntries();
|
|
|
|
protected abstract void RemoveEntry(string key);
|
|
|
|
protected abstract object? GetEntry(string key);
|
|
|
|
// read-write lock the underlying cache
|
|
// protected abstract IDisposable ReadLock { get; }
|
|
// protected abstract IDisposable WriteLock { get; }
|
|
protected abstract void EnterReadLock();
|
|
|
|
protected abstract void ExitReadLock();
|
|
|
|
protected abstract void EnterWriteLock();
|
|
|
|
protected abstract void ExitWriteLock();
|
|
|
|
protected string GetCacheKey(string key) => $"{CacheItemPrefix}-{key}";
|
|
|
|
#endregion
|
|
}
|