From 3a634d7888bd8c7dbeecd392d3f5d480f2c1a9e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=B7=B3?= Date: Wed, 26 Oct 2022 20:01:18 +0800 Subject: [PATCH] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E6=9B=B4?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Yi.Framework.ApiMicroservice/Program.cs | 2 +- .../appsettings.Production.json | 130 +++++++++----- .../appsettings.Staging.json | 130 +++++++++----- .../appsettings.json | 130 +++++++++----- .../Yi.Framework.Core/Cache/CacheInvoker.cs | 145 ++++++++++++---- .../Cache/MemoryCacheClient.cs | 16 +- .../Cache/RedisCacheClient.cs | 164 ++++++++++++++++++ .../Yi.Framework.Core/Cache/RedisClient.cs | 42 ----- .../MiddlewareExtend/CacheExtension.cs | 34 ++++ ...{RedisInitExtend.cs => CacheInitExtend.cs} | 6 +- .../MiddlewareExtend/RedisExtension.cs | 26 --- 11 files changed, 577 insertions(+), 248 deletions(-) create mode 100644 Yi.Framework.Net6/Yi.Framework.Core/Cache/RedisCacheClient.cs delete mode 100644 Yi.Framework.Net6/Yi.Framework.Core/Cache/RedisClient.cs create mode 100644 Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/CacheExtension.cs rename Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/{RedisInitExtend.cs => CacheInitExtend.cs} (88%) delete mode 100644 Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/RedisExtension.cs diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs index 45a105fc..9fc5f3e0 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Program.cs @@ -105,7 +105,7 @@ builder.Services.AddAuthorizationService(); #region //Redis服务配置 #endregion -builder.Services.AddRedisService(); +builder.Services.AddCacheService(); #region //RabbitMQ服务配置 #endregion diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/appsettings.Production.json b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/appsettings.Production.json index 07cf237d..c238933a 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/appsettings.Production.json +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/appsettings.Production.json @@ -1,5 +1,8 @@ { + //程序启动地址,*代表全部网口 "StartUrl": "http://*:19001", + + //默认日志打印过滤 "Logging": { "LogLevel": { "Default": "Information", @@ -7,76 +10,111 @@ "Microsoft.Hosting.Lifetime": "Information" } }, + + //运行全部主机 "AllowedHosts": "*", + //Sql语句日志是否打印 "SqlLog_Enable": false, - "Consul_Enabled": false, - "DbCodeFirst_Enabled": false, - "DbSeed_Enabled": false, - "Apollo_Enabled": false, - "HealthCheck_Enabled": false, - "RabbitMQ_Enabled": false, - "Redis_Enabled": false, - "RedisSeed_Enabled": false, - "Kafka_Enabled": false, - "ElasticSeach_Enabled": false, - "MutiDB_Enabled": false, - "SMS_Enabled": false, - "CAP_Enabled": false, - "CAPDashboard_Enabled": false, - "Cors_Enabled": true, + //【数据库配置】 + //多数据库主从复制是否开启 + "MutiDB_Enabled": false, + //数据库CodeFirst是否开启 + "DbCodeFirst_Enabled": true, + //数据库种子数据是否开启 + "DbSeed_Enabled": true, + //数据库列表 "DbList": [ "Sqlite", "Mysql", "Sqlserver", "Oracle" ], + //数据库类型选择 "DbSelect": "Sqlite", + //数据库连接地址,支持读写分离 "DbConn": { "WriteUrl": "DataSource=yi-sqlsugar-dev.db", - //"WriteUrl": "[xxxx];port=3306;database=[xxxx];user id=[xxxx];password=[xxxx]", "ReadUrl": [ "server=[xxxx];port=3306;database=[xxxx];user id=[xxxx];password=[xxxx]", "server=[xxxx];port=3306;database=[xxxx];user id=[xxxx];password=[xxxx]", "server=[xxxx];port=3306;database=[xxxx];user id=[xxxx];password=[xxxx]" ] }, - "JwtAuthorize": { - "Issuer": "cc", - "Audience": "cc", - "PolicyName": "permission", - "DefaultScheme": "Bearer", - "IsHttps": false, - "Expiration": 300, - "ReExpiration": 3000 + + + //【缓存配置】 + //缓存列表 + "CacheList": [ "Redis", "MemoryCache" ], + //选择缓存 + "CacheSelect": "MemoryCache", + //缓存种子数据是否开启 + "CacheSeed_Enabled": false, + + + //【中间件开启】 + //Consul是否开启 + "Consul_Enabled": false, + //健康检查是否开启 + "HealthCheck_Enabled": false, + //跨域开放是否开启 + "Cors_Enabled": true, + //Apollo是否开启 + "Apollo_Enabled": false, + //RabbitMQ是否开启 + "RabbitMQ_Enabled": false, + //Kafka是否开启 + "Kafka_Enabled": false, + //ElasticSeach是否开启 + "ElasticSeach_Enabled": false, + //短信发送是否开启 + "SMS_Enabled": false, + //分布式CAP是否开启 + "CAP_Enabled": false, + //分布式CAP面板是否开启 + "CAPDashboard_Enabled": false, + + + //【中间件配置】 + //jwt授权内容,公钥私钥转移到目录下的pem文件 + "JWTTokenOptions": { + "Audience": "http://localhost:19000", + "Issuer": "http://localhost:19000", + "SecurityKey": "[]" }, + //Redis地址配置 "RedisConnOptions": { - "Host": "[xxxx]", + "Host": "[]", "Prot": 6379, "DB": 1, - "Password": "[xxxx]" + "Password": "[]" }, + //RabbitMq地址配置 "RabbitConn": { - "HostName": "[xxxx]", - "UserName": "[xxxx]", - "Password": "[xxxx]", + "HostName": "[]", + "UserName": "[]", + "Password": "[]", "Port": 5672 }, + //ElasticSeach地址配置 "ElasticSeachConn": { - "Url": "[xxxx]", - "IndexName": "[xxxx]", - "UserName": "[xxxx]", - "PassWord": "[xxxx]" + "Url": "[]", + "IndexName": "[]", + "UserName": "[]", + "PassWord": "[]." }, + //Kafka地址配置 "KafkaOptions": { - "BrokerList": "[xxxx]", - "TopicName": "[xxxx]" + "BrokerList": "[]:9092", + "TopicName": "kafkalog" }, + //Consul地址配置 "ConsulClientOption": { - "IP": "[xxxx]", - "Port": "[xxxx]", - "Datacenter": "[xxxx]" + "IP": "[]", + "Port": "8500", + "Datacenter": "dc1" }, + //Consul注册本机配置 "ConsulRegisterOption": { - "IP": "[xxxx]", - "Port": "19001", + "IP": "[]", + "Port": "[]", "GroupName": "ApiMicroservice", "HealthCheckUrl": "/Health", "Interval": 10, @@ -84,11 +122,11 @@ "DeregisterCriticalServiceAfter": 60, "Tag": "13" }, + //阿里云短信配置 "SMS": { - "ID": "[xxxx]", - "Secret": "[xxxx]", - "Sign": "[xxxx]", - "Template": "[xxxx]" - }, - "IPLibraryServiceUrl": "http://gRPCIPLibraryService" + "ID": "[]", + "Secret": "[]", + "Sign": "[]", + "Template": "[]" + } } \ No newline at end of file diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/appsettings.Staging.json b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/appsettings.Staging.json index 07cf237d..c238933a 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/appsettings.Staging.json +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/appsettings.Staging.json @@ -1,5 +1,8 @@ { + //程序启动地址,*代表全部网口 "StartUrl": "http://*:19001", + + //默认日志打印过滤 "Logging": { "LogLevel": { "Default": "Information", @@ -7,76 +10,111 @@ "Microsoft.Hosting.Lifetime": "Information" } }, + + //运行全部主机 "AllowedHosts": "*", + //Sql语句日志是否打印 "SqlLog_Enable": false, - "Consul_Enabled": false, - "DbCodeFirst_Enabled": false, - "DbSeed_Enabled": false, - "Apollo_Enabled": false, - "HealthCheck_Enabled": false, - "RabbitMQ_Enabled": false, - "Redis_Enabled": false, - "RedisSeed_Enabled": false, - "Kafka_Enabled": false, - "ElasticSeach_Enabled": false, - "MutiDB_Enabled": false, - "SMS_Enabled": false, - "CAP_Enabled": false, - "CAPDashboard_Enabled": false, - "Cors_Enabled": true, + //【数据库配置】 + //多数据库主从复制是否开启 + "MutiDB_Enabled": false, + //数据库CodeFirst是否开启 + "DbCodeFirst_Enabled": true, + //数据库种子数据是否开启 + "DbSeed_Enabled": true, + //数据库列表 "DbList": [ "Sqlite", "Mysql", "Sqlserver", "Oracle" ], + //数据库类型选择 "DbSelect": "Sqlite", + //数据库连接地址,支持读写分离 "DbConn": { "WriteUrl": "DataSource=yi-sqlsugar-dev.db", - //"WriteUrl": "[xxxx];port=3306;database=[xxxx];user id=[xxxx];password=[xxxx]", "ReadUrl": [ "server=[xxxx];port=3306;database=[xxxx];user id=[xxxx];password=[xxxx]", "server=[xxxx];port=3306;database=[xxxx];user id=[xxxx];password=[xxxx]", "server=[xxxx];port=3306;database=[xxxx];user id=[xxxx];password=[xxxx]" ] }, - "JwtAuthorize": { - "Issuer": "cc", - "Audience": "cc", - "PolicyName": "permission", - "DefaultScheme": "Bearer", - "IsHttps": false, - "Expiration": 300, - "ReExpiration": 3000 + + + //【缓存配置】 + //缓存列表 + "CacheList": [ "Redis", "MemoryCache" ], + //选择缓存 + "CacheSelect": "MemoryCache", + //缓存种子数据是否开启 + "CacheSeed_Enabled": false, + + + //【中间件开启】 + //Consul是否开启 + "Consul_Enabled": false, + //健康检查是否开启 + "HealthCheck_Enabled": false, + //跨域开放是否开启 + "Cors_Enabled": true, + //Apollo是否开启 + "Apollo_Enabled": false, + //RabbitMQ是否开启 + "RabbitMQ_Enabled": false, + //Kafka是否开启 + "Kafka_Enabled": false, + //ElasticSeach是否开启 + "ElasticSeach_Enabled": false, + //短信发送是否开启 + "SMS_Enabled": false, + //分布式CAP是否开启 + "CAP_Enabled": false, + //分布式CAP面板是否开启 + "CAPDashboard_Enabled": false, + + + //【中间件配置】 + //jwt授权内容,公钥私钥转移到目录下的pem文件 + "JWTTokenOptions": { + "Audience": "http://localhost:19000", + "Issuer": "http://localhost:19000", + "SecurityKey": "[]" }, + //Redis地址配置 "RedisConnOptions": { - "Host": "[xxxx]", + "Host": "[]", "Prot": 6379, "DB": 1, - "Password": "[xxxx]" + "Password": "[]" }, + //RabbitMq地址配置 "RabbitConn": { - "HostName": "[xxxx]", - "UserName": "[xxxx]", - "Password": "[xxxx]", + "HostName": "[]", + "UserName": "[]", + "Password": "[]", "Port": 5672 }, + //ElasticSeach地址配置 "ElasticSeachConn": { - "Url": "[xxxx]", - "IndexName": "[xxxx]", - "UserName": "[xxxx]", - "PassWord": "[xxxx]" + "Url": "[]", + "IndexName": "[]", + "UserName": "[]", + "PassWord": "[]." }, + //Kafka地址配置 "KafkaOptions": { - "BrokerList": "[xxxx]", - "TopicName": "[xxxx]" + "BrokerList": "[]:9092", + "TopicName": "kafkalog" }, + //Consul地址配置 "ConsulClientOption": { - "IP": "[xxxx]", - "Port": "[xxxx]", - "Datacenter": "[xxxx]" + "IP": "[]", + "Port": "8500", + "Datacenter": "dc1" }, + //Consul注册本机配置 "ConsulRegisterOption": { - "IP": "[xxxx]", - "Port": "19001", + "IP": "[]", + "Port": "[]", "GroupName": "ApiMicroservice", "HealthCheckUrl": "/Health", "Interval": 10, @@ -84,11 +122,11 @@ "DeregisterCriticalServiceAfter": 60, "Tag": "13" }, + //阿里云短信配置 "SMS": { - "ID": "[xxxx]", - "Secret": "[xxxx]", - "Sign": "[xxxx]", - "Template": "[xxxx]" - }, - "IPLibraryServiceUrl": "http://gRPCIPLibraryService" + "ID": "[]", + "Secret": "[]", + "Sign": "[]", + "Template": "[]" + } } \ No newline at end of file diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/appsettings.json b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/appsettings.json index 0278d8e5..c238933a 100644 --- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/appsettings.json +++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/appsettings.json @@ -1,5 +1,8 @@ { + //程序启动地址,*代表全部网口 "StartUrl": "http://*:19001", + + //默认日志打印过滤 "Logging": { "LogLevel": { "Default": "Information", @@ -7,76 +10,111 @@ "Microsoft.Hosting.Lifetime": "Information" } }, + + //运行全部主机 "AllowedHosts": "*", + //Sql语句日志是否打印 "SqlLog_Enable": false, - "Consul_Enabled": false, - "DbCodeFirst_Enabled": true, - "DbSeed_Enabled": true, - "Apollo_Enabled": false, - "HealthCheck_Enabled": false, - "RabbitMQ_Enabled": false, - "Redis_Enabled": false, - "RedisSeed_Enabled": false, - "Kafka_Enabled": false, - "ElasticSeach_Enabled": false, - "MutiDB_Enabled": false, - "SMS_Enabled": false, - "CAP_Enabled": false, - "CAPDashboard_Enabled": false, - "Cors_Enabled": true, + //【数据库配置】 + //多数据库主从复制是否开启 + "MutiDB_Enabled": false, + //数据库CodeFirst是否开启 + "DbCodeFirst_Enabled": true, + //数据库种子数据是否开启 + "DbSeed_Enabled": true, + //数据库列表 "DbList": [ "Sqlite", "Mysql", "Sqlserver", "Oracle" ], + //数据库类型选择 "DbSelect": "Sqlite", + //数据库连接地址,支持读写分离 "DbConn": { "WriteUrl": "DataSource=yi-sqlsugar-dev.db", - //"WriteUrl": "[xxxx];port=3306;database=[xxxx];user id=[xxxx];password=[xxxx]", "ReadUrl": [ "server=[xxxx];port=3306;database=[xxxx];user id=[xxxx];password=[xxxx]", "server=[xxxx];port=3306;database=[xxxx];user id=[xxxx];password=[xxxx]", "server=[xxxx];port=3306;database=[xxxx];user id=[xxxx];password=[xxxx]" ] }, - "JwtAuthorize": { - "Issuer": "cc", - "Audience": "cc", - "PolicyName": "permission", - "DefaultScheme": "Bearer", - "IsHttps": false, - "Expiration": 300, - "ReExpiration": 3000 + + + //【缓存配置】 + //缓存列表 + "CacheList": [ "Redis", "MemoryCache" ], + //选择缓存 + "CacheSelect": "MemoryCache", + //缓存种子数据是否开启 + "CacheSeed_Enabled": false, + + + //【中间件开启】 + //Consul是否开启 + "Consul_Enabled": false, + //健康检查是否开启 + "HealthCheck_Enabled": false, + //跨域开放是否开启 + "Cors_Enabled": true, + //Apollo是否开启 + "Apollo_Enabled": false, + //RabbitMQ是否开启 + "RabbitMQ_Enabled": false, + //Kafka是否开启 + "Kafka_Enabled": false, + //ElasticSeach是否开启 + "ElasticSeach_Enabled": false, + //短信发送是否开启 + "SMS_Enabled": false, + //分布式CAP是否开启 + "CAP_Enabled": false, + //分布式CAP面板是否开启 + "CAPDashboard_Enabled": false, + + + //【中间件配置】 + //jwt授权内容,公钥私钥转移到目录下的pem文件 + "JWTTokenOptions": { + "Audience": "http://localhost:19000", + "Issuer": "http://localhost:19000", + "SecurityKey": "[]" }, + //Redis地址配置 "RedisConnOptions": { - "Host": "[xxxx]", + "Host": "[]", "Prot": 6379, "DB": 1, - "Password": "[xxxx]" + "Password": "[]" }, + //RabbitMq地址配置 "RabbitConn": { - "HostName": "[xxxx]", - "UserName": "[xxxx]", - "Password": "[xxxx]", + "HostName": "[]", + "UserName": "[]", + "Password": "[]", "Port": 5672 }, + //ElasticSeach地址配置 "ElasticSeachConn": { - "Url": "[xxxx]", - "IndexName": "[xxxx]", - "UserName": "[xxxx]", - "PassWord": "[xxxx]" + "Url": "[]", + "IndexName": "[]", + "UserName": "[]", + "PassWord": "[]." }, + //Kafka地址配置 "KafkaOptions": { - "BrokerList": "[xxxx]", - "TopicName": "[xxxx]" + "BrokerList": "[]:9092", + "TopicName": "kafkalog" }, + //Consul地址配置 "ConsulClientOption": { - "IP": "[xxxx]", - "Port": "[xxxx]", - "Datacenter": "[xxxx]" + "IP": "[]", + "Port": "8500", + "Datacenter": "dc1" }, + //Consul注册本机配置 "ConsulRegisterOption": { - "IP": "[xxxx]", - "Port": "19001", + "IP": "[]", + "Port": "[]", "GroupName": "ApiMicroservice", "HealthCheckUrl": "/Health", "Interval": 10, @@ -84,11 +122,11 @@ "DeregisterCriticalServiceAfter": 60, "Tag": "13" }, + //阿里云短信配置 "SMS": { - "ID": "[xxxx]", - "Secret": "[xxxx]", - "Sign": "[xxxx]", - "Template": "[xxxx]" - }, - "IPLibraryServiceUrl": "http://gRPCIPLibraryService" + "ID": "[]", + "Secret": "[]", + "Sign": "[]", + "Template": "[]" + } } \ No newline at end of file diff --git a/Yi.Framework.Net6/Yi.Framework.Core/Cache/CacheInvoker.cs b/Yi.Framework.Net6/Yi.Framework.Core/Cache/CacheInvoker.cs index bb645630..303a6e3f 100644 --- a/Yi.Framework.Net6/Yi.Framework.Core/Cache/CacheInvoker.cs +++ b/Yi.Framework.Net6/Yi.Framework.Core/Cache/CacheInvoker.cs @@ -9,34 +9,16 @@ using System.Threading.Tasks; using System.IO; using Yi.Framework.Common.IOCOptions; using CSRedis; +using static CSRedis.CSRedisClient; namespace Yi.Framework.Core { public abstract class CacheInvoker { - private readonly RedisConnOptions _RedisOptions; - - protected CacheInvoker Client { get; set; } - - public CacheInvoker Db { get { return Client; } set { } } - public CacheInvoker(IOptionsMonitor redisConnOptions) + public virtual bool Exits(string key) { - - } - public virtual bool Exit(string key) - { - throw new NotImplementedException(); - } - - public virtual long Remove(string key) - { - throw new NotImplementedException(); - } - - public virtual long HRemove(string key, params string[] par) - { - throw new NotImplementedException(); + throw new NotImplementedException(); } public virtual T Get(string key) { @@ -52,27 +34,120 @@ namespace Yi.Framework.Core { throw new NotImplementedException(); } - public virtual T QueuePop(string key) - { - throw new NotImplementedException(); - } - public virtual long QueuePush(string key, T data) - { - throw new NotImplementedException(); - } - public virtual long QueueLen(string key) + + public virtual long Del(string key) { throw new NotImplementedException(); } - public virtual bool HSet(string key, string fieId, T data) - { - throw new NotImplementedException(); - } - public virtual bool HSet(string key, string fieId, T data, TimeSpan time) + 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(); + } + + /// + /// 绠鍗曡闃咃細骞挎挱锛屾棤鎸佷箙鍖栵紝闇瑕丳ublish鍐欏叆闃熷垪 + /// + /// + /// + public virtual SubscribeObject Subscribe(params (string, Action)[] channels) + { + throw new NotImplementedException(); + } + + /// + /// 澶氱浜夋姠妯″紡璁㈤槄锛岄渶瑕丩push鍐欏叆闃熷垪 + /// + /// + /// + /// + public virtual SubscribeListObject SubscribeList(string listKey, Action onMessage) + { + throw new NotImplementedException(); + } + + /// + /// 澶氱闈炰簤鎶㈡ā寮忚闃咃紝闇瑕丩push鍐欏叆闃熷垪 + /// + /// + /// + /// + /// + public virtual SubscribeListBroadcastObject SubscribeListBroadcast(string listKey, string clientId, Action onMessage) + { + 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.Framework.Net6/Yi.Framework.Core/Cache/MemoryCacheClient.cs b/Yi.Framework.Net6/Yi.Framework.Core/Cache/MemoryCacheClient.cs index 78526d6a..b11d30bd 100644 --- a/Yi.Framework.Net6/Yi.Framework.Core/Cache/MemoryCacheClient.cs +++ b/Yi.Framework.Net6/Yi.Framework.Core/Cache/MemoryCacheClient.cs @@ -9,12 +9,16 @@ using Yi.Framework.Common.IOCOptions; namespace Yi.Framework.Core.Cache { - public class MemoryCacheClient: CacheInvoker + public class MemoryCacheClient : CacheInvoker { private IMemoryCache _client; - public MemoryCacheClient(IOptionsMonitor redisConnOptions):base(redisConnOptions) + public MemoryCacheClient() { - _client = new MemoryCache(new MemoryCacheOptions() { }); + _client = new MemoryCache(new MemoryCacheOptions()); + } + public override bool Exits(string key) + { + return _client.TryGetValue(key, out var _); } public override T Get(string key) { @@ -29,5 +33,11 @@ namespace Yi.Framework.Core.Cache { return _client.Set(key, item, time) is not null; } + + public override long Del(string key) + { + _client.Remove(key); + return 1; + } } } diff --git a/Yi.Framework.Net6/Yi.Framework.Core/Cache/RedisCacheClient.cs b/Yi.Framework.Net6/Yi.Framework.Core/Cache/RedisCacheClient.cs new file mode 100644 index 00000000..98217a9b --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.Core/Cache/RedisCacheClient.cs @@ -0,0 +1,164 @@ +锘縰sing Microsoft.Extensions.Options; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.IO; +using Yi.Framework.Common.IOCOptions; +using CSRedis; +using static CSRedis.CSRedisClient; + +namespace Yi.Framework.Core.Cache +{ + public class RedisCacheClient : CacheInvoker + { + private readonly RedisConnOptions _RedisOptions; + + private CSRedisClient _client; + + public RedisCacheClient(IOptionsMonitor redisConnOptions) + { + this._RedisOptions = redisConnOptions.CurrentValue; + _client = new CSRedisClient($"{_RedisOptions.Host}:{_RedisOptions.Prot},password={_RedisOptions.Password},defaultDatabase ={ _RedisOptions.DB }"); + } + public override bool Exits(string key) + { + return _client.Exists(key); + } + public override T Get(string key) + { + return _client.Get(key); + } + + public override bool Set(string key, T data, TimeSpan time) + { + return _client.Set(key, data, time); + } + + public override bool Set(string key, T data) + { + return _client.Set(key, data); + } + + public override long Del(string key) + { + return _client.Del(key); + } + + public override bool HSet(string key, string fieId, object data) + { + return _client.HSet(key, fieId, data); + } + + public override bool HSet(string key, string fieId, object data, TimeSpan time) + { + var res = _client.HSet(key, fieId, data); + var res2 = _client.Expire(key, time); + return res && res2; + } + + public override T HGet(string key, string field) + { + return _client.HGet(key, field); + } + + + public override long HDel(string key, params string[] par) + { + return _client.HDel(key, par); + } + + public override long HLen(string key) + { + return _client.HLen(key); + } + + public override Dictionary HGetAll(string key) + { + return _client.HGetAll(key); + } + + /// + /// 绠鍗曞彂甯 + /// + /// + /// + /// + public override long Publish(string channel, string message) + { + return _client.Publish(channel, message); + } + + /// + /// 绠鍗曡闃咃細骞挎挱锛屾棤鎸佷箙鍖栵紝闇瑕丳ublish鍐欏叆闃熷垪 + /// + /// + /// + public override SubscribeObject Subscribe(params (string, Action)[] channels) + { + return _client.Subscribe(channels); + } + + /// + /// 澶氱浜夋姠妯″紡璁㈤槄锛岄渶瑕丩push鍐欏叆闃熷垪 + /// + /// + /// + /// + public override SubscribeListObject SubscribeList(string listKey, Action onMessage) + { + return _client.SubscribeList(listKey, onMessage); + } + + /// + /// 澶氱闈炰簤鎶㈡ā寮忚闃咃紝闇瑕丩push鍐欏叆闃熷垪 + /// + /// + /// + /// + /// + public override SubscribeListBroadcastObject SubscribeListBroadcast(string listKey, string clientId, Action onMessage) + { + return _client.SubscribeListBroadcast(listKey, clientId, onMessage); + } + + public override bool LSet(string key, long index, object value) + { + return _client.LSet(key, index, value); + } + + + + /// + /// 鍒楄〃鎻掑叆澶撮儴 + /// + /// + /// + /// + /// + public override long LPush(string key, params T[] value) + { + return _client.LPush(key,value); + } + + /// + /// 鍒楄〃寮瑰嚭澶撮儴 + /// + /// + /// + /// + public override T LPop(string key) + { + return _client.LPop(key); + } + + public override string[] Keys(string pattern) + { + return _client.Keys(pattern); + } + + } +} diff --git a/Yi.Framework.Net6/Yi.Framework.Core/Cache/RedisClient.cs b/Yi.Framework.Net6/Yi.Framework.Core/Cache/RedisClient.cs deleted file mode 100644 index af8a03d2..00000000 --- a/Yi.Framework.Net6/Yi.Framework.Core/Cache/RedisClient.cs +++ /dev/null @@ -1,42 +0,0 @@ -锘縰sing Microsoft.Extensions.Options; -using System; -using System.Collections; -using System.Collections.Generic; -using System.Diagnostics; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.IO; -using Yi.Framework.Common.IOCOptions; -using CSRedis; - -namespace Yi.Framework.Core -{ - public class RedisClient: CacheInvoker - { - private readonly RedisConnOptions _RedisOptions; - - private CSRedisClient _client; - - public RedisClient(IOptionsMonitor redisConnOptions):base(redisConnOptions) - { - this._RedisOptions = redisConnOptions.CurrentValue; - _client = new CSRedisClient($"{_RedisOptions.Host}:{_RedisOptions.Prot},password={_RedisOptions.Password},defaultDatabase ={ _RedisOptions.DB }"); - } - - public override T Get(string key) - { - return _client.Get(key); - } - - public override bool Set(string key, T data, TimeSpan time) - { - return _client.Set(key, data, time); - } - - public override bool Set(string key, T data) - { - return _client.Set(key, data); - } - } -} diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/CacheExtension.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/CacheExtension.cs new file mode 100644 index 00000000..e206a7e0 --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/CacheExtension.cs @@ -0,0 +1,34 @@ +锘縰sing Microsoft.AspNetCore.Builder; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.OpenApi.Models; +using System; +using System.IO; +using Yi.Framework.Common.IOCOptions; +using Yi.Framework.Core; +using Yi.Framework.Core.Cache; + +namespace Yi.Framework.WebCore.MiddlewareExtend +{ + /// + /// Redis鎵╁睍 + /// + public static class CacheExtension + { + public static IServiceCollection AddCacheService(this IServiceCollection services) + { + var cacheSelect= Appsettings.app("CacheSelect"); + + switch (cacheSelect) { + case "Redis": + services.Configure(Appsettings.appConfiguration("RedisConnOptions")); + services.AddSingleton(); + break; + case "MemoryCache": + services.AddSingleton(); + break; + default:throw new ArgumentException("CacheSelect閰嶇疆濉殑鏄粈涔堜笢瑗匡紵淇轰笉璁ゅ緱"); + } + return services; + } + } +} diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/RedisInitExtend.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/CacheInitExtend.cs similarity index 88% rename from Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/RedisInitExtend.cs rename to Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/CacheInitExtend.cs index 9b91ae9c..5db35b91 100644 --- a/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/RedisInitExtend.cs +++ b/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/CacheInitExtend.cs @@ -12,13 +12,13 @@ using Yi.Framework.Core; namespace Yi.Framework.WebCore.MiddlewareExtend { - public static class RedisInitExtend + public static class CacheInitExtend { - private static readonly ILog log = LogManager.GetLogger(typeof(RedisInitExtend)); + private static readonly ILog log = LogManager.GetLogger(typeof(CacheInitExtend)); public static void UseRedisSeedInitService(this IApplicationBuilder app ) { - if (Appsettings.appBool("RedisSeed_Enabled")) + if (Appsettings.appBool("CacheSeed_Enabled")) { var _cacheClientDB = app.ApplicationServices.GetService(); diff --git a/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/RedisExtension.cs b/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/RedisExtension.cs deleted file mode 100644 index 0201e529..00000000 --- a/Yi.Framework.Net6/Yi.Framework.WebCore/MiddlewareExtend/RedisExtension.cs +++ /dev/null @@ -1,26 +0,0 @@ -锘縰sing Microsoft.AspNetCore.Builder; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.OpenApi.Models; -using System; -using System.IO; -using Yi.Framework.Common.IOCOptions; -using Yi.Framework.Core; - -namespace Yi.Framework.WebCore.MiddlewareExtend -{ - /// - /// Redis鎵╁睍 - /// - public static class RedisExtension - { - public static IServiceCollection AddRedisService(this IServiceCollection services) - { - if (Appsettings.appBool("Redis_Enabled")) - { - services.Configure(Appsettings.appConfiguration("RedisConnOptions")); - services.AddSingleton(); - } - return services; - } - } -}