From 7ad681c79a6f0bc06147966dce35d611ba575093 Mon Sep 17 00:00:00 2001
From: falcon <9504402@qq.com>
Date: Mon, 4 Mar 2019 13:53:35 +0800
Subject: [PATCH] =?UTF-8?q?(#3)=E5=A2=9E=E5=8A=A0Redis=E6=95=B0=E6=8D=AE?=
=?UTF-8?q?=E5=BA=93=E5=AD=98=E5=8F=96=E6=96=B9=E6=B3=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
WebSiteCode/Cmdjy/Cmdjy/Bll/Cache.cs | 85 +++++++++++++++++++++++++
WebSiteCode/Cmdjy/Cmdjy/Cmdjy.csproj | 32 ++++++++++
WebSiteCode/Cmdjy/Cmdjy/Web.config | 10 +--
WebSiteCode/Cmdjy/Cmdjy/WebSettings.cs | 8 ++-
WebSiteCode/Cmdjy/Cmdjy/packages.config | 10 +++
5 files changed, 138 insertions(+), 7 deletions(-)
create mode 100644 WebSiteCode/Cmdjy/Cmdjy/Bll/Cache.cs
diff --git a/WebSiteCode/Cmdjy/Cmdjy/Bll/Cache.cs b/WebSiteCode/Cmdjy/Cmdjy/Bll/Cache.cs
new file mode 100644
index 0000000..b77b64e
--- /dev/null
+++ b/WebSiteCode/Cmdjy/Cmdjy/Bll/Cache.cs
@@ -0,0 +1,85 @@
+using System;
+using Newtonsoft.Json;
+using StackExchange.Redis;
+
+namespace Cmdjy.Bll
+{
+ public interface ICache
+ {
+ ///
+ /// 是否可用
+ ///
+ bool Usable { get; }
+ ///
+ /// 获取数据
+ ///
+ /// 数据类型
+ /// 保存的key
+ /// 缓存的数据
+ T GetData(string key);
+ ///
+ /// 缓存数据,缓存时间默认
+ ///
+ /// 数据类型
+ /// 保存的key
+ /// 缓存的数据
+ void SetData(string key,T obj);
+ ///
+ /// 缓存数据
+ ///
+ /// 数据类型
+ /// 保存的key
+ /// 缓存的数据
+ /// 缓存时间
+ void SetData(string key,T obj,TimeSpan timeSpan);
+ }
+ ///
+ /// 数据缓存实现类
+ ///
+ public class RedisHelper:ICache
+ {
+ public IDatabase Database { get; set; }
+ public static ConnectionMultiplexer Connection { get; set; }
+
+ public bool Usable {
+ get {
+ return Connection != null && Connection.IsConnected && Database != null;
+ }
+ }
+
+ public RedisHelper() {
+ if(Connection == null) {
+ var connectionStr = WebSettings.RedisConnectionString;
+ Connection = ConnectionMultiplexer.Connect(connectionStr);
+ }
+ this.Database = this.Database ?? Connection.GetDatabase(0);
+ }
+
+ public T GetData(string key) {
+ if(!Usable) return default(T);
+ return fromJson(Database.StringGet(key));
+ }
+
+ public void SetData(string key,T obj) {
+ SetData(key,obj,new TimeSpan(3,0,0,0));
+ }
+
+ public void SetData(string key,T obj,TimeSpan timeSpan) {
+ if(!Usable) return;
+ if(timeSpan == null) {
+ this.Database.StringSet(key,toJson(obj));
+ }
+ else {
+ this.Database.StringSet(key,toJson(obj),timeSpan);
+ }
+ }
+
+ private static string toJson(object value) {
+ return JsonConvert.SerializeObject(value);
+ }
+
+ private static T fromJson(string value) {
+ return JsonConvert.DeserializeObject(value);
+ }
+ }
+}
\ No newline at end of file
diff --git a/WebSiteCode/Cmdjy/Cmdjy/Cmdjy.csproj b/WebSiteCode/Cmdjy/Cmdjy/Cmdjy.csproj
index 23e3bda..d5decd4 100644
--- a/WebSiteCode/Cmdjy/Cmdjy/Cmdjy.csproj
+++ b/WebSiteCode/Cmdjy/Cmdjy/Cmdjy.csproj
@@ -55,9 +55,40 @@
..\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll
+
+ ..\packages\Pipelines.Sockets.Unofficial.1.0.7\lib\net461\Pipelines.Sockets.Unofficial.dll
+
+
+ ..\packages\StackExchange.Redis.2.0.519\lib\net461\StackExchange.Redis.dll
+
+
+ ..\packages\System.Buffers.4.4.0\lib\netstandard2.0\System.Buffers.dll
+
+
+ ..\packages\System.Diagnostics.PerformanceCounter.4.5.0\lib\net461\System.Diagnostics.PerformanceCounter.dll
+
+
+ ..\packages\System.IO.Pipelines.4.5.1\lib\netstandard2.0\System.IO.Pipelines.dll
+
+
+ ..\packages\System.Memory.4.5.1\lib\netstandard2.0\System.Memory.dll
+
+
+
+ ..\packages\System.Numerics.Vectors.4.4.0\lib\net46\System.Numerics.Vectors.dll
+
+
+ ..\packages\System.Runtime.CompilerServices.Unsafe.4.5.0\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll
+
+
+ ..\packages\System.Threading.Channels.4.5.0\lib\netstandard2.0\System.Threading.Channels.dll
+
+
+ ..\packages\System.Threading.Tasks.Extensions.4.5.1\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll
+
@@ -154,6 +185,7 @@
+
diff --git a/WebSiteCode/Cmdjy/Cmdjy/Web.config b/WebSiteCode/Cmdjy/Cmdjy/Web.config
index a2d1a61..99f2394 100644
--- a/WebSiteCode/Cmdjy/Cmdjy/Web.config
+++ b/WebSiteCode/Cmdjy/Cmdjy/Web.config
@@ -15,13 +15,13 @@
-
-
+
+
+
+
-
+
diff --git a/WebSiteCode/Cmdjy/Cmdjy/WebSettings.cs b/WebSiteCode/Cmdjy/Cmdjy/WebSettings.cs
index f76bbc5..95133b9 100644
--- a/WebSiteCode/Cmdjy/Cmdjy/WebSettings.cs
+++ b/WebSiteCode/Cmdjy/Cmdjy/WebSettings.cs
@@ -11,8 +11,6 @@ namespace Cmdjy
///
/// 获取配置的值
///
- ///
- ///
public static string GetValue(String key) {
var val = ConfigurationManager.AppSettings[key];
return val;
@@ -23,5 +21,11 @@ namespace Cmdjy
public static string DesKey {
get => GetValue("DesKey") ?? throw new Exception("必须在web.config文件appSettings节配置DesKey的值");
}
+ ///
+ /// 获取Redis服务器连接字符串
+ ///
+ public static string RedisConnectionString {
+ get => GetValue("RedisConnectionString");
+ }
}
}
\ No newline at end of file
diff --git a/WebSiteCode/Cmdjy/Cmdjy/packages.config b/WebSiteCode/Cmdjy/Cmdjy/packages.config
index 8a07c5d..850dd99 100644
--- a/WebSiteCode/Cmdjy/Cmdjy/packages.config
+++ b/WebSiteCode/Cmdjy/Cmdjy/packages.config
@@ -26,6 +26,16 @@
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file