Redis官网 https://redis.io/
//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(ListlistKey) { 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 TaskSetStringKeyAsync(string key, string value, TimeSpan? expiry = default(TimeSpan?)) { return await db.StringSetAsync(key, value, expiry); } ////// 保存多个key value
//////key ///public static async TaskSetStringKeyAsync(KeyValuePair[] arr) { return await db.StringSetAsync(arr); } ////// 保存一个对象
///////////////public static async TaskSetStringKeyAsync(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 TaskGetStringKeyAsync(string key) { return await db.StringGetAsync(key); } ////// 获取多个Key
//////Redis Key集合 ///public static async TaskGetStringKeyAsync(ListlistKey) { return await db.StringGetAsync(listKey.ToArray()); } ////// 获取一个key的对象
////////////public static async TaskGetStringKeyAsync(string key) { return JsonConvert.DeserializeObject(await db.StringGetAsync(key)); } ////// 为数字增长val
/////////可以为负 ///增长后的值public async TaskStringIncrementAsync(string key, double val = 1) { return await db.StringIncrementAsync(key, val); } ////// 为数字减少val
/////////可以为负 ///减少后的值public async TaskStringDecrementAsync(string key, double val = 1) { return await db.StringDecrementAsync(key, val); } #endregion #region Hash 同步 ////// 存储数据到hash表
//////////////////public static bool HashSet1(string key, string dataKey, Listt) { 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, Listlist, FuncgetModelId) { ListlistHashEntry = 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 ListGetHashKey(string key, ListlisthashFields) { Listresult = 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 ListGetHashAll(string key) { Listresult = 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 ListHashGetAll(string key) { Listresult = 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 TaskHashSetAsync(string key, string dataKey, Listt) {
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 TaskGetHashKeyAsync(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, ListlisthashFields) { Listresult = 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) { Listresult = 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) { Listresult = 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 TaskDeleteHaseAsync(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 TaskKeyDeleteAsync(string key) { return await db.KeyDeleteAsync(key); } ////// 删除多个key
//////rediskey ///成功删除的个数public static async TaskkeyDeleteAsync(RedisKey[] keys) { return await db.KeyDeleteAsync(keys); } ////// 判断key是否存储
//////redis key ///public static async TaskKeyExistsAsync(string key) { return await db.KeyExistsAsync(key); } ////// 重新命名key
//////就的redis key ///新的redis key ///public static async TaskKeyRenameAsync(string key, string newKey) { return await db.KeyRenameAsync(key, newKey); } ////// 设置Key的时间
//////redis key //////public static async TaskKeyExpireAsync(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 ListListRange(string key) { var values = db.ListRange(key); Listresult = 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 TaskListRemoveAsync(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); Listresult = new List(); foreach (var item in values) { var model = JsonConvert.DeserializeObject(item); result.Add(model); } return result; } ////// 入队
/////////public async TaskListRightPushAsync(string key, T value) { return await db.ListRightPushAsync(key, JsonConvert.SerializeObject(value)); } ////// 出队
////////////public async TaskListRightPopAsync(string key) { var value = await db.ListRightPopAsync(key); return JsonConvert.DeserializeObject(value); } ////// 获取集合中的数量
/////////public async TaskListLengthAsync(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 ListSortedSetRangeByRank(string key) { var values = db.SortedSetRangeByRank(key); Listresult = 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 TaskSortedSetAddAsync(string key, T value, double score) { return await db.SortedSetAddAsync(key, JsonConvert.SerializeObject(value), score); } ////// 删除
/////////public static async TaskSortedSetRemoveAsync(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); Listresult = new List(); foreach (var item in values) { var model = JsonConvert.DeserializeObject(item); result.Add(model); } return result; } ////// 获取集合中的数量
/////////public static async TaskSortedSetLengthAsync(string key) { return await db.SortedSetLengthAsync(key); } #endregion 异步方法 ////// 追加值
/////////public static void StringAppend(string key, string value) { ////追加值,返回追加后长度 long appendlong = db.StringAppend(key, value); }