acl/app/wizard_demo/pkv/action/redis_string.cpp

116 lines
2.8 KiB
C++
Raw Normal View History

2023-07-31 18:34:16 +08:00
//
// Created by shuxin <20><><EFBFBD><EFBFBD>zheng on 2023/7/31.
//
#include "stdafx.h"
#include "proto/redis_coder.h"
#include "proto/redis_object.h"
2023-08-01 14:16:44 +08:00
#include "dao/db.h"
#include "redis_handler.h"
2023-07-31 18:34:16 +08:00
#include "redis_string.h"
namespace pkv {
2023-08-01 14:16:44 +08:00
redis_string::redis_string(redis_handler& handler, const redis_object &obj)
: handler_(handler), obj_(obj)
{}
2023-07-31 18:34:16 +08:00
bool redis_string::set(redis_coder& result) {
auto& objs = obj_.get_objects();
if (objs.size() < 3) {
logger_error("invalid SET params' size=%zd", objs.size());
return false;
}
auto key = objs[1]->get_str();
if (key == nullptr || *key == 0) {
logger_error("key null");
return false;
}
auto value = objs[2]->get_str();
if (value == nullptr || *value == 0) {
logger_error("value null");
return false;
}
if (!var_cfg_disable_serialize) {
std::string buff;
2023-08-01 14:16:44 +08:00
auto& coder = handler_.get_coder();
coder.clear();
2023-07-31 18:34:16 +08:00
coder.create_object().create_child().set_string("string", true) // type
.create_child().set_number(-1); // expire time
coder.create_object().set_string(value);
coder.to_string(buff);
//printf("buf=[%s]\n", buff.c_str());
if (!var_cfg_disable_save) {
2023-08-01 14:16:44 +08:00
if (!handler_.get_db()->set(key, buff.c_str())) {
2023-07-31 18:34:16 +08:00
logger_error("db set error, key=%s", key);
return false;
}
}
}
result.create_object().set_status("OK");
return true;
}
bool redis_string::get(redis_coder& result) {
auto& objs = obj_.get_objects();
if (objs.size() < 2) {
logger_error("invalid GET params' size=%zd", objs.size());
return false;
}
auto key = objs[1]->get_str();
if (key == nullptr || *key == 0) {
logger_error("key null");
return false;
}
std::string buff;
2023-08-01 14:16:44 +08:00
if (!handler_.get_db()->get(key, buff) || buff.empty()) {
2023-07-31 18:34:16 +08:00
logger_error("db get error, key=%s", key);
return false;
}
//printf(">>>get key=%s, val=[%s]\n", key, buff.c_str());
2023-08-01 14:16:44 +08:00
auto& coder = handler_.get_coder();
coder.clear();
2023-07-31 18:34:16 +08:00
size_t len = buff.size();
2023-08-01 14:16:44 +08:00
(void) coder.update(buff.c_str(), len);
2023-07-31 18:34:16 +08:00
if (len > 0) {
logger_error("invalid buff in db, key=%s", key);
return false;
}
2023-08-01 14:16:44 +08:00
auto& objs2 = coder.get_objects();
2023-07-31 18:34:16 +08:00
if (objs2.size() != 2) {
logger_error("invalid object in db, key=%s, size=%zd", key, objs2.size());
return false;
}
auto o = objs2[1];
if (o->get_type() != REDIS_OBJ_STRING) {
logger_error("invalid object type=%d, key=%s", (int) o->get_type(), key);
return false;
}
auto v = o->get_str();
if (v == nullptr || *v == 0) {
logger_error("value null, key=%s", key);
return false;
}
result.create_object().set_string(v);
return true;
}
} // namespace pkv