Refactor System.Runtime.Caching to Microsoft.Extensions.Caching.Memory
This commit is contained in:
committed by
Bjarke Berg
parent
ace5c54da2
commit
05921ee6cc
@@ -1,5 +1,7 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Microsoft.Extensions.Options;
|
||||
@@ -13,6 +15,7 @@ namespace Umbraco.Cms.Core.Cache;
|
||||
public class ObjectCacheAppCache : IAppPolicyCache, IDisposable
|
||||
{
|
||||
private readonly IOptions<MemoryCacheOptions> _options;
|
||||
private readonly IHostEnvironment? _hostEnvironment;
|
||||
private readonly ISet<string> _keys = new HashSet<string>();
|
||||
private readonly ReaderWriterLockSlim _locker = new(LockRecursionPolicy.SupportsRecursion);
|
||||
private bool _disposedValue;
|
||||
@@ -29,7 +32,7 @@ public class ObjectCacheAppCache : IAppPolicyCache, IDisposable
|
||||
/// Initializes a new instance of the <see cref="ObjectCacheAppCache" />.
|
||||
/// </summary>
|
||||
public ObjectCacheAppCache()
|
||||
: this(Options.Create(new MemoryCacheOptions()), NullLoggerFactory.Instance)
|
||||
: this(Options.Create(new MemoryCacheOptions()), NullLoggerFactory.Instance, null)
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
@@ -37,9 +40,11 @@ public class ObjectCacheAppCache : IAppPolicyCache, IDisposable
|
||||
/// </summary>
|
||||
/// <param name="options">The options.</param>
|
||||
/// <param name="loggerFactory">The logger factory.</param>
|
||||
public ObjectCacheAppCache(IOptions<MemoryCacheOptions> options, ILoggerFactory loggerFactory)
|
||||
/// <param name="hostEnvironment">The host environment.</param>
|
||||
public ObjectCacheAppCache(IOptions<MemoryCacheOptions> options, ILoggerFactory loggerFactory, IHostEnvironment? hostEnvironment)
|
||||
{
|
||||
_options = options;
|
||||
_hostEnvironment = hostEnvironment;
|
||||
|
||||
MemoryCache = new MemoryCache(_options, loggerFactory);
|
||||
}
|
||||
@@ -101,7 +106,7 @@ public class ObjectCacheAppCache : IAppPolicyCache, IDisposable
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public object? Get(string key, Func<object?> factory, TimeSpan? timeout, bool isSliding = false)
|
||||
public object? Get(string key, Func<object?> factory, TimeSpan? timeout, bool isSliding = false, string[]? dependentFiles = null)
|
||||
{
|
||||
// see notes in HttpRuntimeAppCache
|
||||
Lazy<object?>? result;
|
||||
@@ -116,7 +121,7 @@ public class ObjectCacheAppCache : IAppPolicyCache, IDisposable
|
||||
if (result == null || SafeLazy.GetSafeLazyValue(result, true) == null)
|
||||
{
|
||||
result = SafeLazy.GetSafeLazy(factory);
|
||||
MemoryCacheEntryOptions options = GetOptions(timeout, isSliding);
|
||||
MemoryCacheEntryOptions options = GetOptions(timeout, isSliding, dependentFiles);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -154,7 +159,7 @@ public class ObjectCacheAppCache : IAppPolicyCache, IDisposable
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Insert(string key, Func<object?> factory, TimeSpan? timeout = null, bool isSliding = false)
|
||||
public void Insert(string key, Func<object?> factory, TimeSpan? timeout = null, bool isSliding = false, string[]? dependentFiles = null)
|
||||
{
|
||||
// NOTE - here also we must insert a Lazy<object> but we can evaluate it right now
|
||||
// and make sure we don't store a null value.
|
||||
@@ -165,7 +170,7 @@ public class ObjectCacheAppCache : IAppPolicyCache, IDisposable
|
||||
return; // do not store null values (backward compat)
|
||||
}
|
||||
|
||||
MemoryCacheEntryOptions options = GetOptions(timeout, isSliding);
|
||||
MemoryCacheEntryOptions options = GetOptions(timeout, isSliding, dependentFiles);
|
||||
|
||||
// NOTE: This does an add or update
|
||||
MemoryCache.Set(key, result, options);
|
||||
@@ -318,7 +323,7 @@ public class ObjectCacheAppCache : IAppPolicyCache, IDisposable
|
||||
}
|
||||
}
|
||||
|
||||
private MemoryCacheEntryOptions GetOptions(TimeSpan? timeout, bool isSliding)
|
||||
private MemoryCacheEntryOptions GetOptions(TimeSpan? timeout = null, bool isSliding = false, string[]? dependentFiles = null)
|
||||
{
|
||||
var options = new MemoryCacheEntryOptions();
|
||||
|
||||
@@ -332,6 +337,19 @@ public class ObjectCacheAppCache : IAppPolicyCache, IDisposable
|
||||
options.AbsoluteExpirationRelativeToNow = timeout;
|
||||
}
|
||||
|
||||
// Configure file based expiration
|
||||
if (dependentFiles?.Length > 0 && _hostEnvironment?.ContentRootFileProvider is IFileProvider fileProvider)
|
||||
{
|
||||
foreach (var dependentFile in dependentFiles)
|
||||
{
|
||||
var relativePath = Path.IsPathFullyQualified(dependentFile)
|
||||
? Path.GetRelativePath(_hostEnvironment.ContentRootPath, dependentFile)
|
||||
: dependentFile;
|
||||
|
||||
options.ExpirationTokens.Add(fileProvider.Watch(relativePath));
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure key is removed from set when evicted from cache
|
||||
return options.RegisterPostEvictionCallback((key, _, _, _) => _keys.Remove((string)key));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user