在NuGet直接搜索StackExchange.Redis,下载引用包;

帮助类

/***********************************
* Developer: Lio.Huang
* Date:2018-11-21
**********************************/
using System;
using System.Collections.Generic;
using System.Linq;
using StackExchange.Redis;
using Newtonsoft.Json;
using System.Threading.Tasks; namespace Banana.Utility.Redis
{
/// <summary>
/// Redis Base Utils
/// </summary>
public class RedisUtils
{
/// <summary>
/// redis配置文件信息
/// </summary>
public static string RedisPath = "172.16.3.82:6379"; /// <summary>
/// 注册地址
/// </summary>
/// <param name="redisPath">redis connection string</param>
public static void Register(string redisPath)
{
RedisPath = redisPath;
} private static ConnectionMultiplexer _instance = null; /// <summary>
/// 使用一个静态属性来返回已连接的实例,如下列中所示。这样,一旦 ConnectionMultiplexer 断开连接,便可以初始化新的连接实例。
/// </summary>
public static ConnectionMultiplexer Instance
{
get
{
return conn.Value;
}
} //public static ConnectionMultiplexer Instance
//{
// get
// {
// //if (_instance == null)
// //{
// // lock (_locker)
// // {
// if (_instance == null || !_instance.IsConnected)
// {
// _instance = ConnectionMultiplexer.Connect(RedisPath);
// //注册如下事件
// _instance.ConnectionFailed += MuxerConnectionFailed;
// _instance.ConnectionRestored += MuxerConnectionRestored;
// _instance.ErrorMessage += MuxerErrorMessage;
// _instance.HashSlotMoved += MuxerHashSlotMoved;
// _instance.InternalError += MuxerInternalError;
// }
// // }
// //}
// return _instance;
// }
//} private static Lazy<ConnectionMultiplexer> conn = new Lazy<ConnectionMultiplexer>(
() =>
{
_instance = ConnectionMultiplexer.Connect(RedisPath);
//注册如下事件
_instance.ConnectionFailed += MuxerConnectionFailed;
_instance.ConnectionRestored += MuxerConnectionRestored;
_instance.ErrorMessage += MuxerErrorMessage;
_instance.HashSlotMoved += MuxerHashSlotMoved;
_instance.InternalError += MuxerInternalError;
return _instance;
}
); #region Keys
/// <summary>
/// 判断键是否存在
/// </summary>
/// <param name="dbIndex">数据库</param>
/// <param name="key">键值</param>
/// <returns></returns>
public static bool KeyExists(int dbIndex, string key)
{
var db = Instance.GetDatabase(dbIndex);
return db.KeyExists(key);
} /// <summary>
/// 为指定的键设置失效时间
/// </summary>
/// <param name="dbIndex">数据库</param>
/// <param name="key">键</param>
/// <param name="expiry">时间间隔</param>
/// <returns></returns>
public static bool SetExpire(int dbIndex, string key, TimeSpan? expiry)
{
var db = Instance.GetDatabase(dbIndex);
return db.KeyExpire(key, expiry);
} /// <summary>
/// 为指定的键设置失效时间
/// </summary>
/// <param name="dbIndex">数据库</param>
/// <param name="key">键</param>
/// <param name="timeout">时间间隔(秒)</param>
/// <returns></returns>
public static bool SetExpire(int dbIndex, string key, int timeout = )
{
var db = Instance.GetDatabase(dbIndex);
return db.KeyExpire(key, DateTime.Now.AddSeconds(timeout));
} /// <summary>
/// 删除键
/// </summary>
/// <param name="dbIndex">数据库</param>
/// <param name="key">键</param>
/// <returns></returns>
public static bool KeyDelete(int dbIndex, string key)
{
var db = Instance.GetDatabase(dbIndex);
return db.KeyDelete(key);
} /// <summary>
/// 键重命名
/// </summary>
/// <param name="dbIndex">数据库</param>
/// <param name="oldKey">旧值</param>
/// <param name="newKey">新值</param>
/// <returns></returns>
public static bool KeyRename(int dbIndex, string oldKey, string newKey)
{
var db = Instance.GetDatabase(dbIndex);
return db.KeyRename(oldKey, newKey);
}
#endregion #region Strings
/// <summary>
/// 获取字符串数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dbIndex">数据库</param>
/// <param name="key">Redis键</param>
/// <returns></returns>
public static string StringGet(int dbIndex, string key)
{
var db = Instance.GetDatabase(dbIndex);
if (db != null)
{
return db.StringGet(key);
}
return string.Empty;
} /// <summary>
/// 获取对象类型数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dbIndex">数据库</param>
/// <param name="key">Redis键</param>
/// <returns></returns>
public static T StringGet<T>(int dbIndex, string key) where T : class
{
T data = default(T);
var db = Instance.GetDatabase(dbIndex);
if (db != null)
{
var value = db.StringGet(key);
return ConvertObj<T>(value);
}
return data;
} /// <summary>
/// 设置值类型的值
/// </summary>
/// <param name="dbIndex">数据库</param>
/// <param name="key">键</param>
/// <param name="value">值</param>
public static bool StringSet(int dbIndex, string key, RedisValue value, TimeSpan? expiry)
{
var db = Instance.GetDatabase(dbIndex);
return db.StringSet(key, value, expiry);
} /// <summary>
/// 设置对象类型的值
/// </summary>
/// <param name="dbIndex">数据库</param>
/// <param name="key">键</param>
/// <param name="value">值</param>
public static bool StringSet<T>(int dbIndex, string key, T value, TimeSpan? expiry) where T : class
{
if (value == default(T))
{
return false;
}
var db = Instance.GetDatabase(dbIndex);
return db.StringSet(key, ConvertJson(value), expiry);
}
#endregion #region Hashes
/// <summary>
/// Hash是否存在
/// </summary>
/// <param name="dbIndex">数据库</param>
/// <param name="hashId">HashId</param>
/// <param name="key">键</param>
/// <returns></returns>
public static bool HashExists(int dbIndex, string hashId, string key)
{
var db = Instance.GetDatabase(dbIndex);
return db.HashExists(key, hashId);
} /// <summary>
/// Hash长度
/// </summary>
/// <param name="dbIndex">数据库</param>
/// <param name="hashId">HashId</param>
/// <returns></returns>
public static long HashLength(int dbIndex, string hashId)
{
var db = Instance.GetDatabase(dbIndex);
var length = db.HashLength(hashId);
return length;
} /// <summary>
/// 设置哈希值
/// </summary>
/// <typeparam name="T">哈希值类型</typeparam>
/// <param name="dbIndex">数据库</param>
/// <param name="hashId">哈希ID</param>
/// <param name="key">键</param>
/// <param name="t">哈希值</param>
/// <returns></returns>
public static bool HashSet<T>(int dbIndex, string hashId, string key, T t)
{
var db = Instance.GetDatabase(dbIndex);
return db.HashSet(hashId, key, ConvertJson(t));
} /// <summary>
/// 获取哈希值
/// </summary>
/// <typeparam name="T">哈希值类型</typeparam>
/// <param name="dbIndex">数据库</param>
/// <param name="hashId">哈希ID</param>
/// <param name="key">键</param>
/// <returns></returns>
public static T HashGet<T>(int dbIndex, string hashId, string key)
{
var db = Instance.GetDatabase(dbIndex);
string value = db.HashGet(hashId, key);
if (string.IsNullOrWhiteSpace(value))
return default(T);
return ConvertObj<T>(value);
} /// <summary>
/// 获取哈希值
/// </summary>
/// <param name="dbIndex">数据库</param>
/// <param name="hashId">哈希ID</param>
/// <param name="key">键</param>
/// <returns></returns>
public static string HashGet(int dbIndex, string hashId, string key)
{
var db = Instance.GetDatabase(dbIndex);
return db.HashGet(hashId, key).ToString();
} /// <summary>
/// 获取哈希值的所有键
/// </summary>
/// <param name="dbIndex">数据库</param>
/// <param name="hashId">哈希ID</param>
/// <returns></returns>
public static List<string> HashKeys(int dbIndex, string hashId)
{
var db = Instance.GetDatabase(dbIndex);
var result = new List<string>();
var list = db.HashKeys(hashId).ToList();
if (list.Count > )
{
list.ForEach(x =>
{
result.Add(ConvertObj<string>(x));
});
}
return result;
} /// <summary>
/// 获取所有哈希值
/// </summary>
/// <typeparam name="T">哈希值类型</typeparam>
/// <param name="dbIndex">数据库</param>
/// <param name="hashId">哈希ID</param>
/// <returns></returns>
public static List<T> HashGetAll<T>(int dbIndex, string hashId)
{
var db = Instance.GetDatabase(dbIndex);
var result = new List<T>();
var list = db.HashGetAll(hashId).ToList();
if (list.Count > )
{
list.ForEach(x =>
{
result.Add(ConvertObj<T>(x.Value));
});
}
return result;
} /// <summary>
/// 删除哈希值
/// </summary>
/// <param name="dbIndex">数据库</param>
/// <param name="hashId">哈希ID</param>
/// <param name="key">键</param>
/// <returns></returns>
public static bool HashDelete(int dbIndex, string hashId, string key)
{
var db = Instance.GetDatabase(dbIndex);
return db.HashDelete(hashId, key);
}
#endregion #region Lists
/// <summary>
/// 集合长度
/// </summary>
/// <param name="dbIndex">数据库</param>
/// <param name="listId">集合ID</param>
/// <returns></returns>
public static long ListLength(int dbIndex, string listId)
{
var db = Instance.GetDatabase(dbIndex);
return db.ListLength(listId);
} /// <summary>
/// 向集合中添加元素
/// </summary>
/// <typeparam name="T">元素类型</typeparam>
/// <param name="dbIndex">数据库</param>
/// <param name="listId">集合ID</param>
/// <param name="list">元素值</param>
/// <returns></returns>
public static long AddList<T>(int dbIndex, string listId, List<T> list)
{
var db = Instance.GetDatabase(dbIndex);
if (list != null && list.Count > )
{
foreach (var item in list)
{
db.ListRightPush(listId, ConvertJson(item));
}
}
return db.ListLength(listId);
} /// <summary>
/// 获取集合元素(默认获取整个集合)
/// </summary>
/// <typeparam name="T">元素类型</typeparam>
/// <param name="dbIndex">数据库</param>
/// <param name="listId">集合ID</param>
/// <param name="start">起始位置(0表示第1个位置)</param>
/// <param name="stop">结束位置(-1表示倒数第1个位置)</param>
/// <returns></returns>
public static List<T> GetList<T>(int dbIndex, string listId, long start = , long stop = -)
{
var db = Instance.GetDatabase(dbIndex);
var result = new List<T>();
var list = db.ListRange(listId, start, stop).ToList();
if (list.Count > )
{
list.ForEach(x =>
{
result.Add(ConvertObj<T>(x));
});
}
return result;
}
#endregion #region ZSet #region 同步方法 /// <summary>
/// 添加一个值到Key
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dbIndex">数据库</param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="score">排序分数,为空将获取集合中最大score加1</param>
/// <returns></returns>
public static bool SortedSetAdd<T>(int dbIndex, string key, T value, double? score = null)
{
var db = Instance.GetDatabase(dbIndex);
double scoreNum = score ?? _GetScore(key, db);
return db.SortedSetAdd(key, ConvertJson<T>(value), scoreNum);
} /// <summary>
/// 添加一个集合到Key
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dbIndex">数据库</param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="score">排序分数,为空将获取集合中最大score加1</param>
/// <returns></returns>
public static long SortedSetAdd<T>(int dbIndex, string key, List<T> value, double? score = null)
{
var db = Instance.GetDatabase(dbIndex);
double scoreNum = score ?? _GetScore(key, db);
SortedSetEntry[] rValue = value.Select(o => new SortedSetEntry(ConvertJson<T>(o), scoreNum++)).ToArray();
return db.SortedSetAdd(key, rValue);
} /// <summary>
/// 获取集合中的数量
/// </summary>
/// <param name="dbIndex">数据库</param>
/// <param name="key"></param>
/// <returns></returns>
public static long SortedSetLength(int dbIndex,string key)
{
var db = Instance.GetDatabase(dbIndex);
return db.SortedSetLength(key);
} /// <summary>
/// 获取指定起始值到结束值的集合数量
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dbIndex">数据库</param>
/// <param name="key"></param>
/// <param name="startValue">起始值</param>
/// <param name="endValue">结束值</param>
/// <returns></returns>
public static long SortedSetLengthByValue<T>(int dbIndex, string key, T startValue, T endValue)
{
var db = Instance.GetDatabase(dbIndex);
var sValue = ConvertJson<T>(startValue);
var eValue = ConvertJson<T>(endValue);
return db.SortedSetLengthByValue(key, sValue, eValue);
} /// <summary>
/// 获取指定Key的排序Score值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dbIndex">数据库</param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public static double? SortedSetScore<T>(int dbIndex, string key, T value)
{
var db = Instance.GetDatabase(dbIndex);
var rValue = ConvertJson<T>(value);
return db.SortedSetScore(key, rValue);
} /// <summary>
/// 获取指定Key中最小Score值
/// </summary>
/// <param name="dbIndex">数据库</param>
/// <param name="key"></param>
/// <returns></returns>
public static double SortedSetMinScore(int dbIndex, string key)
{
var db = Instance.GetDatabase(dbIndex);
double dValue = ;
var rValue = db.SortedSetRangeByRankWithScores(key, , , Order.Ascending).FirstOrDefault();
dValue = rValue != null ? rValue.Score : ;
return dValue;
} /// <summary>
/// 获取指定Key中最大Score值
/// </summary>
/// <param name="dbIndex">数据库</param>
/// <param name="key"></param>
/// <returns></returns>
public static double SortedSetMaxScore(int dbIndex, string key)
{
var db = Instance.GetDatabase(dbIndex);
double dValue = ;
var rValue = db.SortedSetRangeByRankWithScores(key, , , Order.Descending).FirstOrDefault();
dValue = rValue != null ? rValue.Score : ;
return dValue;
} /// <summary>
/// 删除Key中指定的值
/// </summary>
/// <param name="dbIndex">数据库</param>
/// <param name="key"></param>
/// <param name="value"></param>
public static long SortedSetRemove<T>(int dbIndex, string key, params T[] value)
{
var db = Instance.GetDatabase(dbIndex);
var rValue = ConvertRedisValue<T>(value);
return db.SortedSetRemove(key, rValue);
} /// <summary>
/// 删除指定起始值到结束值的数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dbIndex">数据库</param>
/// <param name="key"></param>
/// <param name="startValue">起始值</param>
/// <param name="endValue">结束值</param>
/// <returns></returns>
public static long SortedSetRemoveRangeByValue<T>(int dbIndex, string key, T startValue, T endValue)
{
var db = Instance.GetDatabase(dbIndex);
var sValue = ConvertJson<T>(startValue);
var eValue = ConvertJson<T>(endValue);
return db.SortedSetRemoveRangeByValue(key, sValue, eValue);
} /// <summary>
/// 删除 从 start 开始的 stop 条数据
/// </summary>
/// <param name="dbIndex">数据库</param>
/// <param name="key"></param>
/// <param name="start"></param>
/// <param name="stop"></param>
/// <returns></returns>
public static long SortedSetRemoveRangeByRank(int dbIndex, string key, long start, long stop)
{
var db = Instance.GetDatabase(dbIndex);
return db.SortedSetRemoveRangeByRank(key, start, stop);
} /// <summary>
/// 根据排序分数Score,删除从 start 开始的 stop 条数据
/// </summary>
/// <param name="dbIndex">数据库</param>
/// <param name="key"></param>
/// <param name="start"></param>
/// <param name="stop"></param>
/// <returns></returns>
public static long SortedSetRemoveRangeByScore(int dbIndex, string key, double start, double stop)
{
var db = Instance.GetDatabase(dbIndex);
return db.SortedSetRemoveRangeByScore(key, start, stop);
} /// <summary>
/// 获取从 start 开始的 stop 条数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dbIndex">数据库</param>
/// <param name="key"></param>
/// <param name="start">起始数</param>
/// <param name="stop">-1表示到结束,0为1条</param>
/// <param name="desc">是否按降序排列</param>
/// <returns></returns>
public static List<T> SortedSetRangeByRank<T>(int dbIndex, string key, long start = , long stop = -, bool desc = false)
{
var db = Instance.GetDatabase(dbIndex);
Order orderBy = desc ? Order.Descending : Order.Ascending;
var rValue = db.SortedSetRangeByRank(key, start, stop, orderBy);
return ConvetList<T>(rValue);
}
#endregion #region 异步方法 /// <summary>
/// 添加一个值到Key
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dbIndex">数据库</param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="score">排序分数,为空将获取集合中最大score加1</param>
/// <returns></returns>
public static async Task<bool> SortedSetAddAsync<T>(int dbIndex, string key, T value, double? score = null)
{
var db = Instance.GetDatabase(dbIndex);
double scoreNum = score ?? _GetScore(key, db);
return await db.SortedSetAddAsync(key, ConvertJson<T>(value), scoreNum);
} /// <summary>
/// 添加一个集合到Key
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dbIndex">数据库</param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="score">排序分数,为空将获取集合中最大score加1</param>
/// <returns></returns>
public static async Task<long> SortedSetAddAsync<T>(int dbIndex, string key, List<T> value, double? score = null)
{
var db = Instance.GetDatabase(dbIndex);
double scoreNum = score ?? _GetScore(key, db);
SortedSetEntry[] rValue = value.Select(o => new SortedSetEntry(ConvertJson<T>(o), scoreNum++)).ToArray();
return await db.SortedSetAddAsync(key, rValue);
} /// <summary>
/// 获取集合中的数量
/// </summary>
/// <param name="dbIndex">数据库</param>
/// <param name="key"></param>
/// <returns></returns>
public static async Task<long> SortedSetLengthAsync(int dbIndex, string key)
{
var db = Instance.GetDatabase(dbIndex);
return await db.SortedSetLengthAsync(key);
} /// <summary>
/// 获取指定起始值到结束值的集合数量
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dbIndex">数据库</param>
/// <param name="key"></param>
/// <param name="startValue">起始值</param>
/// <param name="endValue">结束值</param>
/// <returns></returns>
public static async Task<long> SortedSetLengthByValueAsync<T>(int dbIndex, string key, T startValue, T endValue)
{
var db = Instance.GetDatabase(dbIndex);
var sValue = ConvertJson<T>(startValue);
var eValue = ConvertJson<T>(endValue);
return await db.SortedSetLengthByValueAsync(key, sValue, eValue);
} /// <summary>
/// 获取指定Key中最大Score值
/// </summary>
/// <param name="dbIndex">数据库</param>
/// <param name="key"></param>
/// <returns></returns>
public static async Task<double> SortedSetMaxScoreAsync(int dbIndex, string key)
{
var db = Instance.GetDatabase(dbIndex);
double dValue = ;
var rValue = (await db.SortedSetRangeByRankWithScoresAsync(key, , , Order.Descending)).FirstOrDefault();
dValue = rValue != null ? rValue.Score : ;
return dValue;
} /// <summary>
/// 删除Key中指定的值
/// </summary>
/// <param name="dbIndex">数据库</param>
/// <param name="key"></param>
/// <param name="value"></param>
public static async Task<long> SortedSetRemoveAsync<T>(int dbIndex, string key, params T[] value)
{
var db = Instance.GetDatabase(dbIndex);
var rValue = ConvertRedisValue<T>(value);
return await db.SortedSetRemoveAsync(key, rValue);
} /// <summary>
/// 删除指定起始值到结束值的数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dbIndex">数据库</param>
/// <param name="key"></param>
/// <param name="startValue">起始值</param>
/// <param name="endValue">结束值</param>
/// <returns></returns>
public static async Task<long> SortedSetRemoveRangeByValueAsync<T>(int dbIndex, string key, T startValue, T endValue)
{
var db = Instance.GetDatabase(dbIndex);
var sValue = ConvertJson<T>(startValue);
var eValue = ConvertJson<T>(endValue);
return await db.SortedSetRemoveRangeByValueAsync(key, sValue, eValue);
} /// <summary>
/// 删除 从 start 开始的 stop 条数据
/// </summary>
/// <param name="dbIndex">数据库</param>
/// <param name="key"></param>
/// <param name="start"></param>
/// <param name="stop"></param>
/// <returns></returns>
public static async Task<long> SortedSetRemoveRangeByRankAsync(int dbIndex, string key, long start, long stop)
{
var db = Instance.GetDatabase(dbIndex);
return await db.SortedSetRemoveRangeByRankAsync(key, start, stop);
} #endregion #region private method
/// <summary>
/// 获取几个集合的交叉并集合,并保存到一个新Key中
/// </summary>
/// <param name="db"></param>
/// <param name="operation">Union:并集 Intersect:交集 Difference:差集 详见 <see cref="SetOperation"/></param>
/// <param name="destination">保存的新Key名称</param>
/// <param name="keys">要操作的Key集合</param>
/// <returns></returns>
private static long _SortedSetCombineAndStore(IDatabase db, SetOperation operation, string destination, params string[] keys)
{
RedisKey[] keyList = ConvertRedisKeysAddSysCustomKey(keys);
var rValue = db.SortedSetCombineAndStore(operation, destination, keyList);
return rValue; } /// <summary>
/// 将string类型的Key转换成 <see cref="RedisKey"/> 型的Key,并添加前缀字符串
/// </summary>
/// <param name="redisKeys"></param>
/// <returns></returns>
private static RedisKey[] ConvertRedisKeysAddSysCustomKey(params string[] redisKeys) => redisKeys.Select(redisKey => (RedisKey)redisKey).ToArray(); /// <summary>
/// 获取指定Key中最大Score值,
/// </summary>
/// <param name="key">key名称,注意要先添加上Key前缀</param>
/// <returns></returns>
private static double _GetScore(string key, IDatabase db)
{
double dValue = ;
var rValue = db.SortedSetRangeByRankWithScores(key, , , Order.Descending).FirstOrDefault();
dValue = rValue != null ? rValue.Score : ;
return dValue + ;
}
#endregion #endregion #region SET /// <summary>
/// Add the specified member to the set stored at key. Specified members that are already a member of this set are ignored. If key does not exist, a new set is created before adding the specified members.
/// </summary>
/// <param name="dbIndex"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public static bool SetAdd<T>(int dbIndex, string key, T value)
{
var db = Instance.GetDatabase(dbIndex);
return db.SetAdd(key, ConvertJson(value));
} /// <summary>
/// Add the specified member to the set stored at key. Specified members that are already a member of this set are ignored. If key does not exist, a new set is created before adding the specified members.
/// </summary>
/// <param name="dbIndex"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns></returns>
public static async Task<bool> SetAddAsync<T>(int dbIndex, string key, T value)
{
var db = Instance.GetDatabase(dbIndex);
return await db.SetAddAsync(key, ConvertJson(value));
} /// <summary>
/// Returns the members of the set resulting from the specified operation against the given sets.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dbIndex"></param>
/// <param name="operation"></param>
/// <param name="firstKey"></param>
/// <param name="secondKey"></param>
/// <returns></returns>
public static string[] SetCombine(int dbIndex, SetOperation operation, string firstKey, string secondKey)
{
var db = Instance.GetDatabase(dbIndex);
var array = db.SetCombine(operation, firstKey, secondKey);
return array.ToStringArray();
} /// <summary>
/// Returns the members of the set resulting from the specified operation against the given sets.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dbIndex"></param>
/// <param name="operation"></param>
/// <param name="firstKey"></param>
/// <param name="secondKey"></param>
/// <returns></returns>
public static async Task<string[]> SetCombineAsync(int dbIndex, SetOperation operation, string firstKey, string secondKey)
{
var db = Instance.GetDatabase(dbIndex);
var array = await db.SetCombineAsync(operation, firstKey, secondKey);
return array.ToStringArray();
} /// <summary>
/// Returns if member is a member of the set stored at key.
/// </summary>
/// <param name="dbIndex"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns>Returns if member is a member of the set stored at key.</returns>
public static bool SetContains<T>(int dbIndex, string key, T value)
{
var db = Instance.GetDatabase(dbIndex);
return db.SetContains(key, ConvertJson(value));
} /// <summary>
/// Returns if member is a member of the set stored at key.
/// </summary>
/// <param name="dbIndex"></param>
/// <param name="key"></param>
/// <param name="value"></param>
/// <returns>Returns if member is a member of the set stored at key.</returns>
public static async Task<bool> SetContainsAsync<T>(int dbIndex, string key, T value)
{
var db = Instance.GetDatabase(dbIndex);
return await db.SetContainsAsync(key, ConvertJson(value));
} /// <summary>
/// 返回对应键值集合的长度|Returns the set cardinality (number of elements) of the set stored at key.
/// </summary>
/// <param name="dbIndex"></param>
/// <param name="key"></param>
/// <returns></returns>
public static long SetLength(int dbIndex, string key)
{
var db = Instance.GetDatabase(dbIndex);
return db.SetLength(key);
} /// <summary>
/// 返回对应键值集合的长度|Returns the set cardinality (number of elements) of the set stored at key.
/// </summary>
/// <param name="dbIndex"></param>
/// <param name="key"></param>
/// <returns></returns>
public static async Task<long> SetLengthAsync(int dbIndex, string key)
{
var db = Instance.GetDatabase(dbIndex);
return await db.SetLengthAsync(key);
} /// <summary>
/// 返回存储在键的集合值的所有成员|Returns all the members of the set value stored at key.
/// </summary>
/// <param name="dbIndex"></param>
/// <param name="key"></param>
/// <returns></returns>
public static List<T> SetMembers<T>(int dbIndex, string key)
{
var db = Instance.GetDatabase(dbIndex);
var array = db.SetMembers(key);
return ConvetList<T>(array);
} /// <summary>
/// 返回存储在键的集合值的所有成员|Returns all the members of the set value stored at key.
/// </summary>
/// <param name="dbIndex"></param>
/// <param name="key"></param>
/// <returns></returns>
public static async Task<List<T>> SetMembersAsync<T>(int dbIndex, string key)
{
var db = Instance.GetDatabase(dbIndex);
var array = await db.SetMembersAsync(key);
return ConvetList<T>(array);
} /// <summary>
/// Move member from the set at source to the set at destination. This operation is atomic. In every given moment the element will appear to be a member of source or destination for other clients. When the specified element already exists in the destination set, it is only removed from the source set.
/// </summary>
/// <param name="dbIndex"></param>
/// <param name="sourceKey"></param>
/// <param name="destinationKey"></param>
/// <param name="value"></param>
/// <returns></returns>
public static bool SetMove<T>(int dbIndex, string sourceKey, string destinationKey, T value)
{
var db = Instance.GetDatabase(dbIndex);
return db.SetMove(sourceKey, destinationKey, ConvertJson(value));
} /// <summary>
/// Move member from the set at source to the set at destination. This operation is atomic. In every given moment the element will appear to be a member of source or destination for other clients. When the specified element already exists in the destination set, it is only removed from the source set.
/// </summary>
/// <param name="dbIndex"></param>
/// <param name="sourceKey"></param>
/// <param name="destinationKey"></param>
/// <param name="value"></param>
/// <returns></returns>
public static async Task<bool> SetMoveAsync<T>(int dbIndex, string sourceKey, string destinationKey, T value)
{
var db = Instance.GetDatabase(dbIndex);
return await db.SetMoveAsync(sourceKey, destinationKey, ConvertJson(value));
} /// <summary>
/// Removes and returns a random element from the set value stored at key.
/// </summary>
/// <param name="dbIndex"></param>
/// <param name="key"></param>
/// <returns></returns>
public static T SetPop<T>(int dbIndex, string key)
{
var db = Instance.GetDatabase(dbIndex);
var value = db.SetPop(key);
return ConvertObj<T>(value);
} /// <summary>
/// Removes and returns a random element from the set value stored at key.
/// </summary>
/// <param name="dbIndex"></param>
/// <param name="key"></param>
/// <returns></returns>
public static async Task<T> SetPopAsync<T>(int dbIndex, string key)
{
var db = Instance.GetDatabase(dbIndex);
var value = await db.SetPopAsync(key);
return ConvertObj<T>(value);
} /// <summary>
/// Return a random element from the set value stored at key.
/// </summary>
/// <param name="dbIndex"></param>
/// <param name="key"></param>
/// <returns></returns>
public static T SetRandomMember<T>(int dbIndex, string key)
{
var db = Instance.GetDatabase(dbIndex);
var value = db.SetRandomMember(key);
return ConvertObj<T>(value);
} /// <summary>
/// Return a random element from the set value stored at key.
/// </summary>
/// <param name="dbIndex"></param>
/// <param name="key"></param>
/// <returns></returns>
public static async Task<T> SetRandomMemberAsync<T>(int dbIndex, string key)
{
var db = Instance.GetDatabase(dbIndex);
var value = await db.SetRandomMemberAsync(key);
return ConvertObj<T>(value);
} /// <summary>
/// Return an array of count distinct elements if count is positive. If called with a negative count the behavior changes and the command is allowed to return the same element multiple times. In this case the numer of returned elements is the absolute value of the specified count.
/// </summary>
/// <param name="dbIndex"></param>
/// <param name="key"></param>
/// <param name="count"></param>
/// <returns></returns>
public static string[] SetRandomMember(int dbIndex, string key, long count)
{
var db = Instance.GetDatabase(dbIndex);
return db.SetRandomMembers(key, count).ToStringArray();
} /// <summary>
/// Return an array of count distinct elements if count is positive. If called with a negative count the behavior changes and the command is allowed to return the same element multiple times. In this case the numer of returned elements is the absolute value of the specified count.
/// </summary>
/// <param name="dbIndex"></param>
/// <param name="key"></param>
/// <param name="count"></param>
/// <returns></returns>
public static async Task<string[]> SetRandomMembersAsync(int dbIndex, string key, long count)
{
var db = Instance.GetDatabase(dbIndex);
var array = await db.SetRandomMembersAsync(key, count);
return array.ToStringArray();
} /// <summary>
/// Remove the specified member from the set stored at key. Specified members that are not a member of this set are ignored.
/// </summary>
/// <param name="dbIndex"></param>
/// <param name="key"></param>
/// <param name="value"></param>
public static bool SetRemove(int dbIndex, string key, string value)
{
var db = Instance.GetDatabase(dbIndex);
return db.SetRemove(key, value);
} /// <summary>
/// Remove the specified member from the set stored at key. Specified members that are not a member of this set are ignored.
/// </summary>
/// <param name="dbIndex"></param>
/// <param name="key"></param>
/// <param name="value"></param>
public static async Task<bool> SetRemoveAsync(int dbIndex, string key, string value)
{
var db = Instance.GetDatabase(dbIndex);
return await db.SetRemoveAsync(key, value);
} /// <summary>
/// The SSCAN command is used to incrementally iterate over set; note: to resume an iteration via cursor, cast the original enumerable or enumerator to IScanningCursor.
/// </summary>
/// <param name="dbIndex"></param>
/// <param name="key"></param>
/// <returns></returns>
public static IEnumerable<RedisValue> SetScan(int dbIndex, string key)
{
return Instance.GetDatabase(dbIndex).SetScan(key);
}
#endregion #region 当作消息代理中间件使用 一般使用更专业的消息队列来处理这种业务场景
/// <summary>
/// 当作消息代理中间件使用
/// 消息组建中,重要的概念便是生产者,消费者,消息中间件。
/// </summary>
/// <param name="channel"></param>
/// <param name="message"></param>
/// <returns></returns>
public static long Publish(string channel, string message)
{
ISubscriber sub = Instance.GetSubscriber();
//return sub.Publish("messages", "hello");
return sub.Publish(channel, message);
} /// <summary>
/// 在消费者端得到该消息并输出
/// </summary>
/// <param name="channelFrom"></param>
/// <returns></returns>
public static void Subscribe(string channelFrom)
{
ISubscriber sub = Instance.GetSubscriber();
sub.Subscribe(channelFrom, (channel, message) =>
{
Console.WriteLine((string)message);
});
}
#endregion #region EventHandler
/// <summary>
/// 连接失败 , 如果重新连接成功你将不会收到这个通知
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void MuxerConnectionFailed(object sender, ConnectionFailedEventArgs e)
{ } /// <summary>
/// 重新建立连接之前的错误
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void MuxerConnectionRestored(object sender, ConnectionFailedEventArgs e)
{ } /// <summary>
/// 发生错误时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void MuxerErrorMessage(object sender, RedisErrorEventArgs e)
{
} /// <summary>
/// 更改集群
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private static void MuxerHashSlotMoved(object sender, HashSlotMovedEventArgs e)
{
// LogHelper.WriteInfoLog("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)
{
}
#endregion #region 内部辅助方法 /// <summary>
/// 将对象转换成string字符串
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <returns></returns>
public static string ConvertJson<T>(T value)
{
string result = value is string ? value.ToString() :
JsonConvert.SerializeObject(value, Formatting.None);
return result;
} /// <summary>
/// 将值集合转换成RedisValue集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="redisValues"></param>
/// <returns></returns>
private static RedisValue[] ConvertRedisValue<T>(params T[] redisValues) => redisValues.Select(o => (RedisValue)ConvertJson<T>(o)).ToArray(); /// <summary>
/// 将值反系列化成对象集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="values"></param>
/// <returns></returns>
public static List<T> ConvetList<T>(RedisValue[] values)
{
List<T> result = new List<T>();
foreach (var item in values)
{
var model = ConvertObj<T>(item);
result.Add(model);
}
return result;
} /// <summary>
/// 将值反系列化成对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <returns></returns>
public static T ConvertObj<T>(RedisValue value)
{
return value.IsNullOrEmpty ? default(T) : JsonConvert.DeserializeObject<T>(value);
} #endregion
}
}

