2025-11-20 09:50:21 +08:00
|
|
|
|
using Services.Extensions;
|
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
using StackExchange.Redis;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections;
|
|
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Services.Tool
|
|
|
|
|
|
{
|
|
|
|
|
|
#region ConnectionMultiplexer对象管理帮助类
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// ConnectionMultiplexer对象管理帮助类
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static class RedisConnectionHelp
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 系统自定义Key前缀
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static readonly string SysCustomKey = ConfigHelper.GetConfigString("RedisKey") ?? "";
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 链接字符串
|
|
|
|
|
|
/// 127.0.0.1:6379,allowadmin=true
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private static readonly string RedisConnectionString = ConfigHelper.GetConfigString("RedisConn");
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 单例初始化并发锁
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private static readonly object Locker = new object();
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 默认实例
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private static ConnectionMultiplexer _instance;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 链接池
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private static readonly ConcurrentDictionary<string, ConnectionMultiplexer> ConnectionCache = new ConcurrentDictionary<string, ConnectionMultiplexer>();
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取默认单例
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static ConnectionMultiplexer Instance
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_instance == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
lock (Locker)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_instance == null || !_instance.IsConnected)
|
|
|
|
|
|
{
|
|
|
|
|
|
_instance = GetManager();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!_instance.IsConnected)
|
|
|
|
|
|
{
|
|
|
|
|
|
_instance.PublishReconfigure();
|
|
|
|
|
|
}
|
|
|
|
|
|
return _instance;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 缓存链接
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="connectionString"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static ConnectionMultiplexer GetConnectionMultiplexer(string connectionString)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!ConnectionCache.ContainsKey(connectionString))
|
|
|
|
|
|
{
|
|
|
|
|
|
ConnectionCache[connectionString] = GetManager(connectionString);
|
|
|
|
|
|
}
|
|
|
|
|
|
return ConnectionCache[connectionString];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 从配置/字符串获取链接
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="connectionString"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
private static ConnectionMultiplexer GetManager(string connectionString = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
connectionString = connectionString ?? RedisConnectionString;
|
|
|
|
|
|
var connect = ConnectionMultiplexer.Connect(connectionString);
|
|
|
|
|
|
|
|
|
|
|
|
//注册如下事件
|
|
|
|
|
|
connect.ConnectionFailed += MuxerConnectionFailed;
|
|
|
|
|
|
connect.ConnectionRestored += MuxerConnectionRestored;
|
|
|
|
|
|
connect.ErrorMessage += MuxerErrorMessage;
|
|
|
|
|
|
connect.ConfigurationChanged += MuxerConfigurationChanged;
|
|
|
|
|
|
connect.HashSlotMoved += MuxerHashSlotMoved;
|
|
|
|
|
|
connect.InternalError += MuxerInternalError;
|
|
|
|
|
|
|
|
|
|
|
|
return connect;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#region 事件
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 配置更改时
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
|
private static void MuxerConfigurationChanged(object sender, EndPointEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine("Configuration changed: " + e.EndPoint);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 发生错误时
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
|
private static void MuxerErrorMessage(object sender, RedisErrorEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine("ErrorMessage: " + e.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 重新建立连接之前的错误
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
|
private static void MuxerConnectionRestored(object sender, ConnectionFailedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine("ConnectionRestored: " + e.EndPoint);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 连接失败 , 如果重新连接成功你将不会收到这个通知
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
|
private static void MuxerConnectionFailed(object sender, ConnectionFailedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine("重新连接:Endpoint failed: " + e.EndPoint + ", " + e.FailureType + (e.Exception == null ? "" : (", " + e.Exception.Message)));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 更改集群
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
|
private static void MuxerHashSlotMoved(object sender, HashSlotMovedEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine("HashSlotMoved:NewEndPoint" + e.NewEndPoint + ", OldEndPoint" + e.OldEndPoint);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// redis类库错误
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
|
private static void MuxerInternalError(object sender, InternalErrorEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine("InternalError:Message" + e.Exception.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion 事件
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// redis 数据库操作帮助类
|
|
|
|
|
|
/// </summary>
|
2025-12-22 17:47:19 +08:00
|
|
|
|
public class StackChangeRedisHelper
|
2025-11-20 09:50:21 +08:00
|
|
|
|
{
|
|
|
|
|
|
//执行顺序---静态字段---静态构造函数---构造函数
|
|
|
|
|
|
|
|
|
|
|
|
#region 键值前缀
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 数据库缓存前缀,区分站点
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static string systemKey { get { return RedisConnectionHelp.SysCustomKey + "_"; } }
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 对每个key进行前缀的添加处理
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="oldKey">原始key</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static string AddSysCustomKey(string oldKey)
|
|
|
|
|
|
{
|
|
|
|
|
|
string snqwe = string.Format("{0}{1}", systemKey, oldKey);
|
|
|
|
|
|
return snqwe;
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region 数据集/数据库索引
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 数据集/数据库索引
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private static int dbindex = -1;
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 数据集/数据库索引
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static int DBIndex
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
return dbindex;
|
|
|
|
|
|
}
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
dbindex = value;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region 客户端连接
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// redis 客户端服务
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static ConnectionMultiplexer Client
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
return RedisConnectionHelp.Instance;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region DB数据集/数据库实例
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// redis DB数据库实例
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static IDatabase Db
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
return Client.GetDatabase(DBIndex);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region 辅助方法
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 将泛型类型序列化Json字符串
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">泛型 T</typeparam>
|
|
|
|
|
|
/// <param name="value">序列化实例</param>
|
|
|
|
|
|
/// <returns>返回序列化 字符串</returns>
|
|
|
|
|
|
private static string ConvertJson<T>(T value)
|
|
|
|
|
|
{
|
|
|
|
|
|
return value is string ? value.ToString() : JsonConvert.SerializeObject(value, new JsonSerializerSettings()
|
|
|
|
|
|
{
|
|
|
|
|
|
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
|
|
|
|
|
|
Formatting = Formatting.None
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 反序列化
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">泛型T</typeparam>
|
|
|
|
|
|
/// <param name="value">反序列化 字符串</param>
|
|
|
|
|
|
/// <returns>返回 泛型T 实例</returns>
|
|
|
|
|
|
private static T ConvertObj<T>(string value)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (typeof(T).Name.ToLower().Equals("string"))
|
|
|
|
|
|
{
|
|
|
|
|
|
return (T)(Object)value;
|
|
|
|
|
|
}
|
|
|
|
|
|
return JsonConvert.DeserializeObject<T>(value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 序列化列表
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">泛型T</typeparam>
|
|
|
|
|
|
/// <param name="values">RedisValue 集合</param>
|
|
|
|
|
|
/// <returns>返回 泛型T的 Lis 实例T集合</returns>
|
|
|
|
|
|
private static List<T> ConvetList<T>(RedisValue[] values)
|
|
|
|
|
|
{
|
|
|
|
|
|
List<T> result = new List<T>();
|
|
|
|
|
|
if (typeof(T).Name.ToLower().Equals("string"))
|
|
|
|
|
|
{
|
|
|
|
|
|
return values as List<T>;
|
|
|
|
|
|
}
|
|
|
|
|
|
values.ForEach(d =>
|
|
|
|
|
|
{
|
|
|
|
|
|
result.Add(ConvertObj<T>(d));
|
|
|
|
|
|
});
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Redis String
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置Redis String缓存,根据键值 (key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="key">原始key</param>
|
|
|
|
|
|
/// <param name="value">值对象</param>
|
|
|
|
|
|
/// <param name="expiry">设置超时</param>
|
|
|
|
|
|
/// <param name="when">存入动作 <see cref="When"/> 0 Always 无论是否存在现有值,都应该执行该操作 1 xists 该操作应该只在存在值时才执行 2 NotExists 该操作应该只在不存在值的情况下执行</param>
|
|
|
|
|
|
/// <param name="flags">与给定命令关联的行为标记</param>
|
|
|
|
|
|
public static void StringSet(string key, object value, TimeSpan? expiry = default(TimeSpan?), When when = When.Always, CommandFlags flags = CommandFlags.None)
|
|
|
|
|
|
{
|
|
|
|
|
|
StringSetCompleteKey(AddSysCustomKey(key), value, expiry, when, flags);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置Redis String缓存,根据键值(完整键值,不包含系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="CompleteKey">全key</param>
|
|
|
|
|
|
/// <param name="value">值对象</param>
|
|
|
|
|
|
/// <param name="expiry">设置超时</param>
|
|
|
|
|
|
/// <param name="when">存入动作 <see cref="When"/> 0 Always 无论是否存在现有值,都应该执行该操作 1 xists 该操作应该只在存在值时才执行 2 NotExists 该操作应该只在不存在值的情况下执行</param>
|
|
|
|
|
|
/// <param name="flags">与给定命令关联的行为标记</param>
|
|
|
|
|
|
public static void StringSetCompleteKey(string CompleteKey, object value, TimeSpan? expiry = default(TimeSpan?), When when = When.Always, CommandFlags flags = CommandFlags.None)
|
|
|
|
|
|
{
|
|
|
|
|
|
Db.StringSet(CompleteKey, ConvertJson(value), expiry, when, flags);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置Redis String缓存,根据键值 (key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="key">原始key</param>
|
|
|
|
|
|
/// <param name="value">值对象</param>
|
|
|
|
|
|
/// <param name="expiry">设置超时</param>
|
|
|
|
|
|
/// <param name="when">存入动作 <see cref="When"/> 0 Always 无论是否存在现有值,都应该执行该操作 1 xists 该操作应该只在存在值时才执行 2 NotExists 该操作应该只在不存在值的情况下执行</param>
|
|
|
|
|
|
/// <param name="flags">与给定命令关联的行为标记</param>
|
|
|
|
|
|
public static void StringAppend(string key, object value, CommandFlags flags = CommandFlags.None)
|
|
|
|
|
|
{
|
|
|
|
|
|
Db.StringAppend(AddSysCustomKey(key), ConvertJson(value), flags);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置键的字符串值并返回其旧值 (key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">泛型T</typeparam>
|
|
|
|
|
|
/// <param name="key">原始key</param>
|
|
|
|
|
|
/// <param name="value">值对象</param>
|
|
|
|
|
|
/// <returns>返回泛型T实例 旧值</returns>
|
|
|
|
|
|
public static T StringGetAndSet<T>(string key, object value)
|
|
|
|
|
|
{
|
|
|
|
|
|
return StringGetAndSetCompleteKey<T>(AddSysCustomKey(key), value);
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 设置键的字符串值并返回其旧值(完整键值,不包含系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">泛型T</typeparam>
|
|
|
|
|
|
/// <param name="CompleteKey">全key</param>
|
|
|
|
|
|
/// <param name="value">值对象</param>
|
|
|
|
|
|
/// <returns>返回泛型T实例 旧值</returns>
|
|
|
|
|
|
public static T StringGetAndSetCompleteKey<T>(string CompleteKey, object value)
|
|
|
|
|
|
{
|
|
|
|
|
|
string str = Db.StringGetSet(CompleteKey, ConvertJson(value));
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(str))
|
|
|
|
|
|
{
|
|
|
|
|
|
return ConvertObj<T>(str);
|
|
|
|
|
|
}
|
|
|
|
|
|
return default(T);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取Redis String缓存,根据键值 (key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">泛型T</typeparam>
|
|
|
|
|
|
/// <param name="key">原始key</param>
|
|
|
|
|
|
/// <returns>返回泛型T实例</returns>
|
|
|
|
|
|
public static T StringGet<T>(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
return StringGetCompleteKey<T>(AddSysCustomKey(key));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取Redis String缓存,根据键值(完整键值,不包含系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">泛型T</typeparam>
|
|
|
|
|
|
/// <param name="CompleteKey">全key</param>
|
|
|
|
|
|
/// <returns>返回泛型T实例</returns>
|
|
|
|
|
|
public static T StringGetCompleteKey<T>(string CompleteKey)
|
|
|
|
|
|
{
|
|
|
|
|
|
string str = Db.StringGet(CompleteKey);
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(str))
|
|
|
|
|
|
{
|
|
|
|
|
|
return ConvertObj<T>(str);
|
|
|
|
|
|
}
|
|
|
|
|
|
return default(T);
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Redis Hash
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 判断该字段是否存在 hash 中 (key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="key">原始key</param>
|
|
|
|
|
|
/// <param name="hashField">hash Key</param>
|
|
|
|
|
|
/// <returns>返回一个 bool 值 表示该字段是否存在 hash 中</returns>
|
|
|
|
|
|
public static bool HashExists(string key, string hashField)
|
|
|
|
|
|
{
|
|
|
|
|
|
return HashExistsCompleteKey(AddSysCustomKey(key), hashField);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 判断该字段是否存在 hash 中(完整键值,不包含系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="CompleteKey">全key</param>
|
|
|
|
|
|
/// <param name="hashField">hash Key</param>
|
|
|
|
|
|
/// <returns>返回一个 bool 值 表示该字段是否存在 hash 中</returns>
|
|
|
|
|
|
public static bool HashExistsCompleteKey(string CompleteKey, string hashField)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Db.HashExists(CompleteKey, hashField);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 从 hash 中移除指定字段 (key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="key">原始key</param>
|
|
|
|
|
|
/// <param name="hashField">hash Key</param>
|
|
|
|
|
|
/// <returns>返回一个 bool 值 表示从 hash 中移除指定字段 是否成功</returns>
|
|
|
|
|
|
public static bool HashDelete(string key, string hashField)
|
|
|
|
|
|
{
|
|
|
|
|
|
return HashDeleteCompleteKey(AddSysCustomKey(key), hashField);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 从 hash 中移除指定字段 (完整键值,不包含系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="CompleteKey">全key</param>
|
|
|
|
|
|
/// <param name="hashField">hash Key</param>
|
|
|
|
|
|
/// <returns>返回一个 bool 值 表示从 hash 中移除指定字段 是否成功</returns>
|
|
|
|
|
|
public static bool HashDeleteCompleteKey(string CompleteKey, string hashField)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Db.HashDelete(CompleteKey, hashField);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 从 hash 中移除指定字段 批量 (key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="key">原始key</param>
|
|
|
|
|
|
/// <param name="hashFields">hash Key集合</param>
|
|
|
|
|
|
/// <returns>返回一个 long 值 表示 成功操作的数量</returns>
|
|
|
|
|
|
public static long HashDelete(string key, IEnumerable<string> hashFields)
|
|
|
|
|
|
{
|
|
|
|
|
|
return HashDeleteCompleteKey(AddSysCustomKey(key), hashFields);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 从 hash 中移除指定字段 批量 (完整键值,不包含系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="CompleteKey">全key</param>
|
|
|
|
|
|
/// <param name="hashFields">hash Key集合</param>
|
|
|
|
|
|
/// <returns>返回一个 long 值 表示 成功操作的数量</returns>
|
|
|
|
|
|
public static long HashDeleteCompleteKey(string CompleteKey, IEnumerable<string> hashFields)
|
|
|
|
|
|
{
|
|
|
|
|
|
var fields = new List<RedisValue>();
|
|
|
|
|
|
foreach (var item in hashFields)
|
|
|
|
|
|
{
|
|
|
|
|
|
fields.Add(item);
|
|
|
|
|
|
}
|
|
|
|
|
|
return Db.HashDelete(CompleteKey, fields.ToArray());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 在 hash 设定值 (key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="key">原始key</param>
|
|
|
|
|
|
/// <param name="hashField">hash Key</param>
|
|
|
|
|
|
/// <param name="value">值对象</param>
|
|
|
|
|
|
/// <returns>返回一个 bool 值 表示操作是否成功</returns>
|
|
|
|
|
|
public static bool HashSet(string key, string hashField, object value)
|
|
|
|
|
|
{
|
|
|
|
|
|
return HashSetCompleteKey(AddSysCustomKey(key), hashField, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 在 hash 设定值 (完整键值,不包含系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="CompleteKey">全key</param>
|
|
|
|
|
|
/// <param name="hashField">hash Key</param>
|
|
|
|
|
|
/// <param name="value">值对象</param>
|
|
|
|
|
|
/// <returns>返回一个 bool 值 表示操作是否成功</returns>
|
|
|
|
|
|
public static bool HashSetCompleteKey(string CompleteKey, string hashField, object value)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Db.HashSet(CompleteKey, hashField, ConvertJson(value));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 在 hash 中设定值 批量 (key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="key">原始key</param>
|
|
|
|
|
|
/// <param name="hashdic">key-value 字典</param>
|
|
|
|
|
|
public static void HashSet(string key, IDictionary hashdic)
|
|
|
|
|
|
{
|
|
|
|
|
|
HashSetCompleteKey(AddSysCustomKey(key), hashdic);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 在 hash 中设定值 批量 (完整键值,不包含系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="CompleteKey">全key</param>
|
|
|
|
|
|
/// <param name="hashdic">key-value 字典</param>
|
|
|
|
|
|
public static void HashSetCompleteKey(string CompleteKey, IDictionary hashdic)
|
|
|
|
|
|
{
|
|
|
|
|
|
var fields = new List<HashEntry>();
|
|
|
|
|
|
foreach (var item in hashdic.Keys)
|
|
|
|
|
|
{
|
|
|
|
|
|
fields.Add(new HashEntry(item.ToSafeString(), ConvertJson(hashdic[item])));
|
|
|
|
|
|
}
|
|
|
|
|
|
Db.HashSet(CompleteKey, fields.ToArray());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 在 hash 中获取值 (key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">泛型T</typeparam>
|
|
|
|
|
|
/// <param name="key">原始key</param>
|
|
|
|
|
|
/// <param name="hashField">hash Key</param>
|
|
|
|
|
|
/// <returns>返回泛型T实例</returns>
|
|
|
|
|
|
public static T HashGet<T>(string key, string hashField)
|
|
|
|
|
|
{
|
|
|
|
|
|
return HashGetCompleteKey<T>(AddSysCustomKey(key), hashField);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 在 hash 中获取值 (完整键值,不包含系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">泛型T</typeparam>
|
|
|
|
|
|
/// <param name="CompleteKey">全key</param>
|
|
|
|
|
|
/// <param name="hashField">hash Key</param>
|
|
|
|
|
|
/// <returns>返回泛型T实例</returns>
|
|
|
|
|
|
public static T HashGetCompleteKey<T>(string CompleteKey, string hashField)
|
|
|
|
|
|
{
|
|
|
|
|
|
string str = Db.HashGet(CompleteKey, hashField);
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(str))
|
|
|
|
|
|
{
|
|
|
|
|
|
return ConvertObj<T>(str);
|
|
|
|
|
|
}
|
|
|
|
|
|
return default(T);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 在 hash 中获取值集合 (key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">泛型T</typeparam>
|
|
|
|
|
|
/// <param name="key">原始key</param>
|
|
|
|
|
|
/// <param name="hashFields">hash Key集合</param>
|
|
|
|
|
|
/// <returns>返回 泛型T的 Lis 实例T集合</returns>
|
|
|
|
|
|
public static List<T> HashGet<T>(string key, IEnumerable<string> hashFields)
|
|
|
|
|
|
{
|
|
|
|
|
|
return HashGetCompleteKey<T>(AddSysCustomKey(key), hashFields);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 在 hash 中获取值 (完整键值,不包含系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">泛型T</typeparam>
|
|
|
|
|
|
/// <param name="CompleteKey">全key</param>
|
|
|
|
|
|
/// <param name="hashFields">hash Key集合</param>
|
|
|
|
|
|
/// <returns>返回 泛型T的 Lis 实例T集合</returns>
|
|
|
|
|
|
public static List<T> HashGetCompleteKey<T>(string CompleteKey, IEnumerable<string> hashFields)
|
|
|
|
|
|
{
|
|
|
|
|
|
var fields = new List<RedisValue>();
|
|
|
|
|
|
foreach (var item in hashFields)
|
|
|
|
|
|
{
|
|
|
|
|
|
fields.Add(item);
|
|
|
|
|
|
}
|
|
|
|
|
|
var values = Db.HashGet(CompleteKey, fields.ToArray());
|
|
|
|
|
|
return ConvetList<T>(values);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 返回 hash 中的所有字典 (key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">泛型T</typeparam>
|
|
|
|
|
|
/// <param name="key">原始key</param>
|
|
|
|
|
|
/// <returns>返回 泛型T的 字典 实例T集合</returns>
|
|
|
|
|
|
public static Dictionary<string, T> HashGetAll<T>(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
return HashGetAllCompleteKey<T>(AddSysCustomKey(key));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 返回 hash 中的所有字典 (key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">泛型T</typeparam>
|
|
|
|
|
|
/// <param name="CompleteKey">全key</param>
|
|
|
|
|
|
/// <returns>返回 泛型T的 字典 实例T集合</returns>
|
|
|
|
|
|
public static Dictionary<string, T> HashGetAllCompleteKey<T>(string CompleteKey)
|
|
|
|
|
|
{
|
|
|
|
|
|
var hashs = Db.HashGetAll(CompleteKey);
|
|
|
|
|
|
var result = new Dictionary<string, T>();
|
|
|
|
|
|
foreach (var item in hashs)
|
|
|
|
|
|
{
|
|
|
|
|
|
result.Add(item.Name, ConvertObj<T>(item.Value));
|
|
|
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 从 hash 返回所有的字段值 (key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="key">原始key</param>
|
|
|
|
|
|
/// <returns>返回hash key列表</returns>
|
|
|
|
|
|
public static List<string> HashKeys(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
return HashKeysCompleteKey(AddSysCustomKey(key));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 从 hash 返回所有的字段值 (完整键值,不包含系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="CompleteKey">全key</param>
|
|
|
|
|
|
/// <returns>返回hash key列表</returns>
|
|
|
|
|
|
public static List<string> HashKeysCompleteKey(string CompleteKey)
|
|
|
|
|
|
{
|
|
|
|
|
|
var keys = Db.HashKeys(CompleteKey);
|
|
|
|
|
|
return keys.Select(x => x.ToString()).ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 从 hash 返回获取集合中的数量(key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="key">原始key</param>
|
|
|
|
|
|
/// <returns>返回一个 long 值 表示 集合中的数量</returns>
|
|
|
|
|
|
public static long HashLength(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
return HashLengthCompleteKey(AddSysCustomKey(key));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 从 hash 返回获取集合中的数量(完整键值,不包含系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="CompleteKey">全key</param>
|
|
|
|
|
|
/// <returns>返回一个 long 值 表示 集合中的数量</returns>
|
|
|
|
|
|
public static long HashLengthCompleteKey(string CompleteKey)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Db.HashLength(CompleteKey);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 返回 hash 中的所有值 (key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">泛型T</typeparam>
|
|
|
|
|
|
/// <param name="key">原始key</param>
|
|
|
|
|
|
/// <returns>返回 泛型T的 Lis 实例T集合</returns>
|
|
|
|
|
|
public static List<T> HashValues<T>(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
return HashValuesCompleteKey<T>(AddSysCustomKey(key));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 返回 hash 中的所有值 (完整键值,不包含系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">泛型T</typeparam>
|
|
|
|
|
|
/// <param name="CompleteKey">全key</param>
|
|
|
|
|
|
/// <returns>返回 泛型T的 Lis 实例T集合</returns>
|
|
|
|
|
|
public static List<T> HashValuesCompleteKey<T>(string CompleteKey)
|
|
|
|
|
|
{
|
|
|
|
|
|
var values = Db.HashValues(CompleteKey);
|
|
|
|
|
|
return ConvetList<T>(values);
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Redis List
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 移除指定ListId的内部List的值 (key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">泛型T</typeparam>
|
|
|
|
|
|
/// <param name="key">原始key</param>
|
|
|
|
|
|
/// <param name="value">值对象</param>
|
|
|
|
|
|
public static void ListRemove<T>(string key, T value)
|
|
|
|
|
|
{
|
|
|
|
|
|
ListRemoveCompleteKey<T>(AddSysCustomKey(key), value);
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 移除指定ListId的内部List的值(完整键值,不包含系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="CompleteKey">全key</param>
|
|
|
|
|
|
/// <typeparam name="T">泛型T</typeparam>
|
|
|
|
|
|
/// <param name="value">值对象</param>
|
|
|
|
|
|
public static void ListRemoveCompleteKey<T>(string CompleteKey, T value)
|
|
|
|
|
|
{
|
|
|
|
|
|
Db.ListRemove(CompleteKey, ConvertJson<T>(value));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取指定key的List(key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">泛型T</typeparam>
|
|
|
|
|
|
/// <param name="key">原始key</param>
|
|
|
|
|
|
/// <returns>返回 泛型T的 Lis 实例T集合</returns>
|
|
|
|
|
|
public static List<T> ListRange<T>(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
return ListRangeCompleteKey<T>(AddSysCustomKey(key));
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取指定key的List(完整键值,不包含系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">泛型T</typeparam>
|
|
|
|
|
|
/// <param name="CompleteKey">全key</param>
|
|
|
|
|
|
/// <returns>返回 泛型T的 Lis 实例T集合</returns>
|
|
|
|
|
|
public static List<T> ListRangeCompleteKey<T>(string CompleteKey)
|
|
|
|
|
|
{
|
|
|
|
|
|
var values = Db.ListRange(CompleteKey);
|
|
|
|
|
|
return ConvetList<T>(values);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 入队 (key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">泛型T</typeparam>
|
|
|
|
|
|
/// <param name="key">原始key</param>
|
|
|
|
|
|
/// <param name="value">值对象</param>
|
|
|
|
|
|
/// <returns>返回一个 long 值 表示 成功操作的数量</returns>
|
|
|
|
|
|
public static long ListRightPush<T>(string key, T value)
|
|
|
|
|
|
{
|
|
|
|
|
|
return ListRightPushCompleteKey<T>(AddSysCustomKey(key), value);
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 入队 (完整键值,不包含系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">泛型T</typeparam>
|
|
|
|
|
|
/// <param name="CompleteKey">全key</param>
|
|
|
|
|
|
/// <param name="value">值对象</param>
|
|
|
|
|
|
/// <returns>返回一个 long 值 表示 成功操作的数量</returns>
|
|
|
|
|
|
public static long ListRightPushCompleteKey<T>(string CompleteKey, T value)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Db.ListRightPush(CompleteKey, ConvertJson(value));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 出队 (key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">泛型T</typeparam>
|
|
|
|
|
|
/// <param name="key">原始key</param>
|
|
|
|
|
|
/// <returns>返回泛型T实例</returns>
|
|
|
|
|
|
public static T ListRightPop<T>(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
return ListRightPopCompleteKey<T>(AddSysCustomKey(key));
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 出队(完整键值,不包含系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">泛型T</typeparam>
|
|
|
|
|
|
/// <param name="CompleteKey">全key</param>
|
|
|
|
|
|
/// <returns>返回泛型T实例</returns>
|
|
|
|
|
|
public static T ListRightPopCompleteKey<T>(string CompleteKey)
|
|
|
|
|
|
{
|
|
|
|
|
|
string str = Db.ListRightPop(CompleteKey);
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(str))
|
|
|
|
|
|
{
|
|
|
|
|
|
return ConvertObj<T>(str);
|
|
|
|
|
|
}
|
|
|
|
|
|
return default(T);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 入栈 (key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">泛型T</typeparam>
|
|
|
|
|
|
/// <param name="key">原始key</param>
|
|
|
|
|
|
/// <param name="value">值对象</param>
|
|
|
|
|
|
/// <returns>返回一个 long 值 表示 成功操作的数量</returns>
|
|
|
|
|
|
public static long ListLeftPush<T>(string key, T value)
|
|
|
|
|
|
{
|
|
|
|
|
|
return ListLeftPushCompleteKey<T>(AddSysCustomKey(key), value);
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 入栈 (完整键值,不包含系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">泛型T</typeparam>
|
|
|
|
|
|
/// <param name="CompleteKey">全key</param>
|
|
|
|
|
|
/// <param name="value">值对象</param>
|
|
|
|
|
|
/// <returns>返回一个 long 值 表示 成功操作的数量</returns>
|
|
|
|
|
|
public static long ListLeftPushCompleteKey<T>(string CompleteKey, T value)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Db.ListLeftPush(CompleteKey, ConvertJson(value));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 出栈(key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">泛型T</typeparam>
|
|
|
|
|
|
/// <param name="key">原始key</param>
|
|
|
|
|
|
/// <returns>返回泛型T实例</returns>
|
|
|
|
|
|
public static T ListLeftPop<T>(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
return ListLeftPopCompleteKey<T>(AddSysCustomKey(key));
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 出栈(完整键值,不包含系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">泛型T</typeparam>
|
|
|
|
|
|
/// <param name="CompleteKey">全key</param>
|
|
|
|
|
|
/// <returns>返回泛型T实例</returns>
|
|
|
|
|
|
public static T ListLeftPopCompleteKey<T>(string CompleteKey)
|
|
|
|
|
|
{
|
|
|
|
|
|
string str = Db.ListLeftPop(CompleteKey);
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(str))
|
|
|
|
|
|
{
|
|
|
|
|
|
return ConvertObj<T>(str);
|
|
|
|
|
|
}
|
|
|
|
|
|
return default(T);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取集合中的数量(key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="key">原始key</param>
|
|
|
|
|
|
/// <returns>返回一个 long 值 表示 集合中的数量</returns>
|
|
|
|
|
|
public static long ListLength(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
return ListLengthCompleteKey(AddSysCustomKey(key));
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取集合中的数量(完整键值,不包含系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="CompleteKey">全key</param>
|
|
|
|
|
|
/// <returns>返回一个 long 值 表示 集合中的数量</returns>
|
|
|
|
|
|
public static long ListLengthCompleteKey(string CompleteKey)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Db.ListLength(CompleteKey);
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Redis SortedSet
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// SortedSet 新增 (key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="key">原始key</param>
|
|
|
|
|
|
/// <param name="member">成员</param>
|
|
|
|
|
|
/// <param name="score">分数</param>
|
|
|
|
|
|
/// <returns>返回一个 bool 值 表示操作是否成功</returns>
|
|
|
|
|
|
public static bool SortedSetAdd(string key, object member, double score)
|
|
|
|
|
|
{
|
|
|
|
|
|
return SortedSetAddCompleteKey(AddSysCustomKey(key), member, score);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// SortedSet 新增 (完整键值,不包含系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="CompleteKey">全key</param>
|
|
|
|
|
|
/// <param name="member">成员</param>
|
|
|
|
|
|
/// <param name="score">分数</param>
|
|
|
|
|
|
/// <returns>返回一个 bool 值 表示操作是否成功</returns>
|
|
|
|
|
|
public static bool SortedSetAddCompleteKey(string CompleteKey, object member, double score)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Db.SortedSetAdd(CompleteKey, ConvertJson(member), score);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 在有序集合中返回指定范围的元素,默认情况下从低到高。 (key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">泛型T</typeparam>
|
|
|
|
|
|
/// <param name="key">原始key</param>
|
|
|
|
|
|
/// <param name="start">开始条目</param>
|
|
|
|
|
|
/// <param name="stop">结束条目</param>
|
|
|
|
|
|
/// <param name="order">排序类型 <see cref="Order"/> 0Ascending 升序 1Descending降序</param>
|
|
|
|
|
|
/// <returns>返回 泛型T的 Lis 实例T集合</returns>
|
|
|
|
|
|
public static List<T> SortedSetRangeByRank<T>(string key, long start = 0L, long stop = -1L,
|
|
|
|
|
|
int order = 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return SortedSetRangeByRankCompleteKey<T>(AddSysCustomKey(key), start, stop, order);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 在有序集合中返回指定范围的元素,默认情况下从低到高。 (完整键值,不包含系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">泛型T</typeparam>
|
|
|
|
|
|
/// <param name="CompleteKey">全key</param>
|
|
|
|
|
|
/// <param name="start">开始条目</param>
|
|
|
|
|
|
/// <param name="stop">结束条目</param>
|
|
|
|
|
|
/// <param name="order">排序类型 <see cref="Order"/> 0Ascending 升序 1Descending降序</param>
|
|
|
|
|
|
/// <returns>返回 泛型T的 Lis 实例T集合</returns>
|
|
|
|
|
|
public static List<T> SortedSetRangeByRankCompleteKey<T>(string CompleteKey, long start = 0L, long stop = -1L,
|
|
|
|
|
|
int order = 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
var values = Db.SortedSetRangeByRank(CompleteKey, start, stop, (Order)order);
|
|
|
|
|
|
return ConvetList<T>(values);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 返回有序集合的元素个数 (key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="key">原始key</param>
|
|
|
|
|
|
/// <returns>返回一个 long 值 表示 集合中的数量</returns>
|
|
|
|
|
|
public static long SortedSetLength(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
return SortedSetLengthCompleteKey(AddSysCustomKey(key));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 返回有序集合的元素个数 (完整键值,不包含系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="CompleteKey">全key</param>
|
|
|
|
|
|
/// <returns>返回一个 long 值 表示 集合中的数量</returns>
|
|
|
|
|
|
public static long SortedSetLengthCompleteKey(string CompleteKey)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Db.SortedSetLength(CompleteKey);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 移除集合的元素(key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="key">原始key</param>
|
|
|
|
|
|
/// <param name="member">成员</param>
|
|
|
|
|
|
/// <returns>返回一个 bool 值 表示操作是否成功</returns>
|
|
|
|
|
|
public static bool SortedSetRemove(string key, object memebr)
|
|
|
|
|
|
{
|
|
|
|
|
|
return SortedSetRemoveCompleteKey(AddSysCustomKey(key), memebr);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 移除集合的元素(完整键值,不包含系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="CompleteKey">全key</param>
|
|
|
|
|
|
/// <param name="member">成员</param>
|
|
|
|
|
|
/// <returns>返回一个 bool 值 表示操作是否成功</returns>
|
|
|
|
|
|
public static bool SortedSetRemoveCompleteKey(string CompleteKey, object memebr)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Db.SortedSetRemove(CompleteKey, ConvertJson(memebr));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 增量的得分排序的集合中的成员存储键值键按增量 (key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="key">原始key</param>
|
|
|
|
|
|
/// <param name="member">成员</param>
|
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static double SortedSetIncrement(string key, object member, double value = 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
return SortedSetIncrementCompleteKey(AddSysCustomKey(key), member, value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 增量的得分排序的集合中的成员存储键值键按增量 (完整键值,不包含系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="CompleteKey">全key</param>
|
|
|
|
|
|
/// <param name="member">成员</param>
|
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static double SortedSetIncrementCompleteKey(string CompleteKey, object member, double value = 1)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Db.SortedSetIncrement(CompleteKey, ConvertJson(member), value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Redis 订阅
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Redis发布订阅 订阅 (channel自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">泛型T</typeparam>
|
|
|
|
|
|
/// <param name="channel">订阅通道</param>
|
|
|
|
|
|
/// <param name="handler">处理委托方法</param>
|
|
|
|
|
|
public static void Subscribe<T>(string channel, Action<RedisChannel, T> handler = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
SubscribeComplete<T>(AddSysCustomKey(channel), handler);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Redis发布订阅 订阅 (完整键值,不包含系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">泛型T</typeparam>
|
|
|
|
|
|
/// <param name="CompleteChannel">订阅通道 包含系统前缀</param>
|
|
|
|
|
|
/// <param name="handler">处理委托方法</param>
|
|
|
|
|
|
public static void SubscribeComplete<T>(string CompleteChannel, Action<RedisChannel, T> handler = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
var sub = Client.GetSubscriber();
|
|
|
|
|
|
sub.Subscribe(CompleteChannel, (channel, message) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (handler == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine(CompleteChannel + " 订阅收到消息:" + message);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
string valuse = message;
|
|
|
|
|
|
handler(channel, ConvertObj<T>(valuse));
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Redis发布订阅 发布 (channel自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="channel">订阅通道</param>
|
|
|
|
|
|
/// <param name="msg">消息实例</param>
|
|
|
|
|
|
/// <returns>返回一个 long 值 表示 成功操作的数量</returns>
|
|
|
|
|
|
public static long Publish(string channel, object msg)
|
|
|
|
|
|
{
|
|
|
|
|
|
return PublishComplete(AddSysCustomKey(channel), ConvertJson(msg));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Redis发布订阅 发布 (完整键值,不包含系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="CompleteChannel">订阅通道</param>
|
|
|
|
|
|
/// <param name="msg">消息实例</param>
|
|
|
|
|
|
/// <returns>返回一个 long 值 表示 成功操作的数量</returns>
|
|
|
|
|
|
public static long PublishComplete(string CompleteChannel, object msg)
|
|
|
|
|
|
{
|
|
|
|
|
|
var sub = Client.GetSubscriber();
|
|
|
|
|
|
return sub.Publish(CompleteChannel, ConvertJson(msg));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Redis发布订阅 取消订阅 (channel自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="channel">订阅通道</param>
|
|
|
|
|
|
public static void Unsubscribe(string channel)
|
|
|
|
|
|
{
|
|
|
|
|
|
UnsubscribeComplete(AddSysCustomKey(channel));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Redis发布订阅 取消订阅 (完整键值,不包含系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="channel">订阅通道</param>
|
|
|
|
|
|
public static void UnsubscribeComplete(string CompleteChannel)
|
|
|
|
|
|
{
|
|
|
|
|
|
var sub = Client.GetSubscriber();
|
|
|
|
|
|
sub.Unsubscribe(CompleteChannel);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Redis发布订阅 取消全部订阅
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public static void UnsubscribeAll()
|
|
|
|
|
|
{
|
|
|
|
|
|
var sub = Client.GetSubscriber();
|
|
|
|
|
|
sub.UnsubscribeAll();
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region 建值管理
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 检查redis key建值,根据键值 (key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="key">原始key</param>
|
|
|
|
|
|
/// <returns>返回一个 bool 值 表示该建值是否存在 hash 中</returns>
|
|
|
|
|
|
public static bool CheckKey(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
return CheckKeyCompleteKey(AddSysCustomKey(key));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 检查redis key建值,根据键值 (完整键值,不包含系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="CompleteKey">全key</param>
|
|
|
|
|
|
/// <returns>返回一个 bool 值 表示该建值是否存在 hash 中</returns>
|
|
|
|
|
|
public static bool CheckKeyCompleteKey(string CompleteKey)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Db.KeyExists(CompleteKey);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取redis据键值列表,只获取本系统的系统前缀名称
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>返回键值列表</returns>
|
|
|
|
|
|
public static List<string> GetAllKey()
|
|
|
|
|
|
{
|
|
|
|
|
|
List<string> list = new List<string>();
|
|
|
|
|
|
foreach (var item in Client.GetEndPoints())
|
|
|
|
|
|
{
|
|
|
|
|
|
var server = Client.GetServer(item);
|
|
|
|
|
|
var temp = server.Keys(0, "*").Select(d => d.ToString()).Where(x => x.StartsWith(systemKey)).ToList();
|
|
|
|
|
|
list.AddRange(temp);
|
|
|
|
|
|
}
|
|
|
|
|
|
return list;
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region 移除键值
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 移除redis键值,根据键值 (key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="key">原始key</param>
|
|
|
|
|
|
/// <returns>返回一个 bool 值 表示操作是否成功</returns>
|
|
|
|
|
|
public static bool RemoveKey(string key)
|
|
|
|
|
|
{
|
|
|
|
|
|
return RemoveKeyCompleteKey(AddSysCustomKey(key));
|
|
|
|
|
|
}
|
|
|
|
|
|
//批量删除key,用于一些批量任务的场合,比如同步任务。
|
|
|
|
|
|
//不管有没有,都删除。
|
|
|
|
|
|
//redis的文档说,如果没有key,删除了也不会报错。
|
|
|
|
|
|
//所以永远返回true
|
|
|
|
|
|
public static bool RemoveKeyList(List<string> keylist)
|
|
|
|
|
|
{
|
|
|
|
|
|
//给每一个key,添加前缀。
|
|
|
|
|
|
List<string> preNameList = new List<string>();
|
|
|
|
|
|
foreach (var item in keylist)
|
|
|
|
|
|
{
|
|
|
|
|
|
preNameList.Add(AddSysCustomKey(item));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
RemoveKeyCompleteKeyList(preNameList);
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 移除redis键值,根据键值 (完整键值,不包含系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="CompleteKey">全key</param>
|
|
|
|
|
|
/// <returns>返回一个 bool 值 表示操作是否成功</returns>
|
|
|
|
|
|
public static bool RemoveKeyCompleteKey(string CompleteKey)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!Db.KeyExists(CompleteKey))
|
|
|
|
|
|
return true;
|
|
|
|
|
|
return Db.KeyDelete(CompleteKey);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//批量删除key,用于一些批量任务的场合,比如同步任务。
|
|
|
|
|
|
//不管有没有,都删除。
|
|
|
|
|
|
//redis的文档说,如果没有key,删除了也不会报错。
|
|
|
|
|
|
//所以永远返回true
|
|
|
|
|
|
public static bool RemoveKeyCompleteKeyList(List<string> CompleteKey)
|
|
|
|
|
|
{
|
|
|
|
|
|
//将字符串列表,转换成 RedisKey 数组
|
|
|
|
|
|
RedisKey[] arrKeys = new RedisKey[CompleteKey.Count];
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
foreach(var p in CompleteKey)
|
|
|
|
|
|
{
|
|
|
|
|
|
arrKeys[i] = (RedisKey)p; //RedisKey类型支持从字符串类型的隐式转换
|
|
|
|
|
|
i++;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//调用redis的批量删除功能。
|
|
|
|
|
|
long affectkeys = Db.KeyDelete(arrKeys);
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 清空所有指定前缀参数集合键值 (key自动添加系统名称前缀)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="key">原始key</param>
|
|
|
|
|
|
/// <returns>返回一个 long 值 表示 成功操作的数量</returns>
|
|
|
|
|
|
public static long ClearAllStartsWithKey(string key = "")
|
|
|
|
|
|
{
|
|
|
|
|
|
long count = 0;
|
|
|
|
|
|
foreach (var item in GetAllKey())
|
|
|
|
|
|
{
|
|
|
|
|
|
if (item.StartsWith(AddSysCustomKey(key)))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Db.KeyDelete(item)) count++;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return count;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 清空所有键值
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>返回一个 long 值 表示 成功操作的数量</returns>
|
|
|
|
|
|
public static long ClearAllKey()
|
|
|
|
|
|
{
|
|
|
|
|
|
long count = 0;
|
|
|
|
|
|
foreach (var item in GetAllKey())
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Db.KeyDelete(item)) count++;
|
|
|
|
|
|
}
|
|
|
|
|
|
return count;
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|