博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
功能比较全的StackExchange.Redis封装帮助类(.Net/C#)
阅读量:5306 次
发布时间:2019-06-14

本文共 13803 字,大约阅读时间需要 46 分钟。

Redis官网

以下内容未全部验证,如有问题请指出

//static NewtonsoftSerializer serializer = new NewtonsoftSerializer();        //static StackExchangeRedisCacheClient cacheClient = new StackExchangeRedisCacheClient(serializer);        //private static IDatabase db = cacheClient.Database;        private static string connstr = System.Configuration.ConfigurationManager.AppSettings["redisCacheClient"];// "127.0.0.1:6379,allowadmin=true";        private static ConnectionMultiplexer conn = ConnectionMultiplexer.Connect(connstr);        private static IDatabase db =  conn.GetDatabase(1);        ///         /// 获取系统的redis key前缀        ///         /// 资源Id        /// 
public static string GetMyKey(string resourceid = "") { string Key = "report_"; if (!string.IsNullOrWhiteSpace(resourceid)) { Key = string.Format("report_res_{0}", resourceid); } return Key; } #region String 可以设置过期时间 同步 /// /// 保存单个key value /// /// Redis Key /// 保存的值 /// 过期时间 ///
public static bool SetStringKey(string key, string value, TimeSpan? expiry = default(TimeSpan?)) { return db.StringSet(key, value, expiry); } /// /// 保存多个key value /// /// key ///
public static bool SetStringKey(KeyValuePair
[] arr) { return db.StringSet(arr); } ///
/// 保存一个对象 /// ///
///
///
///
public static bool SetStringKey
(string key, T obj, TimeSpan? expiry = default(TimeSpan?)) { string json = JsonConvert.SerializeObject(obj); return db.StringSet(key, json, expiry); } ///
/// 获取单个key的值 /// ///
Redis Key ///
public static RedisValue GetStringKey(string key) { return db.StringGet(key); } ///
/// 获取多个Key /// ///
Redis Key集合 ///
public static RedisValue[] GetStringKey(List
listKey) { return db.StringGet(listKey.ToArray()); } ///
/// 获取一个key的对象 /// ///
///
///
public static T GetStringKey
(string key) { return JsonConvert.DeserializeObject
(db.StringGet(key)); } ///
/// 为数字增长val /// ///
///
可以为负 ///
增长后的值
public double StringIncrement(string key, double val = 1) { return db.StringIncrement(key, val); } ///
/// 为数字减少val /// ///
///
可以为负 ///
减少后的值
public double StringDecrement(string key, double val = 1) { return db.StringDecrement(key, val); } #endregion #region String 可以设置过期时间 异步 ///
/// 保存单个key value /// ///
Redis Key ///
保存的值 ///
过期时间 ///
public static async Task
SetStringKeyAsync(string key, string value, TimeSpan? expiry = default(TimeSpan?)) { return await db.StringSetAsync(key, value, expiry); } ///
/// 保存多个key value /// ///
key ///
public static async Task
SetStringKeyAsync(KeyValuePair
[] arr) { return await db.StringSetAsync(arr); } ///
/// 保存一个对象 /// ///
///
///
///
public static async Task
SetStringKeyAsync
(string key, T obj, TimeSpan? expiry = default(TimeSpan?)) { string json = JsonConvert.SerializeObject(obj); return await db.StringSetAsync(key, json, expiry); } ///
/// 获取单个key的值 /// ///
Redis Key ///
public static async Task
GetStringKeyAsync(string key) { return await db.StringGetAsync(key); } ///
/// 获取多个Key /// ///
Redis Key集合 ///
public static async Task
GetStringKeyAsync(List
listKey) { return await db.StringGetAsync(listKey.ToArray()); } ///
/// 获取一个key的对象 /// ///
///
///
public static async Task
GetStringKeyAsync
(string key) { return JsonConvert.DeserializeObject
(await db.StringGetAsync(key)); } ///
/// 为数字增长val /// ///
///
可以为负 ///
增长后的值
public async Task
StringIncrementAsync(string key, double val = 1) { return await db.StringIncrementAsync(key, val); } ///
/// 为数字减少val /// ///
///
可以为负 ///
减少后的值
public async Task
StringDecrementAsync(string key, double val = 1) { return await db.StringDecrementAsync(key, val); } #endregion #region Hash 同步 ///
/// 存储数据到hash表 /// ///
///
///
///
///
public static bool HashSet1
(string key, string dataKey, List
t) { string json = ""; foreach (var item in t) { json = JsonConvert.SerializeObject(item); //listHashEntry.Add(new HashEntry(getModelId(item), json)); } return db.HashSet(key, dataKey, json); //return await ({ // string json = ConvertJson(t); // return db.HashSetAsync(key, dataKey, json); //}); } ///
/// 保存一个集合 /// ///
///
Redis Key ///
数据集合 ///
public static void HashSet
(string key, List
list, Func
getModelId) { List
listHashEntry = new List
(); foreach (var item in list) { string json = JsonConvert.SerializeObject(item); listHashEntry.Add(new HashEntry(getModelId(item), json)); } db.HashSet(key, listHashEntry.ToArray()); } ///
/// 获取Hash中的单个key的值 /// ///
///
Redis Key ///
RedisValue ///
public static T GetHashKey
(string key, string hasFildValue) { if (!string.IsNullOrWhiteSpace(key) && !string.IsNullOrWhiteSpace(hasFildValue)) { RedisValue value = db.HashGet(key, hasFildValue); if (!value.IsNullOrEmpty) { return JsonConvert.DeserializeObject
(value); } } return default(T); } ///
/// 获取hash中的多个key的值 /// ///
///
Redis Key ///
RedisValue value ///
public static List
GetHashKey
(string key, List
listhashFields) { List
result = new List
(); if (!string.IsNullOrWhiteSpace(key) && listhashFields.Count > 0) { RedisValue[] value = db.HashGet(key, listhashFields.ToArray()); foreach (var item in value) { if (!item.IsNullOrEmpty) { result.Add(JsonConvert.DeserializeObject
(item)); } } } return result; } ///
/// 获取hashkey所有Redis key /// ///
///
///
public static List
GetHashAll
(string key) { List
result = new List
(); RedisValue[] arr = db.HashKeys(key); foreach (var item in arr) { if (!item.IsNullOrEmpty) { result.Add(JsonConvert.DeserializeObject
(item)); } } return result; } ///
/// 获取hashkey所有的值 /// ///
///
///
public static List
HashGetAll
(string key) { List
result = new List
(); HashEntry[] arr = db.HashGetAll(key); foreach (var item in arr) { if (!item.Value.IsNullOrEmpty) { result.Add(JsonConvert.DeserializeObject
(item.Value)); } } return result; } ///
/// 删除hasekey /// ///
///
///
public static bool DeleteHase(RedisKey key, RedisValue hashField) { return db.HashDelete(key, hashField); } #endregion #region Hash 异步 ///
/// 存储数据到hash表 /// ///
///
///
///
///
public static async Task
HashSetAsync
(string key, string dataKey, List
t) { string json = ""; foreach (var item in t) { json = JsonConvert.SerializeObject(item); } return await db.HashSetAsync(key, dataKey, json); } ///
/// 获取Hash中的单个key的值 /// ///
///
Redis Key ///
RedisValue ///
public static async Task
GetHashKeyAsync
(string key, string hasFildValue) { if (!string.IsNullOrWhiteSpace(key) && !string.IsNullOrWhiteSpace(hasFildValue)) { RedisValue value = await db.HashGetAsync(key, hasFildValue); if (!value.IsNullOrEmpty) { return JsonConvert.DeserializeObject
(value); } } return default(T); } ///
/// 获取hash中的多个key的值 /// ///
///
Redis Key ///
RedisValue value ///
public static async Task
> GetHashKeyAsync
(string key, List
listhashFields) { List
result = new List
(); if (!string.IsNullOrWhiteSpace(key) && listhashFields.Count > 0) { RedisValue[] value = await db.HashGetAsync(key, listhashFields.ToArray()); foreach (var item in value) { if (!item.IsNullOrEmpty) { result.Add(JsonConvert.DeserializeObject
(item)); } } } return result; } ///
/// 获取hashkey所有Redis key /// ///
///
///
public static async Task
> GetHashAllAsync
(string key) { List
result = new List
(); RedisValue[] arr =await db.HashKeysAsync(key); foreach (var item in arr) { if (!item.IsNullOrEmpty) { result.Add(JsonConvert.DeserializeObject
(item)); } } return result; } ///
/// 获取hashkey所有的值 /// ///
///
///
public static async Task
> HashGetAllAsync
(string key) { List
result = new List
(); HashEntry[] arr =await db.HashGetAllAsync(key); foreach (var item in arr) { if (!item.Value.IsNullOrEmpty) { result.Add(JsonConvert.DeserializeObject
(item.Value)); } } return result; } ///
/// 删除hasekey /// ///
///
///
public static async Task
DeleteHaseAsync(RedisKey key, RedisValue hashField) { return await db.HashDeleteAsync(key, hashField); } #endregion #region key 同步 ///
/// 删除单个key /// ///
redis key ///
是否删除成功
public static bool KeyDelete(string key) { return db.KeyDelete(key); } ///
/// 删除多个key /// ///
rediskey ///
成功删除的个数
public static long keyDelete(RedisKey[] keys) { return db.KeyDelete(keys); } ///
/// 判断key是否存储 /// ///
redis key ///
public static bool KeyExists(string key) { return db.KeyExists(key); } ///
/// 重新命名key /// ///
就的redis key ///
新的redis key ///
public static bool KeyRename(string key, string newKey) { return db.KeyRename(key, newKey); } ///
/// 设置Key的时间 /// ///
redis key ///
///
public static bool KeyExpire(string key, TimeSpan? expiry = default(TimeSpan?)) { return db.KeyExpire(key, expiry); } #endregion #region key 异步 ///
/// 删除单个key /// ///
redis key ///
是否删除成功
public static async Task
KeyDeleteAsync(string key) { return await db.KeyDeleteAsync(key); } ///
/// 删除多个key /// ///
rediskey ///
成功删除的个数
public static async Task
keyDeleteAsync(RedisKey[] keys) { return await db.KeyDeleteAsync(keys); } ///
/// 判断key是否存储 /// ///
redis key ///
public static async Task
KeyExistsAsync(string key) { return await db.KeyExistsAsync(key); } ///
/// 重新命名key /// ///
就的redis key ///
新的redis key ///
public static async Task
KeyRenameAsync(string key, string newKey) { return await db.KeyRenameAsync(key, newKey); } ///
/// 设置Key的时间 /// ///
redis key ///
///
public static async Task
KeyExpireAsync(string key, TimeSpan? expiry = default(TimeSpan?)) { return await db.KeyExpireAsync(key, expiry); } #endregion #region List 同步 ///
/// 移除指定ListId的内部List的值 /// ///
///
public void ListRemove
(string key, T value) { db.ListRemove(key, JsonConvert.SerializeObject(value)); } ///
/// 获取指定key的List /// ///
///
public List
ListRange
(string key) { var values = db.ListRange(key); List
result = new List
(); foreach (var item in values) { var model = JsonConvert.DeserializeObject
(item); result.Add(model); } return result; } ///
/// 入队 /// ///
///
public void ListRightPush
(string key, T value) { db.ListRightPush(key, JsonConvert.SerializeObject(value)); } ///
/// 出队 /// ///
///
///
public T ListRightPop
(string key) { var value = db.ListRightPop(key); return JsonConvert.DeserializeObject
(value); } ///
/// 获取集合中的数量 /// ///
///
public long ListLength(string key) { return db.ListLength(key); } #endregion 同步方法 #region List 异步 ///
/// 移除指定ListId的内部List的值 /// ///
///
public async Task
ListRemoveAsync
(string key, T value) { return await db.ListRemoveAsync(key, JsonConvert.SerializeObject(value)); } ///
/// 获取指定key的List /// ///
///
public async Task
> ListRangeAsync
(string key) { var values =await db.ListRangeAsync(key); List
result = new List
(); foreach (var item in values) { var model = JsonConvert.DeserializeObject
(item); result.Add(model); } return result; } ///
/// 入队 /// ///
///
public async Task
ListRightPushAsync
(string key, T value) { return await db.ListRightPushAsync(key, JsonConvert.SerializeObject(value)); } ///
/// 出队 /// ///
///
///
public async Task
ListRightPopAsync
(string key) { var value = await db.ListRightPopAsync(key); return JsonConvert.DeserializeObject
(value); } ///
/// 获取集合中的数量 /// ///
///
public async Task
ListLengthAsync(string key) { return await db.ListLengthAsync(key); } #endregion 异步方法 #region SortedSet 同步 ///
/// 添加 /// ///
///
///
public static bool SortedSetAdd
(string key, T value, double score) { return db.SortedSetAdd(key, JsonConvert.SerializeObject(value), score); } ///
/// 删除 /// ///
///
public static bool SortedSetRemove
(string key, T value) { return db.SortedSetRemove(key, JsonConvert.SerializeObject(value)); } ///
/// 获取全部 /// ///
///
public static List
SortedSetRangeByRank
(string key) { var values = db.SortedSetRangeByRank(key); List
result = new List
(); foreach (var item in values) { var model = JsonConvert.DeserializeObject
(item); result.Add(model); } return result; } ///
/// 获取集合中的数量 /// ///
///
public static long SortedSetLength(string key) { return db.SortedSetLength(key); } #endregion 同步方法 #region SortedSet 异步 ///
/// 添加 /// ///
///
///
public static async Task
SortedSetAddAsync
(string key, T value, double score) { return await db.SortedSetAddAsync(key, JsonConvert.SerializeObject(value), score); } ///
/// 删除 /// ///
///
public static async Task
SortedSetRemoveAsync
(string key, T value) { return await db.SortedSetRemoveAsync(key, JsonConvert.SerializeObject(value)); } ///
/// 获取全部 /// ///
///
public static async Task
> SortedSetRangeByRankAsync
(string key) { var values = await db.SortedSetRangeByRankAsync(key); List
result = new List
(); foreach (var item in values) { var model = JsonConvert.DeserializeObject
(item); result.Add(model); } return result; } ///
/// 获取集合中的数量 /// ///
///
public static async Task
SortedSetLengthAsync(string key) { return await db.SortedSetLengthAsync(key); } #endregion 异步方法 ///
/// 追加值 /// ///
///
public static void StringAppend(string key, string value) { 追加值,返回追加后长度 long appendlong = db.StringAppend(key, value); }

 

转载于:https://www.cnblogs.com/cyh92/p/7007506.html

你可能感兴趣的文章
[SOF] Pointers, smart pointers or shared pointers?
查看>>
I/O-<File区别>
查看>>
cf249D
查看>>
java.util.concurrent.ExecutionException
查看>>
团队项目(一)
查看>>
centos7安装zabbix3.0超详细步骤解析
查看>>
后台数据传值得到数字需转为汉字
查看>>
库的简介和分类
查看>>
雪碧图
查看>>
洛谷P1803
查看>>
康拓展开-排列的hash
查看>>
.Net实现微信公众平台开发接口(二) 之 “获取access_token”
查看>>
php命名空间
查看>>
idea创建spring-boot项目
查看>>
poj2155Matrix【二位树状数组】
查看>>
beta冲刺(4/7)
查看>>
beta答辩总结
查看>>
Rsync 软件的工作方式
查看>>
自定义控件进阶02_侧滑删除,粘性控件
查看>>
装机篇:将ubuntu 14.04安装在移动硬盘中
查看>>