在以上RedisUtils帮助类的基础上封装一次调用:

 /// <summary>
/// Redis帮助类
/// </summary>
public class RedisHelper
{
/// <summary>
/// 缓存失效时长
/// </summary>
public const int EXPIRY = ; private static int CheckDbIndex(int dbIndex)
{
if (dbIndex > || dbIndex < )
{
dbIndex = ;
}
return dbIndex;
} /// <summary>
/// 获取缓存数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dbIndex">Redis数据库索引</param>
/// <param name="key">Redis键</param>
/// <param name="fun">从其他地方获取数据源,并缓存到Redis中</param>
/// <param name="timeout">过期时间,单位:分钟</param>
/// <returns></returns>
public static T GetObject<T>(int dbIndex, string key, Func<T> fun, int? timeout = EXPIRY) where T : class
{
dbIndex = CheckDbIndex(dbIndex);
T data = RedisUtils.StringGet<T>(dbIndex, key);
if (data != null)
{
return data;
}
if (fun != null)
{
data = fun();
}
if (data != null)
{
TimeSpan? timeSp = null;
if (timeout != null)
timeSp = TimeSpan.FromMinutes(Convert.ToDouble(timeout));
RedisUtils.StringSet<T>(dbIndex, key, data, timeSp);
}
return data;
} /// <summary>
/// KV
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dbIndex">库</param>
/// <param name="key">键</param>
/// <param name="func">如找不到则从func获取</param>
/// <param name="timeout">超时时间</param>
/// <returns></returns>
public static T GetObject_KV<T>(int dbIndex, string key, Func<T> func, TimeSpan? timeout) where T : class
{
T data = RedisUtils.StringGet<T>(dbIndex, key);
if (data != null)
{
return data;
}
if (func != null)
{
data = func();
}
if (data != null)
{
RedisUtils.StringSet<T>(dbIndex, key, data, timeout);
}
return data;
} /// <summary>
/// 异步获取缓存数据
/// </summary>
/// <typeparam name="T">数据集类型</typeparam>
/// <param name="dbIndex">数据库</param>
/// <param name="key">键</param>
/// <param name="fun">从其他地方获取数据源,并缓存到Redis中</param>
/// <param name="timeout">过期时间,单位:分钟</param>
/// <returns></returns>
public static async Task<T> GetObjectAsync<T>(int dbIndex, string key, Func<T> fun, int timeout = EXPIRY) where T : class
{
dbIndex = CheckDbIndex(dbIndex);
T data = RedisUtils.StringGet<T>(dbIndex, key);
if (data != null)
{
return data;
} if (fun != null)
{
data = await Task.Run(() =>
{
return fun();
});
}
if (data != null)
{
RedisUtils.StringSet<T>(dbIndex, key, data, TimeSpan.FromMinutes(timeout));
}
return data;
} /// <summary>
/// 异步获取缓存数据
/// </summary>
/// <typeparam name="T">数据集类型</typeparam>
/// <param name="dbIndex">数据库</param>
/// <param name="key">键</param>
/// <param name="fun">从其他地方获取数据源,并缓存到Redis中</param>
/// <param name="timeout">过期时间,单位:分钟</param>
/// <returns></returns>
public static async Task<T> GetObjectAsync<T>(int dbIndex, string key, Func<RedisCache<T>> fun, int timeout = EXPIRY) where T : class
{
dbIndex = CheckDbIndex(dbIndex);
RedisCache<T> cache = new RedisCache<T>();
cache.CacheData = RedisUtils.StringGet<T>(dbIndex, key);
if (cache.CacheData != null)
{
return cache.CacheData;
} var temp = await Task.Run(() =>
{
return fun();
}); if (temp != null) cache = temp; if (cache.UseCache)
{
RedisUtils.StringSet<T>(dbIndex, key, cache.CacheData, TimeSpan.FromMinutes(timeout));
}
return cache.CacheData;
} /// <summary>
/// 异步获取数据集合
/// </summary>
/// <typeparam name="T">数据集类型</typeparam>
/// <param name="dbIndex">数据库</param>
/// <param name="key">键</param>
/// <param name="fun">从其他地方获取数据源,并缓存到Redis中</param>
/// <param name="timeout">过期时间,单位:分钟</param>
/// <returns></returns>
public static async Task<List<T>> GetListAsync<T>(int dbIndex, string key, Func<List<T>> fun, int timeout = EXPIRY) where T : class
{
dbIndex = CheckDbIndex(dbIndex);
List<T> datas = RedisUtils.StringGet<List<T>>(dbIndex, key);
if (datas != null && datas.Count > )
{
return datas;
} datas = await Task.Run(() =>
{
return fun();
}); if (datas != null && datas.Count > )
{
RedisUtils.StringSet<List<T>>(dbIndex, key, datas, TimeSpan.FromMinutes(timeout));
}
return datas;
} /// <summary>
/// ZSet
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dbIndex">库</param>
/// <param name="key">键</param>
/// <param name="func">如找不到则从func获取</param>
/// <returns></returns>
public static List<T> GetObject_ZSet<T>(int dbIndex, string key, Func<List<T>> func) where T : class
{
List<T> data = RedisUtils.SortedSetRangeByRank<T>(dbIndex, key);
if (data != null && data.Count > )
{
return data;
}
if (func != null)
{
data = func();
}
if (data != null)
{
RedisUtils.SortedSetAdd<T>(dbIndex, key, data);
}
return data;
} /// <summary>
/// Hash
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dbIndex">库</param>
/// <param name="hashID">hashID</param>
/// <param name="key">键</param>
/// <param name="func">如找不到则从func获取</param>
/// <returns></returns>
public static T GetObject_Hash<T>(int dbIndex, string hashID, string key, Func<T> func) where T : class
{
T data = RedisUtils.HashGet<T>(dbIndex, hashID, key);
if (data != null)
{
return data;
}
if (func != null)
{
data = func();
}
if (data != null)
{
RedisUtils.HashSet<T>(dbIndex, hashID, key, data);
}
return data;
}
}

