forked from 0xsky/xredis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxRedisClient_keys.cpp
162 lines (132 loc) · 4.5 KB
/
xRedisClient_keys.cpp
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
/*
* ----------------------------------------------------------------------------
* Copyright (c) 2013-2014, xSky <guozhw at gmail dot com>
* All rights reserved.
* Distributed under GPL license.
* ----------------------------------------------------------------------------
*/
#include "xRedisClient.h"
#include <sstream>
bool xRedisClient::del(const RedisDBIdx& dbi, const string& key) {
if (0==key.length()) {
return false;
}
return command_bool(dbi, "DEL %s", key.c_str());
}
bool xRedisClient::del(const DBIArray& vdbi, const KEYS & vkey, int64_t& count) {
count = 0;
if (vdbi.size()!=vkey.size()) {
return false;
}
DBIArray::const_iterator iter_dbi = vdbi.begin();
KEYS::const_iterator iter_key = vkey.begin();
for(;iter_key!=vkey.end();++iter_key, ++iter_dbi) {
const RedisDBIdx &dbi = (*iter_dbi);
const string &key = (*iter_key);
if (del(dbi, key)) {
count++;
}
}
return true;
}
bool xRedisClient::exists(const RedisDBIdx& dbi, const string& key) {
if (0==key.length()) {
return false;
}
return command_bool(dbi, "EXISTS %s", key.c_str());
}
bool xRedisClient::expire(const RedisDBIdx& dbi, const string& key, const unsigned int second) {
if (0==key.length()) {
return false;
}
int64_t ret = -1;
if (!command_integer(dbi, ret, "EXPIRE %s %u", key.c_str(), second)) {
return false;
}
if (1==ret) {
return true;
} else {
SetErrMessage(dbi, "expire return %ld ", ret);
return false;
}
}
bool xRedisClient::expireat(const RedisDBIdx& dbi, const string& key, const unsigned int timestamp) {
if (0==key.length()) {
return false;
}
return command_bool(dbi, "EXPIREAT %s %u", key.c_str(), timestamp);
}
bool xRedisClient::persist(const RedisDBIdx& dbi, const string& key) {
if (0==key.length()) {
return false;
}
return command_bool(dbi, "PERSIST %s %u", key.c_str());
}
bool xRedisClient::pexpire(const RedisDBIdx& dbi, const string& key, const unsigned int milliseconds) {
if (0==key.length()) {
return false;
}
return command_bool(dbi, "PEXPIRE %s %u", key.c_str(), milliseconds);
}
bool xRedisClient::pexpireat(const RedisDBIdx& dbi, const string& key, const unsigned int millisecondstimestamp) {
if (0==key.length()) {
return false;
}
return command_bool(dbi, "PEXPIREAT %s %u", key.c_str(), millisecondstimestamp);
}
bool xRedisClient::pttl(const RedisDBIdx& dbi, const string& key, int64_t &milliseconds) {
if (0==key.length()) {
return false;
}
return command_integer(dbi, milliseconds, "PTTL %s", key.c_str());
}
bool xRedisClient::ttl(const RedisDBIdx& dbi, const string& key, int64_t &seconds) {
if (0==key.length()) {
return false;
}
return command_integer(dbi, seconds, "TTL %s", key.c_str());
}
bool xRedisClient::type(const RedisDBIdx& dbi, const string& key, string& value) {
if (0 == key.length()) {
return false;
}
return command_string(dbi, value, "TYPE %s", key.c_str());
}
bool xRedisClient::randomkey(const RedisDBIdx& dbi, KEY& key){
return command_string(dbi, key, "RANDOMKEY");
}
bool xRedisClient::sort(const RedisDBIdx& dbi, ArrayReply& array, const string& key, const char* by,
LIMIT *limit /*= NULL*/, bool alpha /*= false*/, const FILEDS* get /*= NULL*/,
const SORTODER order /*= ASC*/, const char* destination )
{
static const char *sort_order[3] = { "ASC", "DESC" };
if (0 == key.length()) {
return false;
}
VDATA vCmdData;
vCmdData.push_back("sort");
vCmdData.push_back(key);
if (NULL != by) {
vCmdData.push_back("by");
vCmdData.push_back(by);
}
if (NULL != limit) {
vCmdData.push_back("LIMIT");
vCmdData.push_back(toString(limit->offset));
vCmdData.push_back(toString(limit->count));
}
if (alpha) {
vCmdData.push_back("ALPHA");
}
if (NULL != get) {
for (FILEDS::const_iterator iter = get->begin(); iter != get->end(); ++iter) {
vCmdData.push_back("get");
vCmdData.push_back(*iter);
}
}
vCmdData.push_back(sort_order[order]);
if (destination) {
vCmdData.push_back(destination);
}
return commandargv_array(dbi, vCmdData, array);
}