一个支持集群版 redis3.0 的客户端例子

使用 acl redis 模块库编写的支持集群版 redis3.0 的例子,该例子采用多线程方式连接 redis 服务器,支持 redis 客户端连接池连接方式,自动进行结点重定向及哈希槽缓存,以及哈希槽的预缓存策略。
该示例在 acl 库中的位置:lib_acl_cpp\samples\redis\redis_client_cluster2

更多参考:
acl 库的国内镜像:http://git.oschina.net/zsxxsz/acl
acl 库下载:http://sourceforge.net/projects/acl/
acl 库的 github:https://github.com/zhengshuxin/acl
更多技术文章:http://zsxxsz.iteye.com

标签: acl

代码片段(1)[全屏查看所有代码]

1. [代码][C/C++]代码    跳至 [1] [全屏预览]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include "stdafx.h"
#include "util.h"
 
static acl::string __keypre( "test_key_cluster" );
 
static bool test_del(acl::redis& cmd, int i)
{
     cmd.clear();
 
     acl::string key;
 
     key.format( "%s_%d" , __keypre.c_str(), i);
     int ret = cmd.del(key.c_str(), NULL);
     if (ret < 0)
     {
         printf ( "del key: %s error\r\n" , key.c_str());
         return false ;
     }
     else if (i < 10)
         printf ( "del ok, key: %s\r\n" , key.c_str());
     return true ;
}
 
static bool test_expire(acl::redis& cmd, int i)
{
     cmd.clear();
 
     acl::string key;
 
     key.format( "%s_%d" , __keypre.c_str(), i);
     if (cmd.expire(key.c_str(), 100) < 0)
     {
         printf ( "expire key: %s error\r\n" , key.c_str());
         return false ;
     }
     else if (i < 10)
         printf ( "expire ok, key: %s\r\n" , key.c_str());
     return true ;
}
 
static bool test_ttl(acl::redis& cmd, int i)
{
     cmd.clear();
 
     acl::string key;
     int ttl;
 
     key.format( "%s_%d" , __keypre.c_str(), i);
     if ((ttl = cmd.ttl(key.c_str())) < 0)
     {
         printf ( "get ttl key: %s error\r\n" , key.c_str());
         return false ;
     }
     else if (i < 10)
         printf ( "ttl ok, key: %s, ttl: %d\r\n" , key.c_str(), ttl);
     return true ;
}
 
static bool test_exists(acl::redis& cmd, int i)
{
     cmd.clear();
 
     acl::string key;
 
     key.format( "%s_%d" , __keypre.c_str(), i);
     if (cmd.exists(key.c_str()) == false )
     {
         if (i < 10)
             printf ( "no exists key: %s\r\n" , key.c_str());
     }
     else
     {
         if (i < 10)
             printf ( "exists key: %s\r\n" , key.c_str());
     }
     return true ;
}
 
static bool test_type(acl::redis& cmd, int i)
{
     cmd.clear();
 
     acl::string key;
 
     key.format( "%s_%d" , __keypre.c_str(), i);
     acl::redis_key_t ret = cmd.type(key.c_str());
     if (ret == acl::REDIS_KEY_UNKNOWN)
     {
         printf ( "unknown type key: %s\r\n" , key.c_str());
         return false ;
     }
     else if (i < 10)
         printf ( "type ok, key: %s, ret: %d\r\n" , key.c_str(), ret);
     return true ;
}
 
static bool test_set(acl::redis& cmd, int i)
{
     cmd.clear();
 
     acl::string key;
     key.format( "%s_%d" , __keypre.c_str(), i);
 
     acl::string value;
     value.format( "value_%s" , key.c_str());
 
     bool ret = cmd.set(key.c_str(), value.c_str());
     return ret;
     if (i < 10)
         printf ( "set key: %s, value: %s %s\r\n" , key.c_str(),
             value.c_str(), ret ? "ok" : "error" );
     return ret;
}
 
