diff --git a/Yi.Furion.Net6/Yi.Framework.Module/Caching/CacheManager.cs b/Yi.Furion.Net6/Yi.Framework.Module/Caching/CacheManager.cs new file mode 100644 index 00000000..fa079103 --- /dev/null +++ b/Yi.Furion.Net6/Yi.Framework.Module/Caching/CacheManager.cs @@ -0,0 +1,116 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.Framework.Module.Caching +{ + /// + /// 考虑到本地缓存与分布式缓存差异太大,使用功能限制太大,所以该抽象类淘汰 + /// + public abstract class CacheManager + { + + public virtual bool Exits(string key) + { + throw new NotImplementedException(); + } + public virtual T Get(string key) + { + throw new NotImplementedException(); + } + + public virtual bool Set(string key, T data, TimeSpan time) + { + throw new NotImplementedException(); + } + + public virtual bool Set(string key, T data) + { + throw new NotImplementedException(); + } + + public virtual long Del(string key) + { + throw new NotImplementedException(); + } + + public virtual bool HSet(string key, string fieId, object data) + { + throw new NotImplementedException(); + } + + public virtual bool HSet(string key, string fieId, object data, TimeSpan time) + { + throw new NotImplementedException(); + } + + public virtual T HGet(string key, string field) + { + throw new NotImplementedException(); + } + + + public virtual long HDel(string key, params string[] par) + { + throw new NotImplementedException(); + } + + public virtual long HLen(string key) + { + throw new NotImplementedException(); + } + + public virtual Dictionary HGetAll(string key) + { + throw new NotImplementedException(); + } + + /// + /// 简单发布 + /// + /// + /// + /// + public virtual long Publish(string channel, string message) + { + throw new NotImplementedException(); + } + + public virtual bool LSet(string key, long index, object value) + { + throw new NotImplementedException(); + } + + + + /// + /// 列表插入头部 + /// + /// + /// + /// + /// + public virtual long LPush(string key, params T[] value) + { + throw new NotImplementedException(); + } + + /// + /// 列表弹出头部 + /// + /// + /// + /// + public virtual T LPop(string key) + { + throw new NotImplementedException(); + } + + public virtual string[] Keys(string pattern) + { + throw new NotImplementedException(); + } + } +} diff --git a/Yi.Furion.Net6/Yi.Framework.Module/Caching/CachingConnOptions.cs b/Yi.Furion.Net6/Yi.Framework.Module/Caching/CachingConnOptions.cs new file mode 100644 index 00000000..2cded02e --- /dev/null +++ b/Yi.Furion.Net6/Yi.Framework.Module/Caching/CachingConnOptions.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Yi.Framework.Module.Caching +{ + public class CachingConnOptions + { + public string? Host { get; set; } + public int DB { get; set; } = 0; + public int Prot { get; set; } + public string? Password { get; set; } + } +} diff --git a/Yi.Furion.Net6/Yi.Framework.Module/Caching/MemoryCacheClient.cs b/Yi.Furion.Net6/Yi.Framework.Module/Caching/MemoryCacheClient.cs new file mode 100644 index 00000000..75bdc960 --- /dev/null +++ b/Yi.Furion.Net6/Yi.Framework.Module/Caching/MemoryCacheClient.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Furion.DependencyInjection; +using Microsoft.Extensions.Caching.Memory; + +namespace Yi.Framework.Module.Caching +{ + public class MemoryCacheClient : CacheManager,ISingleton + { + private IMemoryCache Client { get; set; } + public MemoryCacheClient() + { + Client = new Microsoft.Extensions.Caching.Memory.MemoryCache(new MemoryCacheOptions()); + } + } +} diff --git a/Yi.Furion.Net6/Yi.Framework.Module/Caching/RedisCacheClient.cs b/Yi.Furion.Net6/Yi.Framework.Module/Caching/RedisCacheClient.cs new file mode 100644 index 00000000..11e15fe9 --- /dev/null +++ b/Yi.Furion.Net6/Yi.Framework.Module/Caching/RedisCacheClient.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using CSRedis; +using Furion.DependencyInjection; +using Microsoft.Extensions.Options; +using static CSRedis.CSRedisClient; + +namespace Yi.Framework.Module.Caching +{ + + public class RedisCacheClient : CacheManager, ISingleton + { + public readonly CachingConnOptions _RedisOptions; + + //公开客户端,csredis封装的很完美了 + public CSRedisClient Client { get; set; } + + public RedisCacheClient(IOptions redisConnOptions) + { + this._RedisOptions = redisConnOptions.Value; + Client = new CSRedisClient($"{_RedisOptions.Host}:{_RedisOptions.Prot},password={_RedisOptions.Password},defaultDatabase ={_RedisOptions.DB}"); + } + } +} diff --git a/Yi.Furion.Net6/Yi.Framework.Module/Startup.cs b/Yi.Furion.Net6/Yi.Framework.Module/Startup.cs index d89984f4..b8ede490 100644 --- a/Yi.Furion.Net6/Yi.Framework.Module/Startup.cs +++ b/Yi.Furion.Net6/Yi.Framework.Module/Startup.cs @@ -5,6 +5,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Yi.Framework.Infrastructure.AspNetCore; using Yi.Framework.Infrastructure.Sqlsugar; +using Yi.Framework.Module.Caching; using Yi.Framework.Module.ImageSharp.HeiCaptcha; using Yi.Framework.Module.Sms.Aliyun; @@ -23,6 +24,8 @@ public class Startup : AppStartup services.AddHeiCaptcha(); services.Configure(App.Configuration.GetSection("SmsAliyunOptions")); + + services.Configure(App.Configuration.GetSection("CachingConnOptions")); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) diff --git a/Yi.Furion.Net6/Yi.Framework.Module/Yi.Framework.Module.csproj b/Yi.Furion.Net6/Yi.Framework.Module/Yi.Framework.Module.csproj index 57991a1d..da591219 100644 --- a/Yi.Furion.Net6/Yi.Framework.Module/Yi.Framework.Module.csproj +++ b/Yi.Furion.Net6/Yi.Framework.Module/Yi.Framework.Module.csproj @@ -15,6 +15,7 @@ + diff --git a/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/Impl/MonitorCacheService.cs b/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/Impl/MonitorCacheService.cs index e44f43fa..a9f67244 100644 --- a/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/Impl/MonitorCacheService.cs +++ b/Yi.Furion.Net6/Yi.Furion.Application/Rbac/Services/Impl/MonitorCacheService.cs @@ -3,38 +3,48 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using CSRedis; +using Furion.ClayObject.Extensions; +using Microsoft.AspNetCore.DataProtection.KeyManagement; using Microsoft.Extensions.Caching.Memory; +using Yi.Framework.Module.Caching; using Yi.Furion.Core.Rbac.Dtos.MonitorCache; namespace Yi.Furion.Application.Rbac.Services.Impl { - public class MonitorCacheService : IMonitorCacheService,IDynamicApiController,ITransient + public class MonitorCacheService : IMonitorCacheService, IDynamicApiController, ITransient { - private IMemoryCache _memoryCache; - public MonitorCacheService(IMemoryCache memoryCache) + private static List monitorCacheNames => new List() { - _memoryCache = memoryCache; + new MonitorCacheNameGetListOutputDto{ CacheName="Yi:Login",Remark="登录验证码"}, + new MonitorCacheNameGetListOutputDto{ CacheName="Yi:User",Remark="用户信息"} + }; + private Dictionary monitorCacheNamesDic => monitorCacheNames.ToDictionary(x => x.CacheName, x => x.Remark); + private CSRedisClient _cacheClient; + public MonitorCacheService(RedisCacheClient redisCacheClient) + { + _cacheClient = redisCacheClient.Client; } //cacheKey value为空,只要name和备注 public List GetName() { - return new List() { - new MonitorCacheNameGetListOutputDto{ CacheName="Yi:Login",Remark="登录验证码"}, - new MonitorCacheNameGetListOutputDto{ CacheName="Yi:User",Remark="用户信息"} - }; + //固定的 + return monitorCacheNames; } [HttpGet("key/{cacaheName}")] public List GetKey([FromRoute] string cacaheName) { - return new List() { "1233124","3124","1231251","12312412"}; + var output = _cacheClient.Keys($"{cacaheName}:*"); + return new List() { "1233124", "3124", "1231251", "12312412" }; } //全部不为空 [HttpGet("value/{cacaheName}/{cacaheKey}")] public MonitorCacheGetListOutputDto GetValue([FromRoute] string cacaheName, [FromRoute] string cacaheKey) { - return new MonitorCacheGetListOutputDto() { CacheKey= "1233124",CacheName= "Yi:Login",CacheValue="ttt",Remark= "登录验证码" }; + var value = _cacheClient.Get($"{cacaheName}:{cacaheKey}"); + return new MonitorCacheGetListOutputDto() { CacheKey = cacaheKey, CacheName = cacaheName, CacheValue = "ttt", Remark = monitorCacheNamesDic[cacaheName] }; } } diff --git a/Yi.Furion.Net6/Yi.Furion.Application/Rbac/SignalRHub/OnlineUserHub.cs b/Yi.Furion.Net6/Yi.Furion.Application/Rbac/SignalRHub/OnlineUserHub.cs index 60549993..87d52cc8 100644 --- a/Yi.Furion.Net6/Yi.Furion.Application/Rbac/SignalRHub/OnlineUserHub.cs +++ b/Yi.Furion.Net6/Yi.Furion.Application/Rbac/SignalRHub/OnlineUserHub.cs @@ -42,7 +42,7 @@ namespace Yi.Furion.Application.Rbac.SignalRHub var loginUser = GetLoginLogInfo(_httpContext); var user = clientUsers.Any(u => u.ConnnectionId == Context.ConnectionId); //判断用户是否存在,否则添加集合 - if (!user ) + if (!user) { OnlineUserModel users = new(Context.ConnectionId) { @@ -57,9 +57,10 @@ namespace Yi.Furion.Application.Rbac.SignalRHub _logger.LogInformation($"{DateTime.Now}:{name},{Context.ConnectionId}连接服务端success,当前已连接{clientUsers.Count}个"); //Clients.All.SendAsync(HubsConstant.MoreNotice, SendNotice()); + //当有人加入,向全部客户端发送当前总数 + Clients.All.SendAsync("onlineNum", clientUsers.Count); } - //当有人加入,向全部客户端发送当前总数 - Clients.All.SendAsync("onlineNum", clientUsers.Count); + //Clients.All.SendAsync(HubsConstant.OnlineUser, clientUsers); return base.OnConnectedAsync(); } diff --git a/Yi.Furion.Net6/Yi.Furion.Web.Entry/appsettings.json b/Yi.Furion.Net6/Yi.Furion.Web.Entry/appsettings.json index 8b8ac8b7..7514d8c2 100644 --- a/Yi.Furion.Net6/Yi.Furion.Web.Entry/appsettings.json +++ b/Yi.Furion.Net6/Yi.Furion.Web.Entry/appsettings.json @@ -45,5 +45,11 @@ "SignName": "", "TemplateCode": "", "EnableFeature": false + }, + "CachingConnOptions": { + "Host": "", + "DB": "", + "Prot": "", + "Password": "" } } \ No newline at end of file diff --git a/Yi.RuoYi.Vue3/src/utils/signalR.js b/Yi.RuoYi.Vue3/src/utils/signalR.js index 5674a4a8..376f38fa 100644 --- a/Yi.RuoYi.Vue3/src/utils/signalR.js +++ b/Yi.RuoYi.Vue3/src/utils/signalR.js @@ -13,7 +13,8 @@ export default { baseUrl: '', init(url) { const connection = new signalR.HubConnectionBuilder() - .withUrl(url, { accessTokenFactory: () => getToken() }) + .withUrl(url, { + headers: { Authorization: `Bearer ${getToken()}` }}) .withAutomaticReconnect()//自动重新连接 .configureLogging(signalR.LogLevel.Information) .build();