feat:完善缓存相关

This commit is contained in:
陈淳
2023-04-19 13:39:55 +08:00
parent cb75196455
commit 654c7d650a
10 changed files with 214 additions and 14 deletions

View 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();
}
}
}

View File

@@ -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; }
}
}

View File

@@ -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());
}
}
}

View File

@@ -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}");
}
}
}

View File

@@ -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<SmsAliyunOptions>(App.Configuration.GetSection("SmsAliyunOptions"));
services.Configure<CachingConnOptions>(App.Configuration.GetSection("CachingConnOptions"));
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

View File

@@ -15,6 +15,7 @@
<ItemGroup>
<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.Drawing" Version="1.0.0-beta15" />
</ItemGroup>

View File

@@ -3,30 +3,39 @@ 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
{
private IMemoryCache _memoryCache;
public MonitorCacheService(IMemoryCache memoryCache)
private static List<MonitorCacheNameGetListOutputDto> monitorCacheNames => new List<MonitorCacheNameGetListOutputDto>()
{
_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和备注
public List<MonitorCacheNameGetListOutputDto> GetName()
{
return new List<MonitorCacheNameGetListOutputDto>() {
new MonitorCacheNameGetListOutputDto{ CacheName="Yi:Login",Remark="登录验证码"},
new MonitorCacheNameGetListOutputDto{ CacheName="Yi:User",Remark="用户信息"}
};
//固定的
return monitorCacheNames;
}
[HttpGet("key/{cacaheName}")]
public List<string> GetKey([FromRoute] string cacaheName)
{
var output = _cacheClient.Keys($"{cacaheName}:*");
return new List<string>() { "1233124", "3124", "1231251", "12312412" };
}
@@ -34,7 +43,8 @@ namespace Yi.Furion.Application.Rbac.Services.Impl
[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] };
}
}

View File

@@ -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(HubsConstant.OnlineUser, clientUsers);
return base.OnConnectedAsync();
}

View File

@@ -45,5 +45,11 @@
"SignName": "",
"TemplateCode": "",
"EnableFeature": false
},
"CachingConnOptions": {
"Host": "",
"DB": "",
"Prot": "",
"Password": ""
}
}

View File

@@ -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();