-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdemo4.cpp
More file actions
79 lines (68 loc) · 2.04 KB
/
demo4.cpp
File metadata and controls
79 lines (68 loc) · 2.04 KB
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
/*redis_connect_pool测试
*
* */
#include<iostream>
#include<algorithm>
#include<iterator>
#include"credis.h"
#include"redis_connect_pool.h"
using namespace std;
int main()
{ int pool_size = 100;
string ip = "127.0.0.1";
int port = 6379;
string passwd ="yehang";
RedisConnectPool rc_pool;
if(!rc_pool.init(pool_size, ip, port, passwd))
{
cout<<"rc_pool init failure!"<<endl;
exit(0);
}
//1
cout<<"used num : "<<rc_pool.getUsedCount()<<endl;
cout<<"canUse num : "<<rc_pool.getCanUseNum()<<endl;
rc_pool.getConnect();
//2
cout<<"used num : "<<rc_pool.getUsedCount()<<endl;
cout<<"canUse num : "<<rc_pool.getCanUseNum()<<endl;
redisContext* context = rc_pool.getConnect();
if(context == NULL)
exit(0);
Redis r1(context);
r1.set("hello", "hi");
cout<<r1.get("hello")<<endl;
r1.del("hello");
rc_pool.releaseConnect(context);//回收
//3
cout<<"used num : "<<rc_pool.getUsedCount()<<endl;
cout<<"canUse num : "<<rc_pool.getCanUseNum()<<endl;
context = rc_pool.getConnect();
if(!rc_pool.isUseful(context))
{
cout<<"[ERROR] context is not in use "<<__func__<<__LINE__<<endl;
}
Redis redis(context);//初始化,这种方式会自动禁用disconnect
redis.set("str", "hello");
string v;
v = redis.get("str");
cout<<"get key "<<v<<endl;
redis.del("str");
redis.disconnect();//这句话无效
//rc_pool.releaseConnect(context);//回收
//4
cout<<"used num : "<<rc_pool.getUsedCount()<<endl;
cout<<"canUse num : "<<rc_pool.getCanUseNum()<<endl;
context = rc_pool.getConnect();
if(context==NULL)
cout<<"no context"<<endl;
Redis redis1(context);
cout<<redis1.get("str")<<endl;
rc_pool.releaseConnect(context);
rc_pool.releaseConnect(context);//重复回收
//5
cout<<"used num : "<<rc_pool.getUsedCount()<<endl;
cout<<"canUse num : "<<rc_pool.getCanUseNum()<<endl;
vector<string> vs;
redis.keys("*",vs);
//for(;;);
}