using Newtonsoft.Json; using StackExchange.Redis; using System; using System.Collections.Generic; using System.Linq; namespace SiteCore { public class RedisHelper { #region //执行顺序---静态字段---静态构造函数---构造函数 private static ConnectionMultiplexer redis; static RedisHelper() { if (redis == null || redis.IsConnected) { redis = ConnectionMultiplexer.Connect("127.0.0.1:6379, password=abc123abc,allowAdmin=true"); } } public void Clear() { var endpoints = redis.GetEndPoints(true); foreach (var endpoint in endpoints) { var server = redis.GetServer(endpoint); server.FlushAllDatabases(); } } #endregion public static void SetKeyValue(object key, object v) { if (redis.GetDatabase().StringGet(key.ToString()) == RedisValue.Null) { redis.GetDatabase().StringSet(key.ToString(), v.ToString(), TimeSpan.FromDays(1)); } else { redis.GetDatabase().StringIncrement(key.ToString(), 1); } } public static bool HasKey(string key) { return redis.GetDatabase().KeyExists(key); } #region redis 字符串(string)操作 /// /// 设置指定键的值 /// /// /// /// public static bool StringSet(string key, string value) { return redis.GetDatabase().StringSet(key, value); } /// /// 获取指定键的值 /// /// /// public static object StringGet(string key) { RedisValue rv = redis.GetDatabase().StringGet(key); if (rv == RedisValue.Null) return null; return rv; } /// /// 获取存储在键上的字符串的子字符串 /// /// /// /// /// public static object StringGet(string key, int start, int end) { return redis.GetDatabase().StringGetRange(key, start, end); } /// /// 设置键的字符串值并返回其旧值 /// /// /// /// public static object StringGetAndSet(string key, string value) { return redis.GetDatabase().StringGetSet(key, value); } /// /// 返回在键处存储的字符串值中偏移处的位值 /// /// /// /// public static bool StringGetBit(string key, long offset) { return redis.GetDatabase().StringGetBit(key, offset); } /// /// 获取所有给定键的值 /// /// /// public static List StringMultiGet(string[] keys) { List list = new List(); for (int i = 0; i < keys.Length; i++) { list.Add(redis.GetDatabase().StringGet(keys[i])); } return list; } /// /// 存储在键上的字符串值中设置或清除偏移处的位 /// /// /// /// /// public static bool StringSetBit(string key, long offset) { return redis.GetDatabase().StringSetBit(key, offset, true); } /// /// 使用键和到期时间来设置值 /// /// /// /// /// public static bool StringSet(string key, string value, TimeSpan expiry) { return redis.GetDatabase().StringSet(key, value, expiry); } /// /// 设置键的值,仅当键不存在时 /// /// /// /// public static void StringSetIfAbsent(string key, string value) { if (redis.GetDatabase().StringGet(key) == RedisValue.Null) { redis.GetDatabase().StringSet(key, value); } } /// /// 在指定偏移处开始的键处覆盖字符串的一部分 /// /// 键值 /// 值 /// 偏移量 /// public static object StringSet(string key, long offset, string value) { return redis.GetDatabase().StringSetRange(key, offset, value); } /// /// 获取存储在键中的值的长度 /// /// 键值 /// public static long StringSize(string key) { return redis.GetDatabase().StringLength(key); } /// /// 为多个键分别设置它们的值 /// /// /// public static void StringMultiSet(Dictionary dic) { foreach (KeyValuePair key in dic) { redis.GetDatabase().StringSet(key.Key, key.Value); } } /// /// 为多个键分别设置它们的值,仅当键不存在时 /// /// 键值集合 /// public static void StringMultiSetIfAbsent(Dictionary dic) { foreach (KeyValuePair key in dic) { //判断键值是否存在 if (redis.GetDatabase().StringGet(key.Key) == RedisValue.Null) { redis.GetDatabase().StringSet(key.Key, key.Value); } } } /// /// 将键的整数值按给定的数值增加 /// /// 键值 /// 给定的数值 /// public static double StringIncrement(string key, double value) { return redis.GetDatabase().StringIncrement(key, value); } /// /// 在key键对应值的右面追加值value /// /// /// /// public static long StringAppend(string key, string value) { return redis.GetDatabase().StringAppend(key, value); } /// /// 删除某个键值 /// /// /// public static bool StringDelete(string key) { return redis.GetDatabase().KeyDelete(key); //return false; } #endregion #region redis 哈希/散列/字典(Hash)操作 /// /// 删除指定的哈希字段 /// /// /// /// public static bool HashDelete(string key, string field) { return redis.GetDatabase().HashDelete(key, field); } /// /// 判断是否存在散列字段 /// /// /// /// public static bool HashHasKey(string key, string field) { return redis.GetDatabase().HashExists(key, field); } /// /// 获取存储在指定键的哈希字段的值 /// /// /// /// public static object HashGet(string key, string field) { return redis.GetDatabase().HashGet(key, field); } /// /// 获取存储在指定键的哈希中的所有字段和值 /// /// /// public static Dictionary HashGetAll(string key) { Dictionary dic = new Dictionary(); var collection = redis.GetDatabase().HashGetAll(key); foreach (var item in collection) { dic.Add(item.Name, item.Value); } return dic; } /// /// 将哈希字段的浮点值按给定数值增加 /// /// /// /// 给定的数值 /// public static double HashIncrement(string key, string field, double value) { return redis.GetDatabase().HashIncrement(key, field, value); } /// /// 获取哈希中的所有字段 /// /// /// public static string[] HashKeys(string key) { return redis.GetDatabase().HashKeys(key).ToStringArray(); } /// /// 获取散列中的字段数量 /// /// /// public static long HashSize(string key) { return redis.GetDatabase().HashLength(key); } /// /// 获取所有给定哈希字段的值 /// /// /// /// public static List HashMultiGet(string key, List hashKeys) { List result = new List(); foreach (string field in hashKeys) { result.Add(redis.GetDatabase().HashGet(key, field)); } return result; } /// /// 为多个哈希字段分别设置它们的值 /// /// /// /// public static void HashPutAll(string key, Dictionary dic) { List list = new List(); for (int i = 0; i < dic.Count; i++) { KeyValuePair param = dic.ElementAt(i); list.Add(new HashEntry(param.Key, param.Value)); } redis.GetDatabase().HashSet(key, list.ToArray()); } /// /// 设置散列字段的字符串值 /// /// /// /// /// public static void HashPut(string key, string field, string value) { redis.GetDatabase().HashSet(key, new HashEntry[] { new HashEntry(field, value) }); } /// /// 仅当字段不存在时,才设置散列字段的值 /// /// /// /// /// public static void HashPutIfAbsent(string key, string field, string value) { if (!HashHasKey(key, field)) { redis.GetDatabase().HashSet(key, new HashEntry[] { new HashEntry(field, value) }); } } /// /// 获取哈希中的所有值 /// /// /// public static string[] HashValues(string key) { return redis.GetDatabase().HashValues(key).ToStringArray(); } /// /// redis中获取指定键的值并返回对象 /// /// /// /// public static T GetHashValue(string key) { HashEntry[] array = redis.GetDatabase().HashGetAll(key); Dictionary dic = new Dictionary(); for (int i = 0; i < array.Length; i++) { dic.Add(array[i].Name, array[i].Value); } string strJson = JsonConvert.SerializeObject(dic); return JsonConvert.DeserializeObject(strJson); } /// /// 把指定对象存储在键值为key的redis中 /// /// /// /// public static void SetHashValue(T t, string key) { string strJson = JsonConvert.SerializeObject(t); Dictionary param = JsonConvert.DeserializeObject>(strJson); HashPutAll(key, param); } #endregion #region redis 列表(List)操作 /// /// 从左向右存压栈 /// /// /// /// public static long ListLeftPush(string key, string value) { return redis.GetDatabase().ListLeftPush(key, value); } /// /// 从左出栈 /// /// /// public static object ListLeftPop(string key) { return redis.GetDatabase().ListLeftPop(key); } /// /// 队/栈长 /// /// /// public static long ListSize(string key) { return redis.GetDatabase().ListLength(key); } /// /// 范围检索,返回List /// /// /// /// /// public static string[] ListRange(string key, int start, int end) { return redis.GetDatabase().ListRange(key, start, end).ToStringArray(); } /// /// 移除key中值为value的i个,返回删除的个数;如果没有这个元素则返回0 /// /// /// /// /// public static long ListRemove(string key, string value) { return redis.GetDatabase().ListRemove(key, value); } /// /// 检索 /// /// /// /// public static object ListIndex(string key, long index) { return redis.GetDatabase().ListGetByIndex(key, index); } /// /// 赋值 /// /// /// /// /// public static void ListSet(string key, int index, string value) { redis.GetDatabase().ListSetByIndex(key, index, value); } /// /// 裁剪,删除除了[start,end]以外的所有元素 /// /// /// /// /// public static void ListTrim(string key, int start, int end) { redis.GetDatabase().ListTrim(key, start, end); } /// /// 将源key的队列的右边的一个值删除,然后塞入目标key的队列的左边,返回这个值 /// /// /// /// public static object ListRightPopAndLeftPush(string sourceKey, string destinationKey) { return redis.GetDatabase().ListRightPopLeftPush(sourceKey, destinationKey); } #endregion #region redis 集合(Set)操作 /// /// 集合添加元素 /// /// /// public static void SetAdd(string key, string value) { redis.GetDatabase().SetAdd(key, value); } public static void SetAdd(string key, object value) { redis.GetDatabase().SetAdd(key, (RedisValue)value); } /// /// 集合组合操作 /// /// 操作标示:0--并集;1--交集;2--差集 /// 第一个集合的键值 /// 第二个集合的键值 public static string[] SetCombine(int point, string firstKey, string secondKey) { RedisValue[] array; switch (point) { case 0: array = redis.GetDatabase().SetCombine(SetOperation.Union, firstKey, secondKey); break; case 1: array = redis.GetDatabase().SetCombine(SetOperation.Intersect, firstKey, secondKey); break; case 2: array = redis.GetDatabase().SetCombine(SetOperation.Difference, firstKey, secondKey); break; default: array = new RedisValue[0]; break; } return array.ToStringArray(); } /// /// /// /// /// /// public static bool SetContains(string key, string value) { return redis.GetDatabase().SetContains(key, value); } /// /// 返回对应键值集合的长度 /// /// /// public static long SetLength(string key) { return redis.GetDatabase().SetLength(key); } /// /// 根据键值返回集合中所有的value /// /// /// public static string[] SetMembers(string key) { return redis.GetDatabase().SetMembers(key).ToStringArray(); } /// /// 将成员从源集移动到目标集 /// /// 源集key /// 目标集key /// public static bool SetMove(string sourceKey, string destinationKey, string value) { return redis.GetDatabase().SetMove(sourceKey, destinationKey, value); } /// /// 移除集合中指定键值随机元素 /// /// public static string SetPop(string key) { return redis.GetDatabase().SetPop(key); } /// /// 返回集合中指定键值随机元素 /// /// /// public static string SetRandomMember(string key) { return redis.GetDatabase().SetRandomMember(key); } /// /// /// /// /// public static string[] SetRandomMembers(string key, long count) { return redis.GetDatabase().SetRandomMembers(key, count).ToStringArray(); } /// /// 移除集合中指定key值和value /// /// /// public static void SetRemove(string key, string value) { redis.GetDatabase().SetRemove(key, value); } /// /// /// /// public static void SetScan(string key) { redis.GetDatabase().SetScan(key); } #endregion #region redis 有序集合(sorted set)操作 public static void Method(string key, string value, double score) { redis.GetDatabase().SortedSetAdd(key, new SortedSetEntry[] { new SortedSetEntry(value, score) }); } #endregion } }