😎增加验证码分布式缓存

This commit is contained in:
zuohuaijun 2024-12-24 11:43:10 +08:00
parent a4d19a7311
commit 91b9e35146
3 changed files with 36 additions and 38 deletions

View File

@ -4,6 +4,7 @@
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.DependencyInjection.Extensions;
using NewLife.Caching.Services;
@ -33,6 +34,9 @@ public static class CacheSetup
// 注入 Redis 缓存提供者
services.AddSingleton<ICacheProvider>(p => new RedisCacheProvider(p) { Cache = redis });
// 验证码分布式缓存
services.AddSingleton<IDistributedCache, CaptchaDistributedCache>();
}
// 内存缓存兜底。在没有配置Redis时使用内存缓存逻辑代码无需修改

View File

@ -4,28 +4,30 @@
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
using Lazy.Captcha.Core;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Identity.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Admin.NET.Core;
public class DistributedCache : IDistributedCache
/// <summary>
/// 验证码分布式缓存Redis模式
/// </summary>
public class CaptchaDistributedCache : IDistributedCache
{
private static ICacheProvider _cacheProvider;
public DistributedCache(ICacheProvider cacheProvider)
public CaptchaDistributedCache(ICacheProvider cacheProvider)
{
_cacheProvider = cacheProvider;
}
}
public string GetKey(string key)
/// <summary>
/// 验证码缓存前缀标识符
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
private string GetKey(string key)
{
return "Captcha:" + key;
return $"Captcha:{key}";
}
public byte[] Get(string key)
@ -35,17 +37,29 @@ public class DistributedCache : IDistributedCache
public Task<byte[]> GetAsync(string key, CancellationToken token = default)
{
return Task.FromResult<byte[]>(_cacheProvider.Cache.Get<byte[]>(GetKey(key)));
return Task.FromResult(Get(key));
}
public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
{
var timeSpan = options.AbsoluteExpirationRelativeToNow != null ? options.AbsoluteExpirationRelativeToNow.Value : TimeSpan.FromMinutes(1);
_cacheProvider.Cache.Set(GetKey(key), value, timeSpan);
}
public Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default)
{
Set(key, value, options);
return Task.CompletedTask;
}
public void Refresh(string key)
{
//_cacheProvider.Cache.Re<byte[]>(key);
_cacheProvider.Cache.TryGetValue<byte[]>(key, out _);
}
public Task RefreshAsync(string key, CancellationToken token = default)
{
//throw new NotImplementedException();
Refresh(key);
return Task.CompletedTask;
}
@ -56,22 +70,7 @@ public class DistributedCache : IDistributedCache
public Task RemoveAsync(string key, CancellationToken token = default)
{
return Task.FromResult(_cacheProvider.Cache.Remove(GetKey(key)));
Remove(key);
return Task.CompletedTask;
}
public void Set(string key, byte[] value, DistributedCacheEntryOptions options)
{
if(options.AbsoluteExpirationRelativeToNow != null)
_cacheProvider.Cache.Set(GetKey(key), value, options.AbsoluteExpirationRelativeToNow.Value);
else
_cacheProvider.Cache.Set(GetKey(key), value, TimeSpan.FromMinutes(1));
}
public Task SetAsync(string key, byte[] value, DistributedCacheEntryOptions options, CancellationToken token = default)
{
if (options.AbsoluteExpirationRelativeToNow != null)
return Task.FromResult(_cacheProvider.Cache.Set(GetKey(key), value, options.AbsoluteExpirationRelativeToNow.Value));
else
return Task.FromResult(_cacheProvider.Cache.Set(GetKey(key), value, TimeSpan.FromMinutes(1)));
}
}
}

View File

@ -19,9 +19,7 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpOverrides;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
@ -210,11 +208,8 @@ public class Startup : AppStartup
// 系统日志
services.AddLoggingSetup();
services.AddSingleton<IDistributedCache, DistributedCache>();
// 验证码
services.AddCaptcha();
// 控制台logo
services.AddConsoleLogo();