From 14cb6627ff2ba75d9eafe0e78fce90d863faf352 Mon Sep 17 00:00:00 2001
From: falcon <9504402@qq.com>
Date: Tue, 11 Oct 2022 10:24:08 +0800
Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E7=BC=93=E5=AD=98?=
=?UTF-8?q?=E6=94=AF=E6=8C=81=E5=88=86=E5=B8=83=E5=BC=8F=E7=BC=93=E5=AD=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Cache/DistributedCache.cs | 81 +++++++++++++++++++
.../SugarConnectionConfig.cs | 13 ++-
2 files changed, 93 insertions(+), 1 deletion(-)
create mode 100644 Falcon.SugarApi/DatabaseDefinitions/Cache/DistributedCache.cs
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);
+ }
}
}