cixiren 发表于 2015-7-21 12:35:27

redis在.net架构中的应用(1)--使用servicestack连接redis(转)

  引言:作为少有的.net架构下的大型网站,stackoverflow曾发表了一篇文章,介绍了其技术体系,原文链接http://highscalability.com/blog/2011/3/3/stack-overflow-architecture-update-now-at-95-million-page-vi.html。从文中可以看到,该网站运用了redis作为其缓存层。而新浪微博早就已经大量使用redis。作为一个新兴的nosql数据库,redis既解决了memcached持久化的问题,又在性能上和传统的memcached+mysql不相上下。
  1 redis安装与配置
  目前redis在windows上的运行还不稳定,一般都是将其部署在linux服务器下,网上可以搜到很多安装教程,本文不再赘述http://www.oschina.net/question/12_18065
  2下载servicestack.Redis
  目前redis官方版本不支持.net直接进行连接,需要使用一些开源类库。目前最流行的就是ServiceStack.redis,github链接为https://github.com/ServiceStack/ServiceStack.Redis
  点击页面右侧的DownLoad.Zip,下载后解压,在build\release\MonoDevelop\ServiceStack.Redis下找到所需要的四个dll

  3 利用servicestack连接redis
  新建一个VS project,引入以上四个dll,接下来我们就可以利用C#连接redis数据库了,一个最简单的例子:


  4 目前servicestack.redis仍然在不断发展和改进中,所以一些方法描述并不是很清晰,所以提供一个已经封装好的类共大家使用,
  redis的网络连接方式和传统的rdbms相似,一种是长连接,一种是连接池,此处使用长连接进行连接
  强烈建议在使用之前阅读注释
  





