mirror of
https://github.com/dragonflydb/dragonfly.git
synced 2024-12-14 11:58:02 +00:00
feat(server) implement pexpire command (#464)
Signed-off-by: b0bleet b0bleet@placeq.com
This commit is contained in:
parent
8424f74bec
commit
acafbc6f4f
4 changed files with 43 additions and 1 deletions
|
@ -172,7 +172,7 @@ with respect to Memcached and Redis APIs.
|
|||
- [X] Generic Family
|
||||
- [X] SCAN
|
||||
- [X] PEXPIREAT
|
||||
- [ ] PEXPIRE
|
||||
- [X] PEXPIRE
|
||||
- [x] DUMP
|
||||
- [X] EVAL
|
||||
- [X] EVALSHA
|
||||
|
|
|
@ -778,6 +778,29 @@ void GenericFamily::PexpireAt(CmdArgList args, ConnectionContext* cntx) {
|
|||
}
|
||||
}
|
||||
|
||||
void GenericFamily::Pexpire(CmdArgList args, ConnectionContext* cntx) {
|
||||
string_view key = ArgS(args, 1);
|
||||
string_view msec = ArgS(args, 2);
|
||||
int64_t int_arg;
|
||||
|
||||
if (!absl::SimpleAtoi(msec, &int_arg)) {
|
||||
return (*cntx)->SendError(kInvalidIntErr);
|
||||
}
|
||||
int_arg = std::max(int_arg, 0L);
|
||||
DbSlice::ExpireParams params{.value = int_arg, .unit = TimeUnit::MSEC};
|
||||
|
||||
auto cb = [&](Transaction* t, EngineShard* shard) {
|
||||
return OpExpire(t->GetOpArgs(shard), key, params);
|
||||
};
|
||||
OpStatus status = cntx->transaction->ScheduleSingleHop(std::move(cb));
|
||||
|
||||
if (status == OpStatus::OUT_OF_RANGE) {
|
||||
return (*cntx)->SendError(kExpiryOutOfRange);
|
||||
} else {
|
||||
(*cntx)->SendLong(status == OpStatus::OK);
|
||||
}
|
||||
}
|
||||
|
||||
void GenericFamily::Stick(CmdArgList args, ConnectionContext* cntx) {
|
||||
Transaction* transaction = cntx->transaction;
|
||||
VLOG(1) << "Stick " << ArgS(args, 1);
|
||||
|
@ -1411,6 +1434,7 @@ void GenericFamily::Register(CommandRegistry* registry) {
|
|||
<< CI{"PERSIST", CO::WRITE | CO::FAST, 2, 1, 1, 1}.HFUNC(Persist)
|
||||
<< CI{"KEYS", CO::READONLY, 2, 0, 0, 0}.HFUNC(Keys)
|
||||
<< CI{"PEXPIREAT", CO::WRITE | CO::FAST, 3, 1, 1, 1}.HFUNC(PexpireAt)
|
||||
<< CI{"PEXPIRE", CO::WRITE | CO::FAST, 3, 1, 1, 1}.HFUNC(Pexpire)
|
||||
<< CI{"RENAME", CO::WRITE, 3, 1, 2, 1}.HFUNC(Rename)
|
||||
<< CI{"RENAMENX", CO::WRITE, 3, 1, 2, 1}.HFUNC(RenameNx)
|
||||
<< CI{"SELECT", kSelectOpts, 2, 0, 0, 0}.HFUNC(Select)
|
||||
|
|
|
@ -40,6 +40,7 @@ class GenericFamily {
|
|||
static void Persist(CmdArgList args, ConnectionContext* cntx);
|
||||
static void Keys(CmdArgList args, ConnectionContext* cntx);
|
||||
static void PexpireAt(CmdArgList args, ConnectionContext* cntx);
|
||||
static void Pexpire(CmdArgList args, ConnectionContext* cntx);
|
||||
static void Stick(CmdArgList args, ConnectionContext* cntx);
|
||||
static void Sort(CmdArgList args, ConnectionContext* cntx);
|
||||
static void Move(CmdArgList args, ConnectionContext* cntx);
|
||||
|
|
|
@ -52,6 +52,23 @@ TEST_F(GenericFamilyTest, Expire) {
|
|||
AdvanceTime(1);
|
||||
resp = Run({"get", "key"});
|
||||
EXPECT_THAT(resp, ArgType(RespExpr::NIL));
|
||||
|
||||
// pexpire test
|
||||
Run({"set", "key", "val"});
|
||||
resp = Run({"pexpire", "key", absl::StrCat(2000)});
|
||||
EXPECT_THAT(resp, IntArg(1));
|
||||
|
||||
// expire time override
|
||||
resp = Run({"pexpire", "key", absl::StrCat(3000)});
|
||||
EXPECT_THAT(resp, IntArg(1));
|
||||
|
||||
AdvanceTime(2999);
|
||||
resp = Run({"get", "key"});
|
||||
EXPECT_THAT(resp, "val");
|
||||
|
||||
AdvanceTime(1);
|
||||
resp = Run({"get", "key"});
|
||||
EXPECT_THAT(resp, ArgType(RespExpr::NIL));
|
||||
}
|
||||
|
||||
TEST_F(GenericFamilyTest, Del) {
|
||||
|
|
Loading…
Reference in a new issue