修复

经过反复阅读源码和测试,还有得到源作者NickCraver的指导,ConnectionMultiplexer是线程安全的,在多线程的情况下,避免使用lock来作为单例来使用,出现的连接超时的情况。

It was not possible to connect to the redis server(s).ConnectTimeout

正确姿势在帮助类折叠代码里面。

测试源码:

static void TestRedis()
{
int dbIdx = ;
string key = "testKey";
Task.Run(()=> {
while (true)
{
Task.Run(() =>
{
RedisUtils.StringSet(dbIdx, key, DateTime.Now.ToString(), TimeSpan.FromSeconds());
});
Task.Run(() =>
{
string v = RedisUtils.StringGet(dbIdx, key);
Console.WriteLine("1:" + v);
});
System.Threading.Thread.Sleep();
}
});
Task.Run(() => {
while (true)
{
Task.Run(() =>
{
RedisUtils.StringSet(dbIdx, key, DateTime.Now.ToString(), TimeSpan.FromSeconds());
});
Task.Run(() =>
{
string v = RedisUtils.StringGet(dbIdx, key);
Console.WriteLine("2:" + v);
});
System.Threading.Thread.Sleep();
}
});
Task.Run(() => {
while (true)
{
Task.Run(() =>
{
RedisUtils.StringSet(dbIdx, key, DateTime.Now.ToString(), TimeSpan.FromSeconds());
});
Task.Run(() =>
{
string v = RedisUtils.StringGet(dbIdx, key);
Console.WriteLine("3:" + v);
});
System.Threading.Thread.Sleep();
}
}); }