1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using ServiceStack.Redis;
7 namespace TestRedis
8 {
9   class RedisHelper:IDisposable
10   {
11         /*copyright@2013 All Rights Reserved
12          * Author:Mars
13          * Date:2013.08.27
14          * QQ:258248340
15          * servicestack.redis为github中的开源项目
16          * redis是一个典型的k/v型数据库
17          * redis共支持五种类型的数据 string,list,hash,set,sortedset
18          *
19          * string是最简单的字符串类型
20          *
21          * list是字符串列表,其内部是用双向链表实现的,因此在获取/设置数据时可以支持正负索引
22          * 也可以将其当做堆栈结构使用
23          *
24          * hash类型是一种字典结构,也是最接近RDBMS的数据类型,其存储了字段和字段值的映射,但字段值只能是
25          * 字符串类型,散列类型适合存储对象,建议使用对象类别和ID构成键名,使用字段表示对象属性,字
26          * 段值存储属性值,例如:car:2 price 500 ,car:2color black,用redis命令设置散列时,命令格式
27          * 如下:HSET key field value,即key,字段名,字段值
28          *
29          * set是一种集合类型,redis中可以对集合进行交集,并集和互斥运算
30          *         
31          * sorted set是在集合的基础上为每个元素关联了一个“分数”,我们能够
32          * 获得分数最高的前N个元素,获得指定分数范围内的元素,元素是不同的,但是"分数"可以是相同的
33          * set是用散列表和跳跃表实现的,获取数据的速度平均为o(log(N))
34          *
35          * 需要注意的是,redis所有数据类型都不支持嵌套
36          * redis中一般不区分插入和更新操作,只是命令的返回值不同
37          * 在插入key时,如果不存在,将会自动创建
38          *
39          * 在实际生产环境中,由于多线程并发的关系,建议使用连接池,本类只是用于测试简单的数据类型
40          */
41
42         /*
43          * 以下方法为基本的设置数据和取数据
44          */
45         private static RedisClient redisCli = null;
46         ///
47         /// 建立redis长连接
48         ///
49         /// 将此处的IP换为自己的redis实例IP,如果设有密码,第三个参数为密码,string 类型
50         public static void CreateClient(string hostIP,int port,string keyword)
51         {
52             if (redisCli == null)
53             {
54               redisCli = new RedisClient(hostIP, port, keyword);
55             }
56
57         }
58         public static void CreateClient(string hostIP, int port)
59         {
60             if (redisCli == null)
61             {
62               redisCli = new RedisClient(hostIP, port);
63            }
64
65         }
66         //private static RedisClient redisCli = new RedisClient("192.168.101.165", 6379, "123456");
67         ///
68         /// 获取key,返回string格式
69         ///
70         ///
71         ///
72         public static string getValueString(string key)
73         {
74         
75               string value = redisCli.GetValue(key);
76               return value;
77            
78         
79         }
80         ///
81         /// 获取key,返回byte[]格式
82         ///
83         ///
84         ///
85         public static byte[] getValueByte(string key)
86         {
87             byte[] value = redisCli.Get(key);
88             return value;
89         }
90         ///
91         /// 获得某个hash型key下的所有字段
92         ///
93         ///
94         ///
95         public static List GetHashFields(string hashId)
96         {
97             List hashFields = redisCli.GetHashKeys(hashId);
98             return hashFields;
99         }
100         ///
101         /// 获得某个hash型key下的所有值
102         ///
103         ///
104         ///
105         public static List GetHashValues(string hashId)
106         {
107             List hashValues = redisCli.GetHashKeys(hashId);
108             return hashValues;
109         }
110         ///
111         /// 获得hash型key某个字段的值
112         ///
113         ///
114         ///
115         public static string GetHashField(string key, string field)
116         {
117             string value = redisCli.GetValueFromHash(key, field);
118             return value;
119         }
120         ///
121         /// 设置hash型key某个字段的值
122         ///
123         ///
124         ///
125         ///
126         public static void SetHashField(string key, string field, string value)
127         {
128             redisCli.SetEntryInHash(key, field, value);
129         }
130         ///
131         ///使某个字段增加
132         ///
133         ///
134         ///
135         ///
136         public static void SetHashIncr(string key, string field, long incre)
137         {
138             redisCli.IncrementValueInHash(key, field, incre);
139
140         }
141         ///
142         /// 向list类型数据添加成员,向列表底部(右侧)添加
143         ///
144         ///
145         ///
146         public static void AddItemToListRight(string list, string item)
147         {
148             redisCli.AddItemToList(list, item);
149         }
150         ///
151         /// 向list类型数据添加成员,向列表顶部(左侧)添加
152         ///
153         ///
154         ///
155         public static void AddItemToListLeft(string list, string item)
156         {
157             redisCli.LPush(list, Encoding.Default.GetBytes(item));
158         }
159         ///
160         /// 从list类型数据读取所有成员
161         ///
162         public static List GetAllItems(string list)
163         {
164             List listMembers = redisCli.GetAllItemsFromList(list);
165             return listMembers;
166         }
167         ///
168         /// 从list类型数据指定索引处获取数据,支持正索引和负索引
169         ///
170         ///
171         ///
172         public static string GetItemFromList(string list, int index)
173         {
174             string item = redisCli.GetItemFromList(list, index);
175             return item;
176         }
177         ///
178         /// 向列表底部(右侧)批量添加数据
179         ///
180         ///
181         ///
182         public static void GetRangeToList(string list, List values)
183         {
184             redisCli.AddRangeToList(list, values);
185         }
186         ///
187         /// 向集合中添加数据
188         ///
189         ///
190         ///
191         public static void GetItemToSet(string item, string set)
192         {
193             redisCli.AddItemToSet(item, set);
194         }
195         ///
196         /// 获得集合中所有数据
197         ///
198         ///
199         ///
200         public static HashSet GetAllItemsFromSet(string set)
201         {
202             HashSet items = redisCli.GetAllItemsFromSet(set);
203             return items;
204         }
205         ///
206         /// 获取fromSet集合和其他集合不同的数据
207         ///
208         ///
209         ///
210         ///
211         public static HashSet GetSetDiff(string fromSet, params string[] toSet)
212         {
213             HashSet diff = redisCli.GetDifferencesFromSet(fromSet, toSet);
214             return diff;
215         }
216         ///
217         /// 获得所有集合的并集
218         ///
219         ///
220         ///
221         public static HashSet GetSetUnion(params string[] set)
222         {
223             HashSet union = redisCli.GetUnionFromSets(set);
224             return union;
225         }
226         ///
227         /// 获得所有集合的交集
228         ///
229         ///
230         ///
231         public static HashSet GetSetInter(params string[] set)
232         {
233             HashSet inter = redisCli.GetIntersectFromSets(set);
234             return inter;
235         }
236         ///
237         /// 向有序集合中添加元素
238         ///
239         ///
240         ///
241         ///
242         public static void AddItemToSortedSet(string set,string value,long score)
243         {
244             redisCli.AddItemToSortedSet(set,value,score);
245         }
246         ///
247         /// 获得某个值在有序集合中的排名,按分数的降序排列
248         ///
249         ///
250         ///
251         ///
252         public static int GetItemIndexInSortedSetDesc(string set, string value)
253         {
254             int index = redisCli.GetItemIndexInSortedSetDesc(set, value);
255             return index;
256         }
257         ///
258         /// 获得某个值在有序集合中的排名,按分数的升序排列
259         ///
260         ///
261         ///
262         ///
263         public static int GetItemIndexInSortedSet(string set, string value)
264         {
265             int index = redisCli.GetItemIndexInSortedSet(set, value);
266             return index;
267         }
268         ///
269         /// 获得有序集合中某个值得分数
270         ///
271         ///
272         ///
273         ///
274         public static double GetItemScoreInSortedSet(string set, string value)
275         {
276             double score = redisCli.GetItemScoreInSortedSet(set, value);
277             return score;
278         }
279         ///
280         /// 获得有序集合中,某个排名范围的所有值
281         ///
282         ///
283         ///
284         ///
285         ///
286         public static List GetRangeFromSortedSet(string set,int beginRank, int endRank)
287         {
288             List valueList=redisCli.GetRangeFromSortedSet(set,beginRank,endRank);
289             return valueList;
290         }
291         ///
292         /// 获得有序集合中,某个分数范围内的所有值,升序
293         ///
294         ///
295         ///
296         ///
297         ///
298         public static List GetRangeFromSortedSet(string set, double beginScore, double endScore)
299         {
300             List valueList = redisCli.GetRangeFromSortedSetByHighestScore(set, beginScore, endScore);
301             return valueList;
302         }
303         ///
304         /// 获得有序集合中,某个分数范围内的所有值,降序
305         ///
306         ///
307         ///
308         ///
309         ///
310         public static List GetRangeFromSortedSetDesc(string set, double beginScore, double endScore)
311         {
312             List vlaueList=redisCli.GetRangeFromSortedSetByLowestScore(set,beginScore,endScore);
313             return vlaueList;
314         }
315         public void Dispose()
316         {
317             redisCli.Dispose();
318         }
319
320   }
321 }

  
  本文就到这里,在接下来,我会跟大家讨论redis中的连接池、锁、线程安全、分布式等机制。
  
  作者:Mars
  
  出处:http://www.iyunv.com/marsblog/
  
  本文基于署名-非商业性使用 3.0中国大陆许可协议发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名 Mars (包含链接)
页: [1]
查看完整版本: redis在.net架构中的应用(1)--使用servicestack连接redis(转)