1 using System; 2 using StackExchange.Redis; 3 namespace StackExchangeRedis 4 {
5 public class RedisHelper 6 { 7 8 private ConnectionMultiplexer Redis { get; set; } 9 public ITransaction RedisTransaction { get; set; }//事务 10 public IBatch CreateBatch { get; set; }//批量 11 private IDatabase DB { get; set; } 12 public RedisHelper(string connection) 13 { 14 Redis = ConnectionMultiplexer.Connect(connection); 15 DB = Redis.GetDatabase(); 16 RedisTransaction = DB.CreateTransaction(); 17 CreateBatch = DB.CreateBatch(); 18 } 19 20 /// <summary> 21 /// 增加/修改 22 /// </summary> 23 /// <param name="key"></param> 24 /// <param name="value"></param> 25 /// <returns></returns> 26 public bool SetValue(string key, string value) 27 { 28 return DB.StringSet(key, value); 29 } 30 /// <summary> 31 /// 设置缓存过期时间 32 /// </summary> 33 /// <param name="key"></param> 34 /// <param name="value"></param> 35 /// <param name="t"></param> 36 /// <returns></returns> 37 public bool SetValue(string key, string value, TimeSpan t) 38 { 39 return DB.StringSet(key, value, t); 40 41 } 42 43 44 /// <summary> 45 /// 查询 46 /// </summary> 47 /// <param name="key"></param> 48 /// <returns></returns> 49 public string GetValue(string key) 50 { 51 return DB.StringGet(key); 52 } 53 54 55 /// <summary> 56 /// 设置增量 57 /// </summary> 58 /// <param name="key"></param> 59 /// <param name="value"></param> 60 /// <returns></returns> 61 public double StringIncrement(string key, long value) 62 { 63 return DB.StringIncrement(key, value); 64 65 } 66 /// <summary> 67 /// 设置减量 68 /// </summary> 69 /// <param name="key"></param> 70 /// <param name="value"></param> 71 /// <returns></returns> 72 public double StringDecrement(string key, long value) 73 { 74 return DB.StringDecrement(key, value); 75 76 } 77 /// <summary> 78 /// 从顶部插入 79 /// </summary> 80 /// <param name="keylist"></param> 81 /// <param name="value"></param> 82 /// <returns></returns> 83 public long ListLeftPush(string keylist, string value) 84 { 85 86 return DB.ListLeftPush(keylist, value); 87 88 } 89 /// <summary> 90 /// 从底部插入 91 /// </summary> 92 /// <param name="keylist"></param> 93 /// <param name="value"></param> 94 /// <returns></returns> 95 public long ListRightPush(string keylist, string value) 96 { 97 98 return DB.ListRightPush(keylist, value); 99 100 } 101 /// <summary> 102 /// list 长度 103 /// </summary> 104 /// <param name="keylist"></param> 105 /// <returns></returns> 106 public long ListLength(string keylist) 107 { 108 109 return DB.ListLength(keylist); 110 111 } 112 /// <summary> 113 /// 从顶部抽取数据 114 /// </summary> 115 /// <param name="key"></param> 116 /// <returns></returns> 117 public string ListLeftPop(string key) 118 { 119 return DB.ListLeftPop(key); 120 121 } 122 123 /// <summary> 124 /// 从底部抽取数据 125 /// </summary> 126 /// <param name="key"></param> 127 /// <returns></returns> 128 public string ListRightPop(string key) 129 { 130 return DB.ListRightPop(key); 131 132 } 133 /// <summary> 134 /// 设置hash 135 /// </summary> 136 /// <param name="key"></param> 137 /// <param name="hashvalue"></param> 138 /// <param name="value"></param> 139 /// <returns></returns> 140 public bool HashSet(string key, string hashvalue, string value) 141 { 142 return DB.HashSet(key, hashvalue, value); 143 144 } 145 146 /// <summary> 147 /// 获取Hash值 148 /// </summary> 149 /// <param name="key"></param> 150 /// <param name="hashvalue"></param> 151 /// <returns></returns> 152 public RedisValue HashGet(string key, string hashvalue) 153 { 154 return DB.HashGet(key, hashvalue); 155 156 } 157 /// <summary> 158 /// 获取所有listkey值 159 /// </summary> 160 /// <param name="key"></param> 161 /// <returns></returns> 162 public RedisValue[] ListRange(string key) 163 { 164 return DB.ListRange(key); 165 166 } 167 /// <summary> 168 /// 获取所有hash的值 169 /// </summary> 170 /// <param name="key"></param> 171 /// <returns></returns> 172 public RedisValue[] HashValues(string key) 173 { 174 return DB.HashValues(key); 175 176 } 177 178 179 180 /// <summary> 181 /// 删除 182 /// </summary> 183 /// <param name="key"></param> 184 /// <returns></returns> 185 public bool DeleteKey(string key) 186 { 187 return DB.KeyDelete(key); 188 } 189 190 } 191 }
ServiceStack.Redis
using ServiceStack.Redis; using System; using System.Collections.Generic; using System.Text; namespace ConsoleRedis { internal class RedisHelper : IDisposable { /* * redis是一个典型的k/v型数据库 * redis共支持五种类型的数据 string,list,hash,set,sortedset * * string是最简单的字符串类型 * * list是字符串列表,其内部是用双向链表实现的,因此在获取/设置数据时可以支持正负索引 * 也可以将其当做堆栈结构使用 * * hash类型是一种字典结构,也是最接近RDBMS的数据类型,其存储了字段和字段值的映射,但字段值只能是 * 字符串类型,散列类型适合存储对象,建议使用对象类别和ID构成键名,使用字段表示对象属性,字 * 段值存储属性值,例如:car:2 price 500 ,car:2 color black,用redis命令设置散列时,命令格式 * 如下:HSET key field value,即key,字段名,字段值 * * set是一种集合类型,redis中可以对集合进行交集,并集和互斥运算 * * sorted set是在集合的基础上为每个元素关联了一个“分数”,我们能够 * 获得分数最高的前N个元素,获得指定分数范围内的元素,元素是不同的,但是"分数"可以是相同的 * set是用散列表和跳跃表实现的,获取数据的速度平均为o(log(N)) * * 需要注意的是,redis所有数据类型都不支持嵌套 * redis中一般不区分插入和更新操作,只是命令的返回值不同 * 在插入key时,如果不存在,将会自动创建 * * 在实际生产环境中,由于多线程并发的关系,建议使用连接池,本类只是用于测试简单的数据类型 */ /* * 以下方法为基本的设置数据和取数据 */ private static RedisClient redisCli = null; /// <summary> /// 建立redis长连接 /// </summary> /// 将此处的IP换为自己的redis实例IP,如果设有密码,第三个参数为密码,string 类型 public static void CreateClient(string hostIP, int port, string keyword) { if (redisCli == null) { redisCli = new RedisClient(hostIP, port, keyword); } } public static void CreateClient(string hostIP, int port) { if (redisCli == null) { redisCli = new RedisClient(hostIP, port); } } /// <summary> /// 获取key,返回string格式 /// </summary> /// <param name="key"></param> /// <returns></returns> public static string getValueString(string key) { string value = redisCli.GetValue(key); return value; } /// <summary> /// 获取key,返回byte[]格式 /// </summary> /// <param name="key"></param> /// <returns></returns> public static byte[] getValueByte(string key) { byte[] value = redisCli.Get(key); return value; } /// <summary> /// 获得某个hash型key下的所有字段 /// </summary> /// <param name="hashId"></param> /// <returns></returns> public static List<string> GetHashFields(string hashId) { List<string> hashFields = redisCli.GetHashKeys(hashId); return hashFields; } /// <summary> /// 获得某个hash型key下的所有值 /// </summary> /// <param name="hashId"></param> /// <returns></returns> public static List<string> GetHashValues(string hashId) { List<string> hashValues = redisCli.GetHashKeys(hashId); return hashValues; } /// <summary> /// 获得hash型key某个字段的值 /// </summary> /// <param name="key"></param> /// <param name="field"></param> public static string GetHashField(string key, string field) { string value = redisCli.GetValueFromHash(key, field); return value; } /// <summary> /// 设置hash型key某个字段的值 /// </summary> /// <param name="key"></param> /// <param name="field"></param> /// <param name="value"></param> public static void SetHashField(string key, string field, string value) { redisCli.SetEntryInHash(key, field, value); } /// <summary> ///使某个字段增加 /// </summary> /// <param name="key"></param> /// <param name="field"></param> /// <returns></returns> public static void SetHashIncr(string key, string field, long incre) { redisCli.IncrementValueInHash(key, field, incre); } /// <summary> /// 向list类型数据添加成员,向列表底部(右侧)添加 /// </summary> /// <param name="Item"></param> /// <param name="list"></param> public static void AddItemToListRight(string list, string item) { redisCli.AddItemToList(list, item); } /// <summary> /// 向list类型数据添加成员,向列表顶部(左侧)添加 /// </summary> /// <param name="list"></param> /// <param name="item"></param> public static void AddItemToListLeft(string list, string item) { redisCli.LPush(list, Encoding.Default.GetBytes(item)); } /// <summary> /// 从list类型数据读取所有成员 /// </summary> public static List<string> GetAllItems(string list) { List<string> listMembers = redisCli.GetAllItemsFromList(list); return listMembers; } /// <summary> /// 从list类型数据指定索引处获取数据,支持正索引和负索引 /// </summary> /// <param name="list"></param> /// <returns></returns> public static string GetItemFromList(string list, int index) { string item = redisCli.GetItemFromList(list, index); return item; } /// <summary> /// 向列表底部(右侧)批量添加数据 /// </summary> /// <param name="list"></param> /// <param name="values"></param> public static void GetRangeToList(string list, List<string> values) { redisCli.AddRangeToList(list, values); } /// <summary> /// 向集合中添加数据 /// </summary> /// <param name="item"></param> /// <param name="set"></param> public static void GetItemToSet(string item, string set) { redisCli.AddItemToSet(item, set); } /// <summary> /// 获得集合中所有数据 /// </summary> /// <param name="set"></param> /// <returns></returns> public static HashSet<string> GetAllItemsFromSet(string set) { HashSet<string> items = redisCli.GetAllItemsFromSet(set); return items; } /// <summary> /// 获取fromSet集合和其他集合不同的数据 /// </summary> /// <param name="fromSet"></param> /// <param name="toSet"></param> /// <returns></returns> public static HashSet<string> GetSetDiff(string fromSet, params string[] toSet) { HashSet<string> diff = redisCli.GetDifferencesFromSet(fromSet, toSet); return diff; } /// <summary> /// 获得所有集合的并集 /// </summary> /// <param name="set"></param> /// <returns></returns> public static HashSet<string> GetSetUnion(params string[] set) { HashSet<string> union = redisCli.GetUnionFromSets(set); return union; } /// <summary> /// 获得所有集合的交集 /// </summary> /// <param name="set"></param> /// <returns></returns> public static HashSet<string> GetSetInter(params string[] set) { HashSet<string> inter = redisCli.GetIntersectFromSets(set); return inter; } /// <summary> /// 向有序集合中添加元素 /// </summary> /// <param name="set"></param> /// <param name="value"></param> /// <param name="score"></param> public static void AddItemToSortedSet(string set, string value, long score) { redisCli.AddItemToSortedSet(set, value, score); } /// <summary> /// 获得某个值在有序集合中的排名,按分数的降序排列 /// </summary> /// <param name="set"></param> /// <param name="value"></param> /// <returns></returns> public static int GetItemIndexInSortedSetDesc(string set, string value) { int index = (int)redisCli.GetItemIndexInSortedSetDesc(set, value); return index; } /// <summary> /// 获得某个值在有序集合中的排名,按分数的升序排列 /// </summary> /// <param name="set"></param> /// <param name="value"></param> /// <returns></returns> public static int GetItemIndexInSortedSet(string set, string value) { int index = (int)redisCli.GetItemIndexInSortedSet(set, value); return index; } /// <summary> /// 获得有序集合中某个值得分数 /// </summary> /// <param name="set"></param> /// <param name="value"></param> /// <returns></returns> public static double GetItemScoreInSortedSet(string set, string value) { double score = redisCli.GetItemScoreInSortedSet(set, value); return score; } /// <summary> /// 获得有序集合中,某个排名范围的所有值 /// </summary> /// <param name="set"></param> /// <param name="beginRank"></param> /// <param name="endRank"></param> /// <returns></returns> public static List<string> GetRangeFromSortedSet(string set, int beginRank, int endRank) { List<string> valueList = redisCli.GetRangeFromSortedSet(set, beginRank, endRank); return valueList; } /// <summary> /// 获得有序集合中,某个分数范围内的所有值,升序 /// </summary> /// <param name="set"></param> /// <param name="beginScore"></param> /// <param name="endScore"></param> /// <returns></returns> public static List<string> GetRangeFromSortedSet(string set, double beginScore, double endScore) { List<string> valueList = redisCli.GetRangeFromSortedSetByHighestScore(set, beginScore, endScore); return valueList; } /// <summary> /// 获得有序集合中,某个分数范围内的所有值,降序 /// </summary> /// <param name="set"></param> /// <param name="beginScore"></param> /// <param name="endScore"></param> /// <returns></returns> public static List<string> GetRangeFromSortedSetDesc(string set, double beginScore, double endScore) { List<string> vlaueList = redisCli.GetRangeFromSortedSetByLowestScore(set, beginScore, endScore); return vlaueList; } public void Dispose() { redisCli.Dispose(); } } }