🚸🎨 Caching中增加校验警告,如果意外注册IMemoryCache
Some checks are pending
CodeQL / Analyze (csharp) (push) Waiting to run
CodeQL / Analyze (javascript) (push) Waiting to run
.NET Core / build (push) Waiting to run

This commit is contained in:
LemonNoCry 2024-08-02 10:08:39 +08:00
parent 64c2e8c0e4
commit 288eedf0b4
No known key found for this signature in database
4 changed files with 27 additions and 16 deletions

View File

@ -1,12 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Concurrent;
using System.Text;
using System.Threading.Tasks;
using Blog.Core.Common.Caches.Interface;
using Blog.Core.Common.Const;
using Blog.Core.Common.Extensions;
using Blog.Core.Common.Option;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using StackExchange.Redis;
@ -14,12 +13,14 @@ using StackExchange.Redis;
namespace Blog.Core.Common.Caches;
public class Caching(
ILogger<Caching> logger,
IDistributedCache cache,
IOptions<RedisOptions> redisOptions)
: ICaching
{
private static readonly ConcurrentDictionary<string, bool> _loggedWarnings = new();
private readonly RedisOptions _redisOptions = redisOptions.Value;
private const string WarningMessage = "注入的缓存服务不是MemoryCacheManager,请检查注册配置,无法获取所有KEY";
public IDistributedCache Cache => cache;
public void DelByPattern(string key)
@ -74,8 +75,18 @@ public class Caching(
return keys.Select(u => u.ToString()).ToList();
}
var manage = App.GetService<MemoryCacheManager>(false);
return manage.GetAllKeys().ToList();
var memoryCache = App.GetService<IMemoryCache>();
if (memoryCache is not MemoryCacheManager memoryCacheManager)
{
if (_loggedWarnings.TryAdd(WarningMessage, true))
{
logger.LogWarning(WarningMessage);
}
return [];
}
return memoryCacheManager.GetAllKeys().WhereIf(!key.IsNullOrEmpty(), s => s.StartsWith(key!)).ToList();
}
public T Get<T>(string cacheKey)

View File

@ -15,4 +15,9 @@ public static class UntilExtensions
dic.Add(key, value);
}
}
public static IEnumerable<T> WhereIf<T>(this IEnumerable<T> source, bool condition, Func<T, bool> predicate)
{
return condition ? source.Where(predicate) : source;
}
}

View File

@ -48,7 +48,7 @@ public static class CacheSetup
services.AddSingleton<MemoryCacheManager>();
services.AddSingleton<IMemoryCache>(provider => provider.GetService<MemoryCacheManager>());
services.AddOptions();
services.TryAdd(ServiceDescriptor.Singleton<IDistributedCache, CommonMemoryDistributedCache>());
services.AddSingleton<IDistributedCache, CommonMemoryDistributedCache>();
}
services.AddSingleton<ICaching, Caching>();

View File

@ -1,5 +1,6 @@
using AspNetCoreRateLimit;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
@ -13,15 +14,9 @@ namespace Blog.Core.Extensions
public static void AddIpPolicyRateLimitSetup(this IServiceCollection services, IConfiguration Configuration)
{
if (services == null) throw new ArgumentNullException(nameof(services));
// CacheSetup unified register
// services.AddMemoryCache();
//load general configuration from appsettings.json
services.Configure<IpRateLimitOptions>(Configuration.GetSection("IpRateLimiting"));
// inject counter and rules stores
// services.AddSingleton<IIpPolicyStore, MemoryCacheIpPolicyStore>();
// services.AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>();
// services.AddSingleton<IProcessingStrategy, AsyncKeyLockProcessingStrategy>();
// inject counter and rules distributed cache stores
services.AddSingleton<IIpPolicyStore, DistributedCacheIpPolicyStore>();