From dd045eb51640102b1f229541afbeeb2110ad2430 Mon Sep 17 00:00:00 2001
From: geh <1923421292@qq.com>
Date: Mon, 14 Apr 2025 20:35:08 +0800
Subject: [PATCH] 11
---
SUBQHSE/BLL/Common/Redis/ICache.cs | 127 ++
SUBQHSE/BLL/Common/Redis/Redis.cs | 308 ++++
SUBQHSE/BLL/Common/Redis/RedisHelper.cs | 2077 +++++++++++++++++++++++
3 files changed, 2512 insertions(+)
create mode 100644 SUBQHSE/BLL/Common/Redis/ICache.cs
create mode 100644 SUBQHSE/BLL/Common/Redis/Redis.cs
create mode 100644 SUBQHSE/BLL/Common/Redis/RedisHelper.cs
diff --git a/SUBQHSE/BLL/Common/Redis/ICache.cs b/SUBQHSE/BLL/Common/Redis/ICache.cs
new file mode 100644
index 0000000..09f758f
--- /dev/null
+++ b/SUBQHSE/BLL/Common/Redis/ICache.cs
@@ -0,0 +1,127 @@
+using System;
+using StackExchange.Redis;
+
+namespace BLL
+{
+ ///
+ /// 接口
+ ///
+ public interface ICache
+ {
+ ///
+ /// 缓存过期时间
+ ///
+ int TimeOut { set; get; }
+ ///
+ /// 获得指定键的缓存值
+ ///
+ /// 缓存键
+ /// 缓存值
+ object Get(string key);
+ ///
+ /// 获得指定键的缓存值
+ ///
+ T Get(string key);
+ ///
+ /// 从缓存中移除指定键的缓存值
+ ///
+ /// 缓存键
+ void Remove(string key);
+ ///
+ /// 清空所有缓存对象
+ ///
+ //void Clear();
+ ///
+ /// 将指定键的对象添加到缓存中
+ ///
+ /// 缓存键
+ /// 缓存值
+ void Insert(string key, object data);
+ ///
+ /// 将指定键的对象添加到缓存中
+ ///
+ /// 缓存键
+ /// 缓存值
+ void Insert(string key, T data);
+ ///
+ /// 将指定键的对象添加到缓存中,并指定过期时间
+ ///
+ /// 缓存键
+ /// 缓存值
+ /// 缓存过期时间(秒钟)
+ void Insert(string key, object data, int cacheTime);
+
+ ///
+ /// 将指定键的对象添加到缓存中,并指定过期时间
+ ///
+ /// 缓存键
+ /// 缓存值
+ /// 缓存过期时间(秒钟)
+ void Insert(string key, T data, int cacheTime);
+ ///
+ /// 将指定键的对象添加到缓存中,并指定过期时间
+ ///
+ /// 缓存键
+ /// 缓存值
+ /// 缓存过期时间
+ void Insert(string key, object data, DateTime cacheTime);
+ ///
+ /// 将指定键的对象添加到缓存中,并指定过期时间
+ ///
+ /// 缓存键
+ /// 缓存值
+ /// 缓存过期时间
+ void Insert(string key, T data, DateTime cacheTime);
+ ///
+ /// 判断key是否存在
+ ///
+ bool Exists(string key);
+ ///
+ /// 右侧入队
+ ///
+ ///
+ ///
+ ///
+ long EnqueueListRightPush(RedisKey queueName, RedisValue redisValue);
+ ///
+ /// 左侧入队
+ ///
+ ///
+ ///
+ ///
+ long EnqueueListLeftPush(RedisKey queueName, RedisValue redisvalue);
+ ///
+ /// 获取队列长度
+ ///
+ ///
+ ///
+ long EnqueueListLength(RedisKey queueName);
+ ///
+ /// 左侧出队
+ ///
+ ///
+ ///
+ string DequeueListPopLeft(RedisKey queueName);
+ ///
+ /// 右侧出队
+ ///
+ ///
+ ///
+ string DequeueListPopRight(RedisKey queueName);
+ ///
+ /// 分布式加锁
+ ///
+ /// 键
+ /// 值
+ /// 过期时间
+ ///
+ bool LockTake(string key, string data, TimeSpan seconds, int db = 0);
+ ///
+ /// 解锁
+ ///
+ /// 键
+ /// 值
+ ///
+ bool LockRelease(string key, string data, int db = -1);
+ }
+}
\ No newline at end of file
diff --git a/SUBQHSE/BLL/Common/Redis/Redis.cs b/SUBQHSE/BLL/Common/Redis/Redis.cs
new file mode 100644
index 0000000..c2ece3c
--- /dev/null
+++ b/SUBQHSE/BLL/Common/Redis/Redis.cs
@@ -0,0 +1,308 @@
+using System;
+using System.Configuration;
+using Newtonsoft.Json;
+using StackExchange.Redis;
+
+namespace BLL
+{
+ ///
+ ///
+ ///
+ public class Redis : ICache
+ {
+ int Default_Timeout = 600;//默认超时时间(单位秒)
+ string address;
+ JsonSerializerSettings jsonConfig = new JsonSerializerSettings() { ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore, NullValueHandling = NullValueHandling.Ignore };
+ ConnectionMultiplexer connectionMultiplexer;
+ IDatabase database;
+
+ class CacheObject
+ {
+ public int ExpireTime { get; set; }
+ public bool ForceOutofDate { get; set; }
+ public T Value { get; set; }
+ }
+
+ ///
+ ///
+ ///
+ ///
+ public Redis(int dbbase = 0)
+ {
+ this.address = this.address = ConfigurationManager.AppSettings["RedisHosts"];
+
+ if (this.address == null || string.IsNullOrWhiteSpace(this.address.ToString()))
+ throw new ApplicationException("配置文件中未找到RedisServer的有效配置");
+ connectionMultiplexer = ConnectionMultiplexer.Connect(address);
+ database = connectionMultiplexer.GetDatabase(dbbase);
+ }
+
+ ///
+ /// 连接超时设置
+ ///
+ public int TimeOut
+ {
+ get
+ {
+ return Default_Timeout;
+ }
+ set
+ {
+ Default_Timeout = value;
+ }
+ }
+ ///
+ ///
+ ///
+ ///
+ ///
+ public object Get(string key)
+ {
+ return Get