Skip to content

Commit 406995a

Browse files
committedApr 25, 2020
add string api
1 parent 9518b5b commit 406995a

6 files changed

+336
-116
lines changed
 

‎include/cppredis/client.hpp

+89-4
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,73 @@ namespace cpp_redis {
145145
return false;
146146
}
147147

148-
return client_->setex(std::forward<std::string>(key), std::forward<std::string>(value), seconds);
148+
return client_->setex(std::forward<std::string>(key),
149+
std::forward<std::string>(value),unit::int_to_string(seconds));
150+
}
151+
152+
bool psetex(std::string&& key, std::string&& value, size_t milliseconds)
153+
{
154+
static_assert(is_sting_, "This API Support String Request");
155+
156+
if (client_ == nullptr || key.empty()) {
157+
return false;
158+
}
159+
160+
return client_->psetex(std::forward<std::string>(key),
161+
std::forward<std::string>(value),unit::int_to_string(milliseconds));
162+
}
163+
164+
//提高这以下四个接口,主要防止,有些版本不支持setex的接口
165+
bool set_has_seconds(std::string&& key, std::string&& value,std::size_t seconds)
166+
{
167+
static_assert(is_sting_, "This API Support String Request");
168+
169+
if (client_ == nullptr || key.empty()) {
170+
return false;
171+
}
172+
173+
return client_->set_has_seconds(std::forward<std::string>(key),
174+
std::forward<std::string>(value), unit::int_to_string(seconds));
175+
}
176+
177+
//此接口相当于setnx 加上秒,is_exist:true(NX) false(XX)
178+
bool set_has_seconds_if(std::string&& key, std::string&& value, std::size_t seconds,bool is_exist)
179+
{
180+
static_assert(is_sting_, "This API Support String Request");
181+
182+
if (client_ == nullptr || key.empty()) {
183+
return false;
184+
}
185+
186+
return client_->set_has_seconds_if(std::forward<std::string>(key),
187+
std::forward<std::string>(value), unit::int_to_string(seconds),is_exist);
188+
}
189+
190+
bool set_has_milliseconds(std::string&& key, std::string&& value, std::size_t milliseconds)
191+
{
192+
static_assert(is_sting_, "This API Support String Request");
193+
194+
if (client_ == nullptr || key.empty()) {
195+
return false;
196+
}
197+
198+
199+
return client_->set_has_milliseconds(std::forward<std::string>(key),
200+
std::forward<std::string>(value), unit::int_to_string(milliseconds));
201+
}
202+
203+
//is_exist:true(NX)false(XX)
204+
bool set_has_milliseconds_if(std::string&& key, std::string&& value, std::size_t milliseconds,bool is_exist)
205+
{
206+
static_assert(is_sting_, "This API Support String Request");
207+
208+
if (client_ == nullptr || key.empty()) {
209+
return false;
210+
}
211+
212+
213+
return client_->set_has_milliseconds_if(std::forward<std::string>(key),
214+
std::forward<std::string>(value), unit::int_to_string(milliseconds), is_exist);
149215
}
150216

151217
bool set(std::string&& key, std::string&& value)
@@ -158,6 +224,18 @@ namespace cpp_redis {
158224
return client_->set(std::forward<std::string>(key), std::forward<std::string>(value));
159225
}
160226

227+
std::string get_range(std::string&& key,int start,int end)
228+
{
229+
static_assert(is_sting_, "This API Support String Request");
230+
if (client_ == nullptr || key.empty()) {
231+
return "";
232+
}
233+
234+
return client_->get_range(std::forward<std::string>(key),
235+
unit::int_to_string(start),unit::int_to_string(end));
236+
}
237+
238+
161239
std::tuple<bool, int> incr(std::string&& key, int increment = 1)
162240
{
163241
static_assert(is_sting_, "This API Support String Request");
@@ -402,18 +480,25 @@ namespace cpp_redis {
402480
return false;
403481
}
404482

405-
return client_->list_set(std::forward<std::string>(key), std::forward<std::string>(value), index);
483+
return client_->list_set(std::forward<std::string>(key),
484+
std::forward<std::string>(value),unit::int_to_string(index));
406485
}
407486

408-
std::tuple<bool, int> list_del_elem(std::string&& key, std::string&& value, int index = 0)
487+
488+
//没有移除就为0 ,有移除就大于0,count表示list的起始位置,一般从0开始删除,-1表示最后一个元素删除
489+
//如果从0开始删除,有多少删除多少
490+
//如果从-1开始删除,就只会删除一个元素
491+
//数量为>=|count|
492+
std::tuple<bool, int> list_del_elem(std::string&& key, std::string&& value, int count = 0)
409493
{
410494
static_assert(is_list_, "This API Support List Request");
411495

412496
if (client_ == nullptr || key.empty()) {
413497
return { false,-1 };
414498
}
415499

416-
return client_->list_del_elem(std::forward<std::string>(key), std::forward<std::string>(value), index);
500+
return client_->list_del_elem(std::forward<std::string>(key),
501+
std::forward<std::string>(value),unit::int_to_string(count));
417502
}
418503

