From c551872c8f13d66eab2697dd194eb2e5b7ca615d Mon Sep 17 00:00:00 2001
From: falcon <9504402@qq.com>
Date: Wed, 1 Apr 2020 12:53:37 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Redis=E7=BC=93=E5=86=B2?=
=?UTF-8?q?=E5=92=8CJson=E5=BA=8F=E5=88=97=E5=8C=96=E7=BB=84=E4=BB=B6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Falcon.Extend/Falcon.Extend.csproj | 2 +
Falcon.Extend/ICacheProvider.cs | 27 +++++++++++
Falcon.Extend/IJsonProvider.cs | 23 +++++++++
Falcon.Extend/MsJsonProvider.cs | 29 +++++++++++
Falcon.Extend/RedisCacheProvider.cs | 61 ++++++++++++++++++++++++
Falcon.Extend/ServiceCollectionExtend.cs | 38 +++++++++++++++
6 files changed, 180 insertions(+)
create mode 100644 Falcon.Extend/ICacheProvider.cs
create mode 100644 Falcon.Extend/IJsonProvider.cs
create mode 100644 Falcon.Extend/MsJsonProvider.cs
create mode 100644 Falcon.Extend/RedisCacheProvider.cs
create mode 100644 Falcon.Extend/ServiceCollectionExtend.cs
diff --git a/Falcon.Extend/Falcon.Extend.csproj b/Falcon.Extend/Falcon.Extend.csproj
index 60c7a0b..9798c1d 100644
--- a/Falcon.Extend/Falcon.Extend.csproj
+++ b/Falcon.Extend/Falcon.Extend.csproj
@@ -12,6 +12,8 @@
+
+
diff --git a/Falcon.Extend/ICacheProvider.cs b/Falcon.Extend/ICacheProvider.cs
new file mode 100644
index 0000000..fd0aa4d
--- /dev/null
+++ b/Falcon.Extend/ICacheProvider.cs
@@ -0,0 +1,27 @@
+using System;
+
+namespace Falcon.Extend
+{
+ ///
+ /// 数据缓冲提供器接口
+ ///
+ public interface ICacheProvider
+ {
+ ///
+ /// 设置缓存数据
+ ///
+ /// 数据类型
+ /// 数据键
+ /// 数据对象
+ /// 缓存时间或者从CacheTimeSpan选择值
+ void SetCache(string key,T obj,TimeSpan span) where T : class;
+
+ ///
+ /// 从缓存中获取数据,如果未缓存返回null
+ ///
+ /// 数据类型
+ /// 缓存数据的键
+ /// 数据对象
+ T GetObj(string key) where T : class, new();
+ }
+}
diff --git a/Falcon.Extend/IJsonProvider.cs b/Falcon.Extend/IJsonProvider.cs
new file mode 100644
index 0000000..5ffb719
--- /dev/null
+++ b/Falcon.Extend/IJsonProvider.cs
@@ -0,0 +1,23 @@
+namespace Falcon.Extend
+{
+ ///
+ /// Json序列化接口
+ ///
+ public interface IJsonProvider
+ {
+ ///
+ /// 获取Json字符串
+ ///
+ /// 对象类型
+ /// 要序列化的对象
+ /// Json串
+ string GetJson(T obj);
+ ///
+ /// 获取对象
+ ///
+ /// 对象类型
+ /// Json串
+ /// 对象
+ T GetObj(string json);
+ }
+}
diff --git a/Falcon.Extend/MsJsonProvider.cs b/Falcon.Extend/MsJsonProvider.cs
new file mode 100644
index 0000000..48bbb79
--- /dev/null
+++ b/Falcon.Extend/MsJsonProvider.cs
@@ -0,0 +1,29 @@
+using System.Text.Json;
+
+namespace Falcon.Extend
+{
+ ///
+ /// 微软提供的Json序列化器封装
+ ///
+ public class MsJsonProvider:IJsonProvider
+ {
+ ///
+ /// 从对象序列化字符串
+ ///
+ /// 对象类型
+ /// 要序列化的对象
+ /// 字符串
+ public string GetJson(T obj) {
+ return JsonSerializer.Serialize(obj);
+ }
+ ///
+ /// 从字符串反序列化对象
+ ///
+ /// 对象的类型
+ /// json字符串
+ /// 对象实例
+ public T GetObj(string json) {
+ return JsonSerializer.Deserialize(json);
+ }
+ }
+}
diff --git a/Falcon.Extend/RedisCacheProvider.cs b/Falcon.Extend/RedisCacheProvider.cs
new file mode 100644
index 0000000..814b9f6
--- /dev/null
+++ b/Falcon.Extend/RedisCacheProvider.cs
@@ -0,0 +1,61 @@
+using System;
+using Microsoft.Extensions.Caching.Distributed;
+using Falcon.Extend;
+
+namespace Falcon.Extend
+{
+ ///
+ /// 使用微软Redis扩展方法组件实现Redis缓冲
+ ///
+ public class RedisCacheProvider:ICacheProvider
+ {
+ ///
+ /// 基础缓冲组件
+ ///
+ public IDistributedCache Cache { get; set; }
+ ///
+ /// 基础序列化组件
+ ///
+ public IJsonProvider Json { get; set; }
+ ///
+ /// 通过提供json序列化组件和基础缓冲组件实现Redis提供器
+ ///
+ /// 基础json序列化器
+ /// 基础缓冲器
+ public RedisCacheProvider(IJsonProvider jsonProvider,IDistributedCache distributedCache = null) {
+ this.Cache = distributedCache;
+ this.Json = jsonProvider;
+ }
+ ///
+ /// 通过key获取缓存对象
+ ///
+ /// 对象类型
+ /// 存储对象的key
+ /// 缓存的对象
+ public T GetObj(string key) where T : class, new() {
+ if(this.Cache == null) {
+ return null;
+ }
+ var str = this.Cache.GetString(key);
+ if(str.IsNullOrEmpty()) {
+ return null;
+ }
+ return this.Json.GetObj(str);
+ }
+ ///
+ /// 存储对象
+ ///
+ /// 对象的类型
+ /// 缓存的key
+ /// 要缓存的对象
+ /// 缓存时长
+ public void SetCache(string key,T obj,TimeSpan span) where T : class {
+ if(this.Cache == null || obj == null || span == null || span == TimeSpan.Zero) {
+ return;
+ }
+ this.Cache.SetString(key,this.Json.GetJson(obj),new DistributedCacheEntryOptions {
+ AbsoluteExpirationRelativeToNow = span,
+ });
+ }
+ }
+}
diff --git a/Falcon.Extend/ServiceCollectionExtend.cs b/Falcon.Extend/ServiceCollectionExtend.cs
new file mode 100644
index 0000000..342fb0d
--- /dev/null
+++ b/Falcon.Extend/ServiceCollectionExtend.cs
@@ -0,0 +1,38 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using Microsoft.Extensions.Caching.Redis;
+using Microsoft.Extensions.DependencyInjection;
+
+namespace Falcon.Extend
+{
+ ///
+ /// ServiceCollection扩展
+ ///
+ public static class ServiceCollectionExtend
+ {
+ ///
+ /// 通过提供RedisCacheOptions配置Redis实例
+ ///
+ /// 服务集合
+ /// RedisCache参数
+ /// 服务集合
+ public static IServiceCollection AddRedis(this IServiceCollection services,RedisCacheOptions option) {
+ services.AddDistributedRedisCache(op => {
+ op.InstanceName = option.InstanceName;
+ op.Configuration = option.Configuration;
+ });
+ services.AddTransient();
+ return services;
+ }
+
+ ///
+ /// 添加微软Json序列化器
+ ///
+ /// 服务集合
+ /// 服务集合
+ public static IServiceCollection AddMsJsonProvider(this IServiceCollection services) {
+ return services.AddSingleton();
+ }
+ }
+}