已封装在这里

StackExchange.Redis 二次封装的更多相关文章

  1. 深入理解MVC C#+HtmlAgilityPack+Dapper走一波爬虫 StackExchange.Redis 二次封装 C# WPF 用MediaElement控件实现视频循环播放 net 异步与同步

    深入理解MVC   MVC无人不知,可很多程序员对MVC的概念的理解似乎有误,换言之他们一直在错用MVC,尽管即使如此软件也能被写出来,然而软件内部代码的组织方式却是不科学的,这会影响到软件的可维护性 ...

  2. StackExchange.Redis使用和封装小试

    StackExchange.Redis使用和封装小试 https://git.oschina.net/Ultralisk/ThorActor/blob/ThorActor/DBUtility/Redi ...

  3. Lind.DDD.RedisClient~对StackExchange.Redis调用者的封装及多路复用技术

    回到目录 两雄争霸 使用StackExchange.Redis的原因是因为它开源,免费,而对于商业化的ServiceStack.Redis,它将一步步被前者取代,开源将是一种趋势,商业化也值得被我们尊 ...

  4. StackExchange.Redis使用以及封装

    来源:http://www.cnblogs.com/qtqq/p/5951201.html Redis安装:http://www.runoob.com/redis/redis-install.html ...

  5. StackExchange.Redis.Extensions.Core 源码解读之 Configuration用法

    前言 之前接触到Redis,然后选用了对StackExchange.Redis又一层封装的StackExchange.Redis.Extensions.Core类库.阅读源代码的过程中发现了他使用Co ...

  6. StackExchange.Redis的使用

    StackExchange.Redis介绍 有需要了解的和基础的使用可以参考:http://www.cnblogs.com/bnbqian/p/4962855.html StackExchange.R ...

  7. StackExchange.Redis 之 hash 类型示例

    StackExchange.Redis 的组件封装示例网上有很多,自行百度搜索即可. 这里只演示如何使用Hash类型操作数据: // 在 hash 中存入或修改一个值 并设置order_hashkey ...

  8. StackExchange.Redis帮助类解决方案RedisRepository封装(基础配置)

    本文版权归博客园和作者吴双本人共同所有,转载和爬虫,请注明原文地址.http://www.cnblogs.com/tdws/p/5815735.html 写在前面 这不是教程,分享而已,也欢迎园友们多 ...

  9. StackExchange.Redis帮助类解决方案RedisRepository封装(字符串类型数据操作)

    本文版权归博客园和作者本人共同所有,转载和爬虫请注明原文链接 http://www.cnblogs.com/tdws/tag/NoSql/ 目录 一.基础配置封装 二.String字符串类型数据操作封 ...

