From c29aeeee419118be077d5eb424d309f3c9033f81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com> Date: Mon, 24 Oct 2022 22:31:53 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=93=E5=AD=98=E6=90=AD=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Yi.Framework.Core/Cache/CacheInvoker.cs | 78 ++++++++++++ .../Cache/MemoryCacheClient.cs | 33 +++++ .../Yi.Framework.Core/Cache/RedisClient.cs | 42 +++++++ .../Yi.Framework.Core/CacheInvoker.cs | 113 ------------------ .../AbstractConsulDispatcher.cs | 0 .../{ClienExtend => }/AverageDispatcher.cs | 0 .../{ClienExtend => }/PollingDispatcher.cs | 0 .../{ClienExtend => }/WeightDispatcher.cs | 0 8 files changed, 153 insertions(+), 113 deletions(-) create mode 100644 Yi.Framework.Net6/Yi.Framework.Core/Cache/CacheInvoker.cs create mode 100644 Yi.Framework.Net6/Yi.Framework.Core/Cache/MemoryCacheClient.cs create mode 100644 Yi.Framework.Net6/Yi.Framework.Core/Cache/RedisClient.cs delete mode 100644 Yi.Framework.Net6/Yi.Framework.Core/CacheInvoker.cs rename Yi.Framework.Net6/Yi.Framework.Core/ConsulExtend/{ClienExtend => }/AbstractConsulDispatcher.cs (100%) rename Yi.Framework.Net6/Yi.Framework.Core/ConsulExtend/{ClienExtend => }/AverageDispatcher.cs (100%) rename Yi.Framework.Net6/Yi.Framework.Core/ConsulExtend/{ClienExtend => }/PollingDispatcher.cs (100%) rename Yi.Framework.Net6/Yi.Framework.Core/ConsulExtend/{ClienExtend => }/WeightDispatcher.cs (100%) diff --git a/Yi.Framework.Net6/Yi.Framework.Core/Cache/CacheInvoker.cs b/Yi.Framework.Net6/Yi.Framework.Core/Cache/CacheInvoker.cs new file mode 100644 index 00000000..bb645630 --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.Core/Cache/CacheInvoker.cs @@ -0,0 +1,78 @@ +using 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 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 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(); + } + 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 T QueuePop(string key) + { + throw new NotImplementedException(); + } + public virtual long QueuePush(string key, T data) + { + throw new NotImplementedException(); + } + public virtual long QueueLen(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) + { + 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 new file mode 100644 index 00000000..78526d6a --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.Core/Cache/MemoryCacheClient.cs @@ -0,0 +1,33 @@ +using Microsoft.Extensions.Caching.Memory; +using Microsoft.Extensions.Options; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Yi.Framework.Common.IOCOptions; + +namespace Yi.Framework.Core.Cache +{ + public class MemoryCacheClient: CacheInvoker + { + private IMemoryCache _client; + public MemoryCacheClient(IOptionsMonitor redisConnOptions):base(redisConnOptions) + { + _client = new MemoryCache(new MemoryCacheOptions() { }); + } + public override T Get(string key) + { + return _client.Get(key); + } + public override bool Set(string key, T item) + { + return _client.Set(key, item) is not null; + } + + public override bool Set(string key, T item, TimeSpan time) + { + return _client.Set(key, item, time) is not null; + } + } +} diff --git a/Yi.Framework.Net6/Yi.Framework.Core/Cache/RedisClient.cs b/Yi.Framework.Net6/Yi.Framework.Core/Cache/RedisClient.cs new file mode 100644 index 00000000..af8a03d2 --- /dev/null +++ b/Yi.Framework.Net6/Yi.Framework.Core/Cache/RedisClient.cs @@ -0,0 +1,42 @@ +using 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.Core/CacheInvoker.cs b/Yi.Framework.Net6/Yi.Framework.Core/CacheInvoker.cs deleted file mode 100644 index 442f895f..00000000 --- a/Yi.Framework.Net6/Yi.Framework.Core/CacheInvoker.cs +++ /dev/null @@ -1,113 +0,0 @@ -using 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 CacheInvoker - { - - public delegate T MyAction(CSRedisClient client); - - private readonly RedisConnOptions _RedisOptions; - - private CSRedisClient Client { get; set; } - - public CSRedisClient _Db { get { return Client; } set { } } - public CacheInvoker(IOptionsMonitor redisConnOptions) - { - this._RedisOptions = redisConnOptions.CurrentValue; - Client = new CSRedisClient($"{_RedisOptions.Host}:{_RedisOptions.Prot},password={_RedisOptions.Password},defaultDatabase ={ _RedisOptions.DB }"); - } - - private T TryCatch(MyAction action) - { - - - T result = default(T); - try - { - result = action(Client); - } - catch (Exception exinfo) - { - Console.WriteLine(exinfo); - } - //finally - //{ - // Client.Dispose(); - //} - - return result; - } - - - public bool Exit(string key) - { - return this.TryCatch((u) => u.Exists(key)); - } - - public long Remove(string key) - { - return this.TryCatch((u) => u.Del(key)); - } - - public long HRemove(string key, params string[] par) - { - return this.TryCatch((u) => u.HDel(key, par)); - } - - public T Get(string key) - { - return this.TryCatch((u) => u.Get(key)); - } - public bool Set(string key, T data, TimeSpan time) - { - return this.TryCatch((u) => u.Set(key, data, time)); - } - - public bool Set(string key, T data) - { - return this.TryCatch((u) => u.Set(key, data)); - } - public T QueuePop(string key) - { - return this.TryCatch((u) => u.RPop(key)); - } - public long QueuePush(string key, T data) - { - return this.TryCatch((u) => u.LPush(key, data)); - } - public long QueueLen(string key) - { - return TryCatch((u) => u.LLen(key)); - } - - public bool HSet(string key, string fieId, T data) - { - return this.TryCatch((u) => u.HSet(key, fieId, data)); - } - public bool HSet(string key, string fieId, T data, TimeSpan time) - { - return this.TryCatch((u) => - { - var res = u.HSet(key, fieId, data); - u.Expire(key, time); - return res; - }); - } - - public CSRedisClient Db() - { - return new CSRedisClient($"{_RedisOptions.Host}:{_RedisOptions.Prot},password={_RedisOptions.Password},defaultDatabase ={ _RedisOptions.DB }"); - } - } -} diff --git a/Yi.Framework.Net6/Yi.Framework.Core/ConsulExtend/ClienExtend/AbstractConsulDispatcher.cs b/Yi.Framework.Net6/Yi.Framework.Core/ConsulExtend/AbstractConsulDispatcher.cs similarity index 100% rename from Yi.Framework.Net6/Yi.Framework.Core/ConsulExtend/ClienExtend/AbstractConsulDispatcher.cs rename to Yi.Framework.Net6/Yi.Framework.Core/ConsulExtend/AbstractConsulDispatcher.cs diff --git a/Yi.Framework.Net6/Yi.Framework.Core/ConsulExtend/ClienExtend/AverageDispatcher.cs b/Yi.Framework.Net6/Yi.Framework.Core/ConsulExtend/AverageDispatcher.cs similarity index 100% rename from Yi.Framework.Net6/Yi.Framework.Core/ConsulExtend/ClienExtend/AverageDispatcher.cs rename to Yi.Framework.Net6/Yi.Framework.Core/ConsulExtend/AverageDispatcher.cs diff --git a/Yi.Framework.Net6/Yi.Framework.Core/ConsulExtend/ClienExtend/PollingDispatcher.cs b/Yi.Framework.Net6/Yi.Framework.Core/ConsulExtend/PollingDispatcher.cs similarity index 100% rename from Yi.Framework.Net6/Yi.Framework.Core/ConsulExtend/ClienExtend/PollingDispatcher.cs rename to Yi.Framework.Net6/Yi.Framework.Core/ConsulExtend/PollingDispatcher.cs diff --git a/Yi.Framework.Net6/Yi.Framework.Core/ConsulExtend/ClienExtend/WeightDispatcher.cs b/Yi.Framework.Net6/Yi.Framework.Core/ConsulExtend/WeightDispatcher.cs similarity index 100% rename from Yi.Framework.Net6/Yi.Framework.Core/ConsulExtend/ClienExtend/WeightDispatcher.cs rename to Yi.Framework.Net6/Yi.Framework.Core/ConsulExtend/WeightDispatcher.cs