feat:完善缓存相关
This commit is contained in:
116
Yi.Furion.Net6/Yi.Framework.Module/Caching/CacheManager.cs
Normal file
116
Yi.Furion.Net6/Yi.Framework.Module/Caching/CacheManager.cs
Normal file
@@ -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
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 考虑到本地缓存与分布式缓存差异太大,使用功能限制太大,所以该抽象类淘汰
|
||||||
|
/// </summary>
|
||||||
|
public abstract class CacheManager
|
||||||
|
{
|
||||||
|
|
||||||
|
public virtual bool Exits(string key)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
public virtual T Get<T>(string key)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual bool Set<T>(string key, T data, TimeSpan time)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual bool Set<T>(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<T>(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<string, string> HGetAll(string key)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 简单发布
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="channel"></param>
|
||||||
|
/// <param name="message"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual long Publish(string channel, string message)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual bool LSet(string key, long index, object value)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 列表插入头部
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="key"></param>
|
||||||
|
/// <param name="value"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual long LPush<T>(string key, params T[] value)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 列表弹出头部
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <param name="key"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public virtual T LPop<T>(string key)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual string[] Keys(string pattern)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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<CachingConnOptions> redisConnOptions)
|
||||||
|
{
|
||||||
|
this._RedisOptions = redisConnOptions.Value;
|
||||||
|
Client = new CSRedisClient($"{_RedisOptions.Host}:{_RedisOptions.Prot},password={_RedisOptions.Password},defaultDatabase ={_RedisOptions.DB}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ using Microsoft.Extensions.DependencyInjection;
|
|||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Yi.Framework.Infrastructure.AspNetCore;
|
using Yi.Framework.Infrastructure.AspNetCore;
|
||||||
using Yi.Framework.Infrastructure.Sqlsugar;
|
using Yi.Framework.Infrastructure.Sqlsugar;
|
||||||
|
using Yi.Framework.Module.Caching;
|
||||||
using Yi.Framework.Module.ImageSharp.HeiCaptcha;
|
using Yi.Framework.Module.ImageSharp.HeiCaptcha;
|
||||||
using Yi.Framework.Module.Sms.Aliyun;
|
using Yi.Framework.Module.Sms.Aliyun;
|
||||||
|
|
||||||
@@ -23,6 +24,8 @@ public class Startup : AppStartup
|
|||||||
services.AddHeiCaptcha();
|
services.AddHeiCaptcha();
|
||||||
|
|
||||||
services.Configure<SmsAliyunOptions>(App.Configuration.GetSection("SmsAliyunOptions"));
|
services.Configure<SmsAliyunOptions>(App.Configuration.GetSection("SmsAliyunOptions"));
|
||||||
|
|
||||||
|
services.Configure<CachingConnOptions>(App.Configuration.GetSection("CachingConnOptions"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="AlibabaCloud.SDK.Dysmsapi20170525" Version="2.0.23" />
|
<PackageReference Include="AlibabaCloud.SDK.Dysmsapi20170525" Version="2.0.23" />
|
||||||
|
<PackageReference Include="CSRedisCore" Version="3.8.670" />
|
||||||
<PackageReference Include="SixLabors.ImageSharp" Version="3.0.1" />
|
<PackageReference Include="SixLabors.ImageSharp" Version="3.0.1" />
|
||||||
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta15" />
|
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="1.0.0-beta15" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -3,38 +3,48 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using CSRedis;
|
||||||
|
using Furion.ClayObject.Extensions;
|
||||||
|
using Microsoft.AspNetCore.DataProtection.KeyManagement;
|
||||||
using Microsoft.Extensions.Caching.Memory;
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
|
using Yi.Framework.Module.Caching;
|
||||||
using Yi.Furion.Core.Rbac.Dtos.MonitorCache;
|
using Yi.Furion.Core.Rbac.Dtos.MonitorCache;
|
||||||
|
|
||||||
namespace Yi.Furion.Application.Rbac.Services.Impl
|
namespace Yi.Furion.Application.Rbac.Services.Impl
|
||||||
{
|
{
|
||||||
public class MonitorCacheService : IMonitorCacheService,IDynamicApiController,ITransient
|
public class MonitorCacheService : IMonitorCacheService, IDynamicApiController, ITransient
|
||||||
{
|
{
|
||||||
private IMemoryCache _memoryCache;
|
private static List<MonitorCacheNameGetListOutputDto> monitorCacheNames => new List<MonitorCacheNameGetListOutputDto>()
|
||||||
public MonitorCacheService(IMemoryCache memoryCache)
|
|
||||||
{
|
{
|
||||||
_memoryCache = memoryCache;
|
new MonitorCacheNameGetListOutputDto{ CacheName="Yi:Login",Remark="登录验证码"},
|
||||||
|
new MonitorCacheNameGetListOutputDto{ CacheName="Yi:User",Remark="用户信息"}
|
||||||
|
};
|
||||||
|
private Dictionary<string, string> monitorCacheNamesDic => monitorCacheNames.ToDictionary(x => x.CacheName, x => x.Remark);
|
||||||
|
private CSRedisClient _cacheClient;
|
||||||
|
public MonitorCacheService(RedisCacheClient redisCacheClient)
|
||||||
|
{
|
||||||
|
_cacheClient = redisCacheClient.Client;
|
||||||
}
|
}
|
||||||
//cacheKey value为空,只要name和备注
|
//cacheKey value为空,只要name和备注
|
||||||
|
|
||||||
public List<MonitorCacheNameGetListOutputDto> GetName()
|
public List<MonitorCacheNameGetListOutputDto> GetName()
|
||||||
{
|
{
|
||||||
return new List<MonitorCacheNameGetListOutputDto>() {
|
//固定的
|
||||||
new MonitorCacheNameGetListOutputDto{ CacheName="Yi:Login",Remark="登录验证码"},
|
return monitorCacheNames;
|
||||||
new MonitorCacheNameGetListOutputDto{ CacheName="Yi:User",Remark="用户信息"}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
[HttpGet("key/{cacaheName}")]
|
[HttpGet("key/{cacaheName}")]
|
||||||
public List<string> GetKey([FromRoute] string cacaheName)
|
public List<string> GetKey([FromRoute] string cacaheName)
|
||||||
{
|
{
|
||||||
return new List<string>() { "1233124","3124","1231251","12312412"};
|
var output = _cacheClient.Keys($"{cacaheName}:*");
|
||||||
|
return new List<string>() { "1233124", "3124", "1231251", "12312412" };
|
||||||
}
|
}
|
||||||
|
|
||||||
//全部不为空
|
//全部不为空
|
||||||
[HttpGet("value/{cacaheName}/{cacaheKey}")]
|
[HttpGet("value/{cacaheName}/{cacaheKey}")]
|
||||||
public MonitorCacheGetListOutputDto GetValue([FromRoute] string cacaheName, [FromRoute] string 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] };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ namespace Yi.Furion.Application.Rbac.SignalRHub
|
|||||||
var loginUser = GetLoginLogInfo(_httpContext);
|
var loginUser = GetLoginLogInfo(_httpContext);
|
||||||
var user = clientUsers.Any(u => u.ConnnectionId == Context.ConnectionId);
|
var user = clientUsers.Any(u => u.ConnnectionId == Context.ConnectionId);
|
||||||
//判断用户是否存在,否则添加集合
|
//判断用户是否存在,否则添加集合
|
||||||
if (!user )
|
if (!user)
|
||||||
{
|
{
|
||||||
OnlineUserModel users = new(Context.ConnectionId)
|
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}个");
|
_logger.LogInformation($"{DateTime.Now}:{name},{Context.ConnectionId}连接服务端success,当前已连接{clientUsers.Count}个");
|
||||||
|
|
||||||
//Clients.All.SendAsync(HubsConstant.MoreNotice, SendNotice());
|
//Clients.All.SendAsync(HubsConstant.MoreNotice, SendNotice());
|
||||||
|
//当有人加入,向全部客户端发送当前总数
|
||||||
|
Clients.All.SendAsync("onlineNum", clientUsers.Count);
|
||||||
}
|
}
|
||||||
//当有人加入,向全部客户端发送当前总数
|
|
||||||
Clients.All.SendAsync("onlineNum", clientUsers.Count);
|
|
||||||
//Clients.All.SendAsync(HubsConstant.OnlineUser, clientUsers);
|
//Clients.All.SendAsync(HubsConstant.OnlineUser, clientUsers);
|
||||||
return base.OnConnectedAsync();
|
return base.OnConnectedAsync();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,5 +45,11 @@
|
|||||||
"SignName": "",
|
"SignName": "",
|
||||||
"TemplateCode": "",
|
"TemplateCode": "",
|
||||||
"EnableFeature": false
|
"EnableFeature": false
|
||||||
|
},
|
||||||
|
"CachingConnOptions": {
|
||||||
|
"Host": "",
|
||||||
|
"DB": "",
|
||||||
|
"Prot": "",
|
||||||
|
"Password": ""
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -13,7 +13,8 @@ export default {
|
|||||||
baseUrl: '',
|
baseUrl: '',
|
||||||
init(url) {
|
init(url) {
|
||||||
const connection = new signalR.HubConnectionBuilder()
|
const connection = new signalR.HubConnectionBuilder()
|
||||||
.withUrl(url, { accessTokenFactory: () => getToken() })
|
.withUrl(url, {
|
||||||
|
headers: { Authorization: `Bearer ${getToken()}` }})
|
||||||
.withAutomaticReconnect()//自动重新连接
|
.withAutomaticReconnect()//自动重新连接
|
||||||
.configureLogging(signalR.LogLevel.Information)
|
.configureLogging(signalR.LogLevel.Information)
|
||||||
.build();
|
.build();
|
||||||
|
|||||||
Reference in New Issue
Block a user