随机推荐

  1. Linux命令pmap

    jmap可以查看Java程序的堆内存使用情况,pmap可以查看Linux上运行的进程的内存使用情况. 查看Linux上运行的进程的内存使用情况,可以使用jmap,top,ps命令. top命令本身也比 ...

  2. 51NOD 1376 最长递增子序列的数量 [CDQ分治]

    1376 最长递增子序列的数量 首先可以用线段树优化$DP$做,转移时取$0...a[i]$的最大$f$值 但我要练习$CDQ$ $LIS$是二维偏序问题,偏序关系是$i<j,\ a_i< ...

  3. 从细菌GFF文件提取CDS序列并转换为氨基酸序列

    最近在上生物信息学原理,打算记录一些课上的作业.第一次作业:如题. 基本思路: 1.从GFF中读取CDS的起始终止位置以及正负链信息.GFF格式见http://blog.sina.com.cn/s/b ...

  4. PHP中cookies跨目录无法调用解决办法

    localhost/a/test.php 中写入: <?php setcookie("user","zhangsan",time()+3600); ?&g ...

  5. 广告等第三方应用嵌入到web页面方案 之 使用js片段

    在自己的项目中嵌入过广告的朋友们可能都用过百度联盟, 只需要嵌入如下一段js代码片段, 就可以在自己的项目中嵌入广告, 来获得收益. <script type="text javasc ...

  6. CentOS 7.3 minimal 开启网络服务

    CentOS7解决不能上网问题 1.先进入控制台 输入ip addr 2.然后su 获取超级管理员权限 3.编辑网络配置文件 vi  /etc/sysonfig/network-scripts/ifc ...

  7. Yaf框架的配置

    http://www.laruence.com/manual/yaf.ini.html //先看一下惠新宸鸟哥yaf官网的配置说明 我们可以在php.ini中定义开发环节配置项,把本地开发设置成dev ...

  8. PHP开发中多种方案实现高并发下的抢购、秒杀功能

    抢购.秒杀是如今很常见的一个应用场景,主要需要解决的问题有两个: 1 高并发对数据库产生的压力 2 竞争状态下如何解决库存的正确减少("超卖"问题) 对于第一个问题,已经很容易想到 ...

  9. 简单模拟一下ab压力测试

    简单了解下ab ab全程是apache benchmark,是apache官方推出的一个工具,创建多个并发访问线程,模拟多个访问者同时对一个URL地址进行访问.它的测试目标是基于URL的,因此它既可以 ...

  10. github的拉取、提交,创建分支与合并

    前期准备: 1.安装git  官网地址:https://git-scm.com/(下载下来,直接下一步)   2.github账号(这有点废话)   3.配置github密钥 下载及安装好git后,右 ...