-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlua.cpp
More file actions
112 lines (95 loc) · 2.59 KB
/
Copy pathlua.cpp
File metadata and controls
112 lines (95 loc) · 2.59 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
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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <hiredis/hiredis.h>
#include <boost/thread/thread.hpp>
using namespace std;
struct timeval _timeout ;
redisContext *_redisContext;
const long long SEC_TO_USEC = 1000000 ;
void connect(const std::string &ip,
int port,
int timeoutInUsec )
{
_timeout.tv_sec = timeoutInUsec / SEC_TO_USEC ;
_timeout.tv_usec = timeoutInUsec % SEC_TO_USEC ;
_redisContext = redisConnectWithTimeout(ip.c_str(), port, _timeout);
if (_redisContext->err)
{
std::cout << "Cannot connect to redis server. "
<< " Error : " << _redisContext->errstr
<< std::endl ;
exit(1);
}
}
string scriptSingleCommand = "local link_id = redis.call(\"INCR\", KEYS[1]) "
"return link_id ";
string scriptMultipleCommands =
"local link_id = redis.call(\"INCR\", KEYS[1]) "
"redis.call(\"HSET\", KEYS[2], link_id, ARGV[1]) "
"local data = redis.call(\"HGETALL\",KEYS[2]) "
"return data ";
void eval()
{
redisReply *reply =
( redisReply * ) redisCommand( _redisContext,
"eval %s 1 %s", "return {KEYS[1]}", "key1") ;
if (reply->type == REDIS_REPLY_ARRAY) {
for (int j = 0; j < reply->elements; j++) {
cout<< j <<","<<reply->element[j]->str<<endl;
}
}
cout<<"EVAL: "<< reply->str<<endl;
freeReplyObject(reply);
}
void evalSingleCommand()
{
string command;
command.append( scriptSingleCommand );
redisReply *reply =
( redisReply * ) redisCommand( _redisContext,
"EVAL %s %d %s ",command.c_str(),1,"a");
if (reply->type == REDIS_REPLY_ARRAY) {
for (int j = 0; j < reply->elements; j++) {
cout<< j <<","<<reply->element[j]->str<<endl;
}
}
cout<<endl<<"EVAL: "<< reply->str<<endl;
freeReplyObject(reply);
}
void evalMultipleCommands(char** argv)
{
string command;
command.append( scriptMultipleCommands );
redisReply *reply =
( redisReply * ) redisCommand( _redisContext,
"EVAL %s %d %s %s %s",command.c_str(),2,argv[1],argv[2],argv[3]);
cout<<"Redis reply type "<<reply->type<<endl;
if (reply->type == REDIS_REPLY_ARRAY)
{
cout<<"Redis reply size "<<reply->elements<<endl;
for (int j = 0; j < reply->elements; j++)
{
if((j+1) < reply->elements)
{
cout<<(reply->element[j]->str)<<","<<(reply->element[j+1]->str)<<endl;
++j;
}
}
}
else if (reply->type == REDIS_REPLY_INTEGER) {
cout<<"Key value "<<reply->integer<<endl;
}
else
cout<<endl<<"EVAL: "<< reply->str<<endl;
freeReplyObject(reply);
}
int main(int argc,char** argv)
{
connect("10.0.0.30",6379,1500000);
// eval();
// evalSingleCommand();
evalMultipleCommands(argv);
return 0;
}