static bool test_get(acl::redis& cmd, int i)
{
     cmd.clear();
 
     acl::string key;
     key.format( "%s_%d" , __keypre.c_str(), i);
 
     acl::string value;
 
     bool ret = cmd.get(key.c_str(), value);
     if (i < 10)
         printf ( "get key: %s, value: %s %s, len: %d\r\n" ,
             key.c_str(), value.c_str(), ret ? "ok" : "error" ,
             ( int ) value.length());
     return ret;
}
 
static bool test_lrem(acl::redis& cmd, int i)
{
     cmd.clear();
 
     acl::string key;
     key.format( "list_%s_%d" , __keypre.c_str(), i);
 
     int ret = cmd.lrem(key.c_str(), 0, "list_value" );
     if (i < 10)
         printf ( "lrem key: %s, ret: %d\r\n" , key.c_str(), ret);
     return i >= 0 ? true : false ;
}
 
static int __threads_exit = 0;
 
class test_thread : public acl:: thread
{
public :
     test_thread(acl::locker& locker, acl::redis_client_cluster& cluster,
         int max_conns, const char * cmd, int n)
     : locker_(locker)
     , cluster_(cluster)
     , max_conns_(max_conns)
     , cmd_(cmd)
     , n_(n)
     {}
 
     ~test_thread() {}
 
protected :
     virtual void * run()
     {
         bool ret;
         acl::redis cmd;
         cmd.set_cluster(&cluster_, max_conns_);
 
         for ( int i = 0; i < n_; i++)
         {
             if (cmd_ == "set" )
                 ret = test_set(cmd, i);
             else if (cmd_ == "get" )
                 ret = test_get(cmd, i);
             else if (cmd_ == "del" )
                 ret = test_del(cmd, i);
             else if (cmd_ == "expire" )
                 ret = test_expire(cmd, i);
             else if (cmd_ == "ttl" )
                 ret = test_ttl(cmd, i);
             else if (cmd_ == "exists" )
                 ret = test_exists(cmd, i);
             else if (cmd_ == "type" )
                 ret = test_type(cmd, i);
             else if (cmd_ == "lrem" )
                 ret = test_lrem(cmd, i);
             else if (cmd_ == "all" )
             {
                 if (test_set(cmd, i) == false
                     || test_get(cmd, i) == false
                     || test_exists(cmd, i) == false
                     || test_type(cmd, i) == false
                     || test_expire(cmd, i) == false
                     || test_ttl(cmd, i) == false
                     || test_del(cmd, i) == false )
                 {
                     ret = false ;
                 }
                 else
                     ret = true ;
             }
             else
             {
                 printf ( "unknown cmd: %s\r\n" , cmd_.c_str());
                 break ;
             }
 
             if (ret == false )
             {
                 printf ( "cmd: %s error, tid: %lu\r\n" ,
                     cmd_.c_str(), thread_self());
                 break ;
             }
 
             if (i > 0 && i % 1000 == 0)
             {
                 char tmp[128];
                 acl::safe_snprintf(tmp, sizeof (tmp), "%d" , i);
                 acl::meter_time(__FILE__, __LINE__, tmp);
             }
         }
 
         locker_.lock();
         __threads_exit++;
         locker_.unlock();
 
         return NULL;
     }
 
private :
     acl::locker& locker_;
     acl::redis_client_cluster& cluster_;
     int max_conns_;
     acl::string cmd_;
     int n_;
};
 
static void usage( const char * procname)
{
     printf ( "usage: %s -h[help]\r\n"
         "-s redis_addr_list[127.0.0.1:6379]\r\n"
         "-n count[default: 10]\r\n"
         "-C connect_timeout[default: 10]\r\n"
         "-I rw_timeout[default: 10]\r\n"
         "-c max_threads[default: 10]\r\n"
         "-w wait_for_cluster_resume[default: 500 ms]\r\n"
         "-r retry_for_cluster_resnum[default: 10]\r\n"
         "-p [preset all hash-slots of the cluster]\r\n"
         "-a cmd[set|get|expire|ttl|exists|type|del|lrem]\r\n" ,
         procname);
}
 
