-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdemo2.cpp
More file actions
64 lines (63 loc) · 1.98 KB
/
demo2.cpp
File metadata and controls
64 lines (63 loc) · 1.98 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
/*基本使用
* */
#include<iostream>
#include"credis.h"
#include<algorithm>
#include<iterator>
using namespace std;
int main()
{
Redis redis;
string ip="127.0.0.1";
int port = 6379;
string password="";
redis.init(ip, port, password);//init
redis.connect();//connect
vector<string> keys={"str1","str2","str3","str4","str5"};
vector<string> values={"1s","2s","3s","4s","5s"};
redis.mset(keys,values);//
keys.clear();
redis.keys("str?", keys);
sort(keys.begin(),keys.end());
cout<<"all keys are:"<<endl;
copy(keys.begin(),keys.end(),ostream_iterator<string>(cout," "));
cout<<endl;
vector<string> v;
redis.mget(keys,v);//
cout<<"values ars"<<endl;
copy(v.begin(), v.end(),ostream_iterator<string>(cout, " "));cout<<endl;
cout<<"delete all keys like str?"<<endl;
for(int i=0;i<keys.size();++i)
{
redis.del(keys[i]);//
}
string key = "str1";
string t = redis.type(key);//
cout<<"type of str1 is "<<t<<endl;
{
cout<<"hash操作:"<<endl;
vector<string> fields={"f1","f2","f3","f4","f5"};
vector<string> values={"hv1","hv2","hv3","hv4","hv5"};
redis.hmset("hash_test",fields,values);//
map<string,string> hash;
redis.hgetall("hash_test",hash);//
for(map<string,string>::iterator it=hash.begin();it!=hash.end();++it)
{
cout<<it->first<<" "<<it->second<<endl;
}
cout<<endl;
values.clear();
redis.hmget("hash_test",fields,values);//
cout<<"hmget"<<endl;
copy(values.begin(),values.end(),ostream_iterator<string>(cout," "));cout<<endl;
redis.del("hash_test");
}
cout<<"获取string类型的keys:"<<endl;
vector<string> ks;
redis.getKeys("strinG", "", 35,ks);//
cout<<"共获取keys个数:"<<ks.size()<<endl;
copy(ks.begin(),ks.end(),ostream_iterator<string>(cout," "));cout<<endl;
//while(1);
redis.disconnect();
redis.disconnect();//重复dis看看
}