diff --git a/Falcon.SugarApi/DatabaseDefinitions/Cache/DistributedCache.cs b/Falcon.SugarApi/DatabaseDefinitions/Cache/DistributedCache.cs new file mode 100644 index 0000000..b05572f --- /dev/null +++ b/Falcon.SugarApi/DatabaseDefinitions/Cache/DistributedCache.cs @@ -0,0 +1,81 @@ +using Falcon.SugarApi.JsonSerialize; +using Microsoft.Extensions.Caching.Distributed; +using SqlSugar; +using System; +using System.Collections.Generic; + +namespace Falcon.SugarApi.DatabaseDefinitions.Cache +{ + /// + /// 使用分布式缓存存储器 + /// + public class DistributedCache : ICacheService + { + /// + /// 分布式缓存存储器 + /// + public IDistributedCache Cache { get; set; } + + /// + /// 序列化方法 + /// + public IJsonSerialize Serialize { get; set; } + + /// + /// 提供分布式缓存存储器 + /// + /// 分布式缓存存储器 + /// 实现序列化的接口s + public DistributedCache(IDistributedCache cache, IJsonSerialize serialize) { + this.Cache = cache; + this.Serialize = serialize; + } + + /// + public void Add(string key, V value) { + var valStr = this.Serialize.Serialize(value); + this.Cache.SetString(key, valStr); + } + + /// + public void Add(string key, V value, int cacheDurationInSeconds) { + var valStr = this.Serialize.Serialize(value); + DistributedCacheEntryOptions op = new() { + AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(cacheDurationInSeconds), + }; + this.Cache.SetString(key, valStr, op); + } + + /// + public bool ContainsKey(string key) { + return this.Cache.Get(key) != null; + } + + /// + public V Get(string key) { + var val = this.Cache.GetString(key); + if (val == null) return default(V); + return (V)this.Serialize.Deserialize(val, typeof(V)); + } + + /// + public IEnumerable GetAllKey() { + throw new NotImplementedException(); + } + + /// + public V GetOrCreate(string cacheKey, Func create, int cacheDurationInSeconds = int.MaxValue) { + if (ContainsKey(cacheKey)) { + return Get(cacheKey); + } + V val = create(); + Add(cacheKey, val, cacheDurationInSeconds); + return val; + } + + /// + public void Remove(string key) { + this.Cache.Remove(key); + } + } +} diff --git a/Falcon.SugarApi/DatabaseDefinitions/SugarConnectionConfig.cs b/Falcon.SugarApi/DatabaseDefinitions/SugarConnectionConfig.cs index dcb8822..8545594 100644 --- a/Falcon.SugarApi/DatabaseDefinitions/SugarConnectionConfig.cs +++ b/Falcon.SugarApi/DatabaseDefinitions/SugarConnectionConfig.cs @@ -1,8 +1,9 @@ using Falcon.SugarApi.DatabaseDefinitions.Cache; +using Falcon.SugarApi.JsonSerialize; +using Microsoft.Extensions.Caching.Distributed; using SqlSugar; using System; using System.ComponentModel.DataAnnotations; -using System.Linq; using System.Reflection; namespace Falcon.SugarApi.DatabaseDefinitions @@ -93,5 +94,15 @@ namespace Falcon.SugarApi.DatabaseDefinitions this.ConfigureExternalServices ??= new ConfigureExternalServices { }; this.ConfigureExternalServices.DataInfoCacheService = new HttpRuntimeCache(); } + + /// + /// 分布式缓存 + /// + /// 缓存提供程序 + /// 序列化实现 + public void AddDistributedCache(IDistributedCache cache, IJsonSerialize serialize) { + this.ConfigureExternalServices ??= new ConfigureExternalServices { }; + this.ConfigureExternalServices.DataInfoCacheService = new DistributedCache(cache,serialize); + } } }