int main( int argc, char * argv[])
{
     int  ch, n = 1, conn_timeout = 10, rw_timeout = 10;
     int  max_threads = 10, nsleep = 500, nretry = 10;
     acl::string addrs( "127.0.0.1:6379" ), cmd;
     bool preset = false ;
 
     while ((ch = getopt(argc, argv, "hs:n:C:I:c:a:w:r:p" )) > 0)
     {
         switch (ch)
         {
         case 'h' :
             usage(argv[0]);
             return 0;
         case 's' :
             addrs = optarg;
             break ;
         case 'n' :
             n = atoi (optarg);
             break ;
         case 'C' :
             conn_timeout = atoi (optarg);
             break ;
         case 'I' :
             rw_timeout = atoi (optarg);
             break ;
         case 'c' :
             max_threads = atoi (optarg);
             break ;
         case 'a' :
             cmd = optarg;
             break ;
         case 'w' :
             nsleep = atoi (optarg);
             break ;
         case 'r' :
             nretry = atoi (optarg);
             break ;
         case 'p' :
             preset = true ;
             break ;
         default :
             break ;
         }
     }
 
     acl::acl_cpp_init();
     acl:: log ::stdout_open( true );
 
     acl::redis_client_cluster cluster(conn_timeout, rw_timeout);
 
     // 当某个连接池结点出问题,设置探测该连接结点是否恢复的时间间隔(秒),当该值
     // 为 0 时,则不检测
     cluster.set_retry_inter(1);
 
     // 设置重定向的最大阀值,若重定向次数超过此阀值则报错
     cluster.set_redirect_max(nretry);
 
     // 当重定向次数 >= 2 时每次再重定向此函数设置休息的时间(毫秒)
     cluster.set_redirect_sleep(nsleep);
 
     cluster.init(NULL, addrs.c_str(), max_threads);
 
     // 是否需要将所有哈希槽的对应关系提前设置好,这样可以去掉运行时动态添加
     // 哈希槽的过程,从而可以提高运行时的效率
     if (preset)
     {
         const std::vector<acl::string>& token = addrs.split2( ",; \t" );
         cluster.set_all_slot(token[0], max_threads);
     }
 
     struct timeval begin;
     gettimeofday(&begin, NULL);
 
     acl::locker locker;
 
     std::vector<test_thread*> threads;
     for ( int i = 0; i < max_threads; i++)
     {
         test_thread* thread = new test_thread(locker, cluster,
             max_threads, cmd.c_str(), n);
         threads.push_back( thread );
         thread ->set_detachable( true );
         thread ->start();
     }
 
     while ( true )
     {
         locker.lock();
         if (__threads_exit == max_threads)
         {
             locker.unlock();
             printf ( "All threads over now!\r\n" );
             break ;
         }
         locker.unlock();
 
         //printf("max_threads: %d, threads_exit: %d\r\n",
         //  max_threads, __threads_exit);
         sleep(1);
     }
 
     std::vector<test_thread*>::iterator it = threads.begin();
     for (; it != threads.end(); ++it)
     {
         //(*it)->wait();
         delete (*it);
     }
 
     struct timeval end;
     gettimeofday(&end, NULL);
 
     long long int total = max_threads * n;
     double inter = util::stamp_sub(&end, &begin);
     printf ( "total %s: %lld, spent: %0.2f ms, speed: %0.2f\r\n" , cmd.c_str(),
         total, inter, (total * 1000) /(inter > 0 ? inter : 1));
 
#ifdef WIN32
     printf ( "enter any key to exit\r\n" );
     getchar ();
#endif
     return 0;
}



FROM:  http://www.oschina.net/code/snippet_568966_47162




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值