forked from stanleyhsl/CRedisClient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedisClientHash.cpp
More file actions
226 lines (171 loc) · 4.55 KB
/
RedisClientHash.cpp
File metadata and controls
226 lines (171 loc) · 4.55 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
*
* @file RedisClientHash.cpp
* @brief CRedisClient hash 操作的代码实现。
* @author: yuhaiyang
* @date: 2015年6月14日
*
*/
#include "Command.h"
#include "CRedisClient.h"
//------------------------------hash method-----------------------------------
uint8_t CRedisClient::hset(const std::string &key, const std::string &field, const std::string &value)
{
Command cmd( "HSET" );
cmd << key << field << value;
int64_t num = 0;
_getInt( cmd , num );
return num;
}
bool CRedisClient::hget( const std::string &key, const std::string &field, string &value )
{
Command cmd( "HGET" );
cmd << key << field;
return _getString( cmd , value );
}
uint64_t CRedisClient::hdel( const string &key, const CRedisClient::VecString &fields )
{
Command cmd( "HDEL" );
cmd << key;
VecString::const_iterator it = fields.begin();
VecString::const_iterator end = fields.end();
for ( ; it != end; ++it )
{
cmd << *it;
}
int64_t num = 0;
_getInt( cmd , num );
return num;
}
bool CRedisClient::hexists(const string &key, const string &field)
{
Command cmd( "HEXISTS" );
cmd << key << field;
int64_t num = 0;
_getInt( cmd, num );
return ( num==1 ? true:false );
}
uint64_t CRedisClient::hgetall(const string &key, CRedisClient::MapString &pairs)
{
Command cmd( "HGETALL" );
cmd << key;
_getArry( cmd, pairs );
return pairs.size();
}
uint64_t CRedisClient::hincrby(const string &key, const string &field, uint64_t increment)
{
Command cmd( "HINCRBY" );
cmd << key << field << increment;
int64_t num = 0;
_getInt( cmd, num );
return num;
}
float CRedisClient::hincrbyfloat(const string &key, const string &field, float increment)
{
Command cmd( "HINCRBYFLOAT" );
cmd << key << field << increment;
string value;
_getString( cmd , value );
return _valueFromString<float>( value );
}
uint64_t CRedisClient::hkeys(const string &key, CRedisClient::VecString &values)
{
Command cmd( "HKEYS" );
cmd << key;
_getArry( cmd, values );
return values.size();
}
uint64_t CRedisClient::hlen(const string &key)
{
Command cmd( "HLEN" );
cmd << key;
int64_t num = 0;
_getInt( cmd, num );
return num;
}
void CRedisClient::hmget(const string &key, const CRedisClient::VecString &fields, CResult &result)
{
_socket.clearBuffer();
Command cmd( "HMGET" );
cmd << key;
VecString::const_iterator it = fields.begin();
VecString::const_iterator end = fields.end();
for ( ; it != end; ++it )
{
cmd << *it;
}
_getArry( cmd , result );
}
void CRedisClient::hmset(const string &key, const CRedisClient::MapString &pairs, CResult &result)
{
_socket.clearBuffer();;
Command cmd( "HMSET" );
cmd << key;
MapString::const_iterator it = pairs.begin();
MapString::const_iterator end = pairs.end();
for ( ; it !=end ; ++it )
{
cmd << it->first;
cmd << it->second;
}
_sendCommand( cmd );
_getReply( result );
}
void CRedisClient::hmset(const string &key, const CRedisClient::MapString &pairs)
{
Command cmd( "HMSET" );
cmd << key;
MapString::const_iterator it = pairs.begin();
MapString::const_iterator end = pairs.end();
for ( ; it !=end ; ++it )
{
cmd << it->first;
cmd << it->second;
}
string status;
_getStatus( cmd, status );
}
bool CRedisClient::hsetnx(const string &key, const string &field, const string &value)
{
Command cmd( "HSETNX" );
cmd << key << field << value;
int64_t num = 0;
_getInt( cmd, num );
return ( num==1 ? true:false );
}
uint64_t CRedisClient::hvals(const string &key, CRedisClient::VecString &values)
{
Command cmd( "HVALS" );
cmd << key ;
_getArry( cmd, values );
return values.size();
}
bool CRedisClient::hscan(const string &key, int64_t cursor, MapString &values, const string &match, uint64_t count )
{
static uint64_t lastCur = 0;
uint64_t realCur = 0;
CResult result;
if ( cursor >= 0 )
{
realCur = cursor;
}else
{
realCur = lastCur;
}
Command cmd( "HSCAN" );
cmd << key << realCur;
if ( "" != match )
{
cmd << "MATCH" << match;
}
if ( 0 != count )
{
cmd << "COUNT" << count;
}
_getArry( cmd, result );
CResult::ListCResult::const_iterator it = result.getArry().begin();
lastCur = _valueFromString<uint64_t>( it->getString() );
++it;
_getStringMapFromArry( it->getArry(), values );
return ( lastCur == 0 ? false : true );
}