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));
|
||||
}
|
||||
|
||||
@@ -17,6 +17,19 @@
|
||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options.DataAnnotations" />
|
||||
<PackageReference Include="System.Runtime.Caching" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0-rc.2.*" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0-rc.2.*" />
|
||||
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="8.0.0-rc.2.*" />
|
||||
<PackageReference Include="Microsoft.Extensions.FileProviders.Physical" Version="8.0.0-rc.2.*" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0-rc.2.*" />
|
||||
<PackageReference Include="Microsoft.Extensions.Identity.Core" Version="8.0.0-rc.2.*" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0-rc.2.*" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.0-rc.2.*" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="8.0.0-rc.2.*" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options.DataAnnotations" Version="8.0.0-rc.2.*" />
|
||||
<PackageReference Include="System.Runtime.Caching" Version="8.0.0-rc.2.*" />
|
||||
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
|
||||
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.7.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
using System.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using OpenIddict.Abstractions;
|
||||
@@ -41,8 +40,7 @@ public class OpenIddictCleanup : RecurringHostedServiceBase
|
||||
|
||||
try
|
||||
{
|
||||
IOpenIddictTokenManager tokenManager = scope.ServiceProvider.GetService<IOpenIddictTokenManager>()
|
||||
?? throw new ConfigurationErrorsException($"Could not retrieve an {nameof(IOpenIddictTokenManager)} service from the current scope");
|
||||
IOpenIddictTokenManager tokenManager = scope.ServiceProvider.GetRequiredService<IOpenIddictTokenManager>();
|
||||
await tokenManager.PruneAsync(threshold);
|
||||
}
|
||||
catch (Exception exception)
|
||||
@@ -52,8 +50,7 @@ public class OpenIddictCleanup : RecurringHostedServiceBase
|
||||
|
||||
try
|
||||
{
|
||||
IOpenIddictAuthorizationManager authorizationManager = scope.ServiceProvider.GetService<IOpenIddictAuthorizationManager>()
|
||||
?? throw new ConfigurationErrorsException($"Could not retrieve an {nameof(IOpenIddictAuthorizationManager)} service from the current scope");
|
||||
IOpenIddictAuthorizationManager authorizationManager = scope.ServiceProvider.GetRequiredService<IOpenIddictAuthorizationManager>();
|
||||
await authorizationManager.PruneAsync(threshold);
|
||||
}
|
||||
catch (Exception exception)
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
// See LICENSE for more details.
|
||||
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Cms.Core.Cache;
|
||||
using Umbraco.Cms.Core.Collections;
|
||||
@@ -27,7 +26,7 @@ public class DeepCloneAppCacheTests : RuntimeAppCacheTests
|
||||
private DeepCloneAppCache _provider;
|
||||
private ObjectCacheAppCache _memberCache;
|
||||
|
||||
protected override int GetTotalItemCount => _memberCache.MemoryCache.Count();
|
||||
protected override int GetTotalItemCount => _memberCache.MemoryCache.Count;
|
||||
|
||||
internal override IAppCache AppCache => _provider;
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
// Copyright (c) Umbraco.
|
||||
// See LICENSE for more details.
|
||||
|
||||
using System.Linq;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Cms.Core.Cache;
|
||||
|
||||
@@ -18,7 +17,7 @@ public class ObjectAppCacheTests : RuntimeAppCacheTests
|
||||
|
||||
private ObjectCacheAppCache _provider;
|
||||
|
||||
protected override int GetTotalItemCount => _provider.MemoryCache.Count();
|
||||
protected override int GetTotalItemCount => _provider.MemoryCache.Count;
|
||||
|
||||
internal override IAppCache AppCache => _provider;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user