419504
std::string list_rpoplpush(std::string&& src_key, std::string&& dst_key)

‎include/cppredis/client_interface.hpp

+36-3
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,12 @@ namespace cpp_redis {
168168
return int_results[0];
169169
}
170170

171-
virtual bool setex(std::string&& key, std::string&& value, size_t seconds)
171+
virtual bool setex(std::string&& key, std::string&& value,std::string && seconds)
172+
{
173+
return false;
174+
}
175+
176+
virtual bool psetex(std::string&& key, std::string&& value,std::string && milliseconds)
172177
{
173178
return false;
174179
}
@@ -178,6 +183,34 @@ namespace cpp_redis {
178183
return false;
179184
}
180185

186+
virtual bool set_has_seconds(std::string&& key, std::string&& value,std::string&&seconds)
187+
{
188+
return false;
189+
}
190+
191+
192+
virtual bool set_has_seconds_if(std::string&& key, std::string&& value,
193+
std::string&& seconds,bool is_exist)
194+
{
195+
return false;
196+
}
197+
198+
virtual bool set_has_milliseconds(std::string&& key, std::string&& value,std::string&&milliseconds)
199+
{
200+
return false;
201+
}
202+
203+
virtual bool set_has_milliseconds_if(std::string&& key, std::string&& value,
204+
std::string&& milliseconds,bool is_exist)
205+
{
206+
return false;
207+
}
208+
209+
virtual std::string get_range(std::string&&key, std::string&& start, std::string&& end)
210+
{
211+
return "";
212+
}
213+
181214
virtual std::tuple<bool, int> incr(std::string&& key, int increment = 1)
182215
{
183216
return { false,-1 };
@@ -269,12 +302,12 @@ namespace cpp_redis {
269302
return "";
270303
}
271304

272-
virtual bool list_set(std::string&& key, std::string&& value, int index)
305+
virtual bool list_set(std::string&& key, std::string&& value,std::string && index)
273306
{
274307
return "";
275308
}
276309

277-
virtual std::tuple<bool, int> list_del_elem(std::string&& key, std::string&& value, int index = 0)
310+
virtual std::tuple<bool, int> list_del_elem(std::string&& key, std::string&& value,std::string&&count)
278311
{
279312
return { false,-1 };
280313
}

‎include/cppredis/cpp_define.h

+81-77
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ namespace cpp_redis {
1818
const std::string g_ttl_cmd = "TTL";
1919
/****************字符串操作***********************************/
2020
const std::string g_setx_cmd = "SETEX";
21+
const std::string g_psetx_cmd = "PSETEX";
2122
const std::string g_set_cmd = "SET";
23+
const std::string g_strsub_cmd = "GETRANGE";//2.0之前叫substr
2224
const std::string g_incrby_cmd = "INCRBY";
2325
const std::string g_get_cmd = "GET";
2426
const std::string g_get_set_cmd = "GETSET";
@@ -123,83 +125,85 @@ namespace cpp_redis {
123125
select = 1,
124126
set = 2,
125127
setex = 3,
126-
incrby = 4,
127-
del = 5,
128-
exists = 6,
129-
get = 7,
130-
expire = 8,
131-
expire_at = 9,
132-
ttl = 10,
133-
get_set = 11,
134-
substr = 12,
135-
mget = 13,
136-
mset = 14,
137-
msetnx = 15,
138-
append = 16,
139-
rpush = 17,
140-
lpush = 18,
141-
llen = 19,
142-
lrange = 20,
143-
rpop = 21,
144-
lpop = 22,
145-
brpop = 23,
146-
blpop = 24,
147-
ltrim = 25,
148-
lindex = 26,
149-
lset = 27,
150-
lrem = 28,
151-
rpoplpush = 29,
152-
lpushx = 30,
153-
rpushx = 31,
154-
list_insert = 32,
155-
brpoplpush = 33,
156-
sadd = 34,
157-
srem = 35,
158-
sismember = 36,
159-
spop_elem = 37,
160-
srandmember = 38,
161-
smove = 39,
162-
ssize = 40,
163-
smembers = 41,
164-
sinter = 42,
165-
ssinter_store = 43,
166-
sunion = 44,
167-
ssunion_store = 45,
168-
sdiff = 46,
169-
sdiff_store = 47,
170-
zset_add = 48,
171-
zset_score = 49,
172-
zset_incrby = 50,
173-
zset_card = 51,
174-
zset_count = 52,
175-
zset_range = 53,
176-
zset_rank = 54,
177-
zset_rem = 55,
178-
zset_revrank = 56,
179-
zset_revrange = 57,
180-
zset_lexcount = 58,
181-
zset_rangebylex = 59,
182-
zset_union_store = 60,
183-
zset_inter_store = 61,
184-
zset_range_score = 62,
185-
zset_remrangebylex = 63,
186-
zset_rerange_score = 64,
187-
zset_remrangebyscore = 65,
188-
zset_remrangeby_rank = 66,
189-
hash_set = 67,
190-
hash_setx = 68,
191-
hash_exists = 69,
192-
hash_get = 70,
193-
hash_del = 71,
194-
hash_len = 72,
195-
hash_mset = 73,
196-
hash_mget = 74,
197-
hash_vals = 75,
198-
hash_keys = 76,
199-
hash_strlen = 77,
200-
hash_incrby = 78,
201-
hash_get_all = 79,
202-
hash_incrby_float = 80,
128+
psetex = 4,
129+
strsub = 5,
130+
incrby = 6,
131+
del = 7,
132+
exists = 8,
133+
get = 9,
134+
expire = 10,
135+
expire_at = 11,
136+
ttl = 12,
137+
get_set = 13,
138+
substr = 14,
139+
mget = 15,
140+
mset = 16,
141+
msetnx = 17,
142+
append = 18,
143+
rpush = 19,
144+
lpush = 20,
145+
llen = 21,
146+
lrange = 22,
147+
rpop = 23,
148+
lpop = 24,
149+
brpop = 25,
150+
blpop = 26,
151+
ltrim = 27,
152+
lindex = 28,
153+
lset = 29,
154+
lrem = 30,
155+
rpoplpush = 31,
156+
lpushx = 32,
157+
rpushx = 33,
158+
list_insert = 34,
159+
brpoplpush = 35,
160+
sadd = 36,
161+
srem = 37,
162+
sismember = 38,
163+
spop_elem = 39,
164+
srandmember = 40,
165+
smove = 41,
166+
ssize = 42,
167+
smembers = 43,
168+
sinter = 44,
169+
ssinter_store = 45,
170+
sunion = 46,
171+
ssunion_store = 47,
172+
sdiff = 48,
173+
sdiff_store = 49,
174+
zset_add = 50,
175+
zset_score = 51,
176+
zset_incrby = 52,
177+
zset_card = 53,
178+
zset_count = 54,
179+
zset_range = 55,
180+
zset_rank = 56,
181+
zset_rem = 57,
182+
zset_revrank = 58,
183+
zset_revrange = 59,
184+
zset_lexcount = 60,
185+
zset_rangebylex = 61,
186+
zset_union_store = 62,
187+
zset_inter_store = 63,
188+
zset_range_score = 64,
189+
zset_remrangebylex = 65,
190+
zset_rerange_score = 66,
191+
zset_remrangebyscore = 67,
192+
zset_remrangeby_rank = 68,
193+
hash_set = 69,
194+
hash_setx = 70,
195+
hash_exists = 71,
196+
hash_get = 72,
197+
hash_del = 73,
198+
hash_len = 74,
199+
hash_mset = 75,
200+
hash_mget = 76,
201+
hash_vals = 77,
202+
hash_keys = 78,
203+
hash_strlen = 79,
204+
hash_incrby = 80,
205+
hash_get_all = 81,
206+
hash_incrby_float = 82,
203207
};
204208

205209
enum request_type

‎include/cppredis/cpp_redis_list.hpp

+10-7
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,13 @@ namespace cpp_redis {
254254
}
255255

256256
//指定索引上设置元素值
257-
virtual bool list_set(std::string&& key, std::string&& value, int index)
257+
virtual bool list_set(std::string&& key, std::string&& value,std::string&& index)
258258
{
259259
check_args();
260260

261-
auto pair = request_->make_pair(std::forward<std::string>(key), std::forward<std::string>(value));
262-
std::string msg = request_->req_key_value_has_index(request_->get_cmd(cpp_redis::lset), std::move(pair), std::forward<std::string>(unit::int_to_string(index)));
261+
std::string msg = request_->req_n_key(request_->get_cmd(cpp_redis::lset),std::forward<std::string>(key),
262+
std::forward<std::string>(index),std::forward<std::string>(value));
263+
263264
socket_->send_msg(std::move(msg));
264265
const auto res = socket_->get_responese();
265266

@@ -270,15 +271,17 @@ namespace cpp_redis {
270271
return false;
271272
}
272273

273-
//没有移除就为0 ,有移除就大于0,index表示list的起始位置,一般从0开始删除,-1表示最后一个元素删除
274+
//没有移除就为0 ,有移除就大于0,count表示list的起始位置,一般从0开始删除,-1表示最后一个元素删除
274275
//如果从0开始删除,有多少删除多少
275276
//如果从-1开始删除,就只会删除一个元素
276-
virtual std::tuple<bool, int> list_del_elem(std::string&& key, std::string&& value, int index = 0)
277+
//数量为>=|count|
278+
virtual std::tuple<bool, int> list_del_elem(std::string&& key, std::string&& value,std::string &&count)
277279
{
278280
check_args();
279281

280-
auto pair = request_->make_pair(std::forward<std::string>(key), std::forward<std::string>(value));
281-
std::string msg = request_->req_key_value_has_index(request_->get_cmd(cpp_redis::lrem), std::move(pair), std::forward<std::string>(unit::int_to_string(index)));
282+
std::string msg = request_->req_n_key(request_->get_cmd(cpp_redis::lrem),std::forward<std::string>(key),
283+
std::forward<std::string>(count),std::forward<std::string>(value));
284+
282285
socket_->send_msg(std::move(msg));
283286

284287
const auto res = socket_->get_responese();

0 commit comments

Comments
 (0)
Failed to load comments.