Skip to content

Commit

Permalink
🚸🎨 Caching中增加校验警告,如果意外注册IMemoryCache
Browse files Browse the repository at this point in the history
  • Loading branch information
LemonNoCry committed Aug 2, 2024
1 parent 64c2e8c commit 288eedf
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
27 changes: 19 additions & 8 deletions Blog.Core.Common/Caches/Caching.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
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;

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)
Expand Down Expand Up @@ -74,8 +75,18 @@ public List<string> GetAllCacheKeys(string key = default)
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)
Expand Down
5 changes: 5 additions & 0 deletions Blog.Core.Common/Extensions/UntilExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ public static void AddOrModify<TKey, TValue>(this IDictionary<TKey, TValue> dic,
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;
}
}
2 changes: 1 addition & 1 deletion Blog.Core.Extensions/ServiceExtensions/CacheSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static void AddCacheSetup(this IServiceCollection services)
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>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AspNetCoreRateLimit;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

Expand All @@ -13,15 +14,9 @@ public static class IpPolicyRateLimitSetup
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>();
Expand Down

0 comments on commit 288eedf

Please sign in to comment.