test redis_pipeline

This commit is contained in:
shuxin   zheng 2021-01-09 14:08:40 +08:00
parent 16e51c46ed
commit f4c6ef3dc4
2 changed files with 22 additions and 4 deletions

View File

@ -443,14 +443,16 @@ protected:
protected: protected:
dbuf_pool* dbuf_; dbuf_pool* dbuf_;
// 根据键值计算哈希槽值
void hash_slot(const char* key);
void hash_slot(const char* key, size_t len);
private: private:
void init(void); void init(void);
public: public:
// compute hash slot of the given key and store it in the current
// redis command will be used in the next operation for redis cluster.
void hash_slot(const char* key);
void hash_slot(const char* key, size_t len);
// get the current hash slot stored internal
int get_slot(void) const { int get_slot(void) const {
return slot_; return slot_;
} }

View File

@ -14,6 +14,11 @@ public:
argv_[1] = "test-key"; argv_[1] = "test-key";
lens_[0] = strlen(argv_[0]); lens_[0] = strlen(argv_[0]);
lens_[1] = strlen(argv_[1]); lens_[1] = strlen(argv_[1]);
// computer the hash slot for redis cluster node
cmd_.hash_slot(argv_[1]);
// build redis request command with the given args.
cmd_.build_request(argc_, argv_, lens_); cmd_.build_request(argc_, argv_, lens_);
} }
@ -23,6 +28,12 @@ public:
return msg_; return msg_;
} }
void clear(void) {
// we want to reuse the hash slot in next operation,
// so the parameter save_slot was set to true.
cmd_.clear(true);
}
private: private:
acl::redis cmd_; acl::redis cmd_;
acl::redis_pipeline_message& msg_; acl::redis_pipeline_message& msg_;
@ -48,6 +59,7 @@ public:
protected: protected:
// @override // @override
void* run(void) { void* run(void) {
// parepare for a lot of redis commands in one request
std::vector<redis_command*> commands; std::vector<redis_command*> commands;
for (size_t i = 0; i < (size_t) once_count_; i++) { for (size_t i = 0; i < (size_t) once_count_; i++) {
redis_command* command = new redis_command(pipeline_); redis_command* command = new redis_command(pipeline_);
@ -58,6 +70,7 @@ protected:
request(commands); request(commands);
} }
// free all requests commands
for (std::vector<redis_command*>::iterator it = commands.begin(); for (std::vector<redis_command*>::iterator it = commands.begin();
it != commands.end(); ++it) { it != commands.end(); ++it) {
delete *it; delete *it;
@ -90,6 +103,9 @@ private:
printf("wait result error\r\n"); printf("wait result error\r\n");
break; break;
} }
// clear the temp memroy internal allocated by dbuf
(*it)->clear();
} }
} }