提交 19e6322b 编写于 作者: S songzhao

add set key value [EX seconds] [NX|XX]

上级 04b99482
...@@ -19,35 +19,52 @@ void SetCmd::Do(std::list<std::string> &argv, std::string &ret) { ...@@ -19,35 +19,52 @@ void SetCmd::Do(std::list<std::string> &argv, std::string &ret) {
argv.pop_front(); argv.pop_front();
std::string value = argv.front(); std::string value = argv.front();
argv.pop_front(); argv.pop_front();
if (argv.size() > 0) { bool is_xx = false;
bool is_nx = false;
int64_t sec = 0;
while (argv.size() > 0) {
std::string opt = argv.front(); std::string opt = argv.front();
argv.pop_front(); argv.pop_front();
transform(opt.begin(), opt.end(), opt.begin(), ::tolower); transform(opt.begin(), opt.end(), opt.begin(), ::tolower);
if (opt == "xx") { if (opt == "xx") {
ret = "+OK\r\n"; is_xx = true;
return; } else if (opt == "nx") {
} else if (opt == "nx" && argv.empty()) { is_nx = true;
ret = "+OK\r\n";
} else if (opt == "ex") { } else if (opt == "ex") {
if (argv.size() != 1) { if (argv.size() < 1) {
ret = "-ERR syntax error\r\n"; ret = "-ERR syntax error\r\n";
return; return;
} }
ret = "+OK\r\n"; std::string str_sec = argv.front();
} else if (opt == "px") { argv.pop_front();
if (argv.size() != 1) { if (!string2l(str_sec.data(), str_sec.size(), &sec)) {
ret = "-ERR syntax error\r\n"; ret = "-ERR value is not an integer or out of range\r\n";
return; return;
} }
ret = "+OK\r\n";
} else { } else {
ret = "-ERR syntax error\r\n"; ret = "-ERR syntax error\r\n";
return; return;
} }
} }
nemo::Status s = g_pikaServer->GetHandle()->Set(key, value); nemo::Status s;
if (s.ok()) { int64_t res = 1;
ret = "+OK\r\n"; switch (is_nx) {
case true : s = g_pikaServer->GetHandle()->Setnx(key, value, &res, (int32_t)sec);
break;
case false : switch (is_xx) {
case true : s = g_pikaServer->GetHandle()->Setxx(key, value, &res, sec);
break;
case false : s = g_pikaServer->GetHandle()->Set(key, value, sec);
break;
}
}
if (s.ok() || s.IsNotFound()) {
if (res == 1) {
ret = "+OK\r\n";
} else {
ret = "*-1\r\n";
}
} else { } else {
ret.append("-ERR "); ret.append("-ERR ");
ret.append(s.ToString().c_str()); ret.append(s.ToString().c_str());
......
Subproject commit 5b2023eeaf466292e743bb91ca5bfa6716ad45c4 Subproject commit 87ac5a87e4d8009e34f34a9788c343ba5ee50d7d
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册