1
+ #include " libredis.hpp"
2
+ #include < sw/redis++/redis++.h>
3
+
4
+ using namespace sw ::redis;
5
+
6
+ typedef Redis *RedisPtr;
7
+
8
+ SEQ_FUNC void seq_test_redis () {
9
+ auto password = getenv (" REDIS_PASSWORD" );
10
+
11
+ ConnectionOptions connection_options;
12
+ connection_options.host = " 1.94.26.93" ;
13
+ connection_options.port = 6379 ;
14
+ connection_options.password = password;
15
+ connection_options.db = 0 ;
16
+
17
+ // Optional. Timeout before we successfully send request to or receive
18
+ // response from redis. By default, the timeout is 0ms, i.e. never timeout
19
+ // and block until we send or receive successfuly. NOTE: if any command is
20
+ // timed out, we throw a TimeoutError exception.
21
+ connection_options.socket_timeout = std::chrono::milliseconds (3 * 1000 );
22
+
23
+ // Connect to Redis server with a single connection.
24
+ Redis redis (connection_options);
25
+ // redis.rpush("", "");
26
+ redis.set (" test" , " test123" );
27
+ auto value = redis.get (" test" );
28
+ if (value) {
29
+ printf (" value=%s\n " , value.value ().c_str ());
30
+ } else {
31
+ printf (" value is null\n " );
32
+ }
33
+ }
34
+
35
+ SEQ_FUNC RedisPtr seq_redis_new (const char *host, size_t hostLen, int port,
36
+ const char *password, size_t passwordLen,
37
+ int db, int timeout_ms) {
38
+ auto hostStr = std::string (host, hostLen);
39
+ auto pwdStr = std::string (password, passwordLen);
40
+
41
+ ConnectionOptions connection_options;
42
+ connection_options.host = hostStr;
43
+ connection_options.port = port;
44
+ connection_options.password = pwdStr;
45
+ connection_options.db = db;
46
+
47
+ // Optional. Timeout before we successfully send request to or receive
48
+ // response from redis. By default, the timeout is 0ms, i.e. never timeout
49
+ // and block until we send or receive successfuly. NOTE: if any command is
50
+ // timed out, we throw a TimeoutError exception.
51
+ connection_options.socket_timeout = std::chrono::milliseconds (timeout_ms);
52
+
53
+ return new Redis (connection_options);
54
+ }
55
+
56
+ SEQ_FUNC bool seq_redis_set (RedisPtr redis, const char *key, size_t keyLen,
57
+ const char *value, size_t valueLen) {
58
+ return redis->set (std::string (key, keyLen), std::string (value, valueLen));
59
+ }
60
+
61
+ SEQ_FUNC bool seq_redis_get (RedisPtr redis, const char *key, size_t keyLen,
62
+ char *value, size_t *valueLen) {
63
+ auto result = redis->get (std::string (key, keyLen));
64
+ if (result) {
65
+ *valueLen = result->size ();
66
+ memcpy (value, result->data (), *valueLen);
67
+ return true ;
68
+ }
69
+ return false ;
70
+ }
71
+
72
+ SEQ_FUNC bool seq_redis_expire (RedisPtr redis, const char *key, size_t keyLen,
73
+ int expireTime) {
74
+ return redis->expire (std::string (key, keyLen), expireTime);
75
+ }
76
+
77
+ SEQ_FUNC int64_t seq_redis_rpush (RedisPtr redis, const char *key, size_t keyLen,
78
+ const char *value, size_t valueLen) {
79
+ return redis->rpush (std::string (key, keyLen), std::string (value, valueLen));
80
+ }
81
+
82
+ SEQ_FUNC size_t seq_redis_lrange (RedisPtr redis, const char *key, size_t keyLen,
83
+ char *values[], size_t *valuesLen,
84
+ size_t maxLen) {
85
+ std::vector<OptionalString> result;
86
+ redis->lrange (std::string (key, keyLen), 0 , -1 , std::back_inserter (result));
87
+ size_t count = 0 ;
88
+ for (const auto &val : result) {
89
+ if (count >= maxLen)
90
+ break ;
91
+ if (val) {
92
+ values[count] = new char [val->size () + 1 ];
93
+ memcpy (values[count], val->data (), val->size ());
94
+ values[count][val->size ()] = ' \0 ' ;
95
+ valuesLen[count] = val->size ();
96
+ count++;
97
+ }
98
+ }
99
+ return count;
100
+ }
101
+
102
+ SEQ_FUNC int64_t seq_redis_hset (RedisPtr redis, const char *key, size_t keyLen,
103
+ const char *field, size_t fieldLen,
104
+ const char *value, size_t valueLen) {
105
+ return redis->hset (std::string (key, keyLen), std::string (field, fieldLen),
106
+ std::string (value, valueLen));
107
+ }
108
+
109
+ SEQ_FUNC size_t seq_redis_hgetall (RedisPtr redis, const char *key,
110
+ size_t keyLen, char *fields[],
111
+ size_t *fieldsLen, char *values[],
112
+ size_t *valuesLen, size_t maxLen) {
113
+ std::unordered_map<std::string, std::string> result;
114
+ redis->hgetall (std::string (key, keyLen),
115
+ std::inserter (result, result.begin ()));
116
+ size_t count = 0 ;
117
+ for (const auto &pair : result) {
118
+ if (count >= maxLen)
119
+ break ;
120
+ fields[count] = new char [pair.first .size () + 1 ];
121
+ memcpy (fields[count], pair.first .data (), pair.first .size ());
122
+ fields[count][pair.first .size ()] = ' \0 ' ;
123
+ fieldsLen[count] = pair.first .size ();
124
+
125
+ values[count] = new char [pair.second .size () + 1 ];
126
+ memcpy (values[count], pair.second .data (), pair.second .size ());
127
+ values[count][pair.second .size ()] = ' \0 ' ;
128
+ valuesLen[count] = pair.second .size ();
129
+ count++;
130
+ }
131
+ return count;
132
+ }
133
+
134
+ SEQ_FUNC int64_t seq_redis_sadd (RedisPtr redis, const char *key, size_t keyLen,
135
+ const char *member, size_t memberLen) {
136
+ return redis->sadd (std::string (key, keyLen),
137
+ std::string (member, memberLen));
138
+ }
139
+
140
+ SEQ_FUNC size_t seq_redis_smembers (RedisPtr redis, const char *key,
141
+ size_t keyLen, char *members[],
142
+ size_t *membersLen, size_t maxLen) {
143
+ std::unordered_set<std::string> result;
144
+ redis->smembers (std::string (key, keyLen),
145
+ std::inserter (result, result.begin ()));
146
+ size_t count = 0 ;
147
+ for (const auto &member : result) {
148
+ if (count >= maxLen)
149
+ break ;
150
+ members[count] = new char [member.size () + 1 ];
151
+ memcpy (members[count], member.data (), member.size ());
152
+ members[count][member.size ()] = ' \0 ' ;
153
+ membersLen[count] = member.size ();
154
+ count++;
155
+ }
156
+ return count;
157
+ }
158
+
159
+ SEQ_FUNC int64_t seq_redis_zadd (RedisPtr redis, const char *key, size_t keyLen,
160
+ const char *member, size_t memberLen,
161
+ double score) {
162
+ return redis->zadd (std::string (key, keyLen), std::string (member, memberLen),
163
+ score);
164
+ }
165
+
166
+ SEQ_FUNC void seq_redis_free (RedisPtr redis) { delete redis; }
0 commit comments