Database connection pool Dbpool analysis (vii): Redis connection Control Redisobj

Source: Internet
Author: User

Because a large number of references are used in this connection-controlled class, setting a macro in and out is just a sign that the token is output or input.
This class design is very similar to the mysqlobj design, but the difference is that there is no need for an object to store the query value specifically, because Redis is key-value, and the query returns a string or a number directly.
So here we just need a member variable to save the query value, that is redisreply* r_presult;
Redisreply is defined in the Hiredis.h header file.
Another design requirement is that the login password is not added here. Redis is a password that can be set.

The big gap compared to mysqlobj is these functions:

public:     // 从redis当中获取返回值    intlonglong &result);    intstring &result);    intstring &result);    intvector<string> &result);    intvector<redisReply*> &result);

Also because Redis is not a relational database, the Presult type of the return value needs to be checked. The corresponding function is then executed after the check.

#ifndef _redis_obj_h#define _REDIS_OBJ_H#include #include <string>#include <vector>#include "debug.h"using STD::string;using STD::Vector;//Set a flag, the function parameter list with out is the output reference, with in is the input parameter#define out#define inclassredisobj{ Public: Redisobj (stringHostIP,stringPpassword,unsignedIport);Virtual~redisobj ();//dump refers to the value of the M_predis pointer pointing to print out    voidDump ()Const;stringErrorMessage ();//Set up Redis connections based on member variables    BOOLConnect ();voidClose ();intExecutecmd (Const Char* Pcmd);//Returns a pointer to RedisRediscontext* get ()Const; Public://Get return value from Redis    intIntegerresult (outLong Long&result);intStringresult (outstring&result);intStatusresult (outstring&result);intStringarrayresult (out vector<string>&result);intArrayresult (out vector<redisReply*>&result);Private://Get return value from Redis    intIntegerresult (outLong Long&result, in redisreply* reply);intStringresult (outstring&result, in redisreply* reply);intStatusresult (outstring&result, in redisreply* reply);intStringarrayresult (out Vector<std::string>&result, in redisreply* reply);intArrayresult (out vector<redisReply*>&result, in redisreply* reply);Private://Represents a database connectionrediscontext* R_predis; redisreply* R_presult;Private://connection Required Parameters: IP address, user, password, database name, port number    stringR_strhost;stringR_strpassword;unsignedR_iport;//Error Messages    stringR_strerrormessage;};#endif/* Redis_obj_h * /
#include ". /include/redis_obj.h "#include <assert.h>#include <cstring>Redisobj::redisobj (stringHoststringPasswordunsignedPort): R_strhost (Host), R_strpassword (password), R_iport (port) {R_predis = NULL;} Redisobj::~redisobj () {Close ();}voidRedisobj::D UMP ()Const{printf("R_predis=%p", R_predis);}stringRedisobj::errormessage () {return "";}BOOLRedisobj::connect () {R_predis = Redisconnect (R_strhost.c_str (), r_iport);if(R_predis->err) {return false; }return true;}voidRedisobj::close () {if(R_predis)        {Redisfree (R_predis);    R_predis = NULL; }}intRedisobj::executecmd (inConst Char* pcmd) {R_presult = (redisreply*) rediscommand (R_predis, pcmd);if(R_presult = = NULL) {return-1; }return 0;} rediscontext* Redisobj::get ()Const{returnR_predis;}intRedisobj::integerresult (outLong Long&result) {returnIntegerresult (result, r_presult);}intRedisobj::stringresult (outstring&result) {returnStringresult (result, r_presult);}intRedisobj::statusresult (outstring&result) {returnStatusresult (result, r_presult);}intRedisobj::stringarrayresult (out vector<string>&result) {returnStringarrayresult (result, r_presult);}intRedisobj::arrayresult (out vector<redisReply*>&result) {returnArrayresult (result, r_presult);}//Get return value from RedisintRedisobj::integerresult (outLong Long&result, in redisreply* reply) {if(reply = = NULL)return-1;if(Reply->type! = Redis_reply_integer)return-1; result = reply->integer;return 1;}intRedisobj::stringresult (outSTD::string&result, in redisreply* reply) {if(reply = = NULL)return-1;if(Reply->type = = Redis_reply_nil)return 0;if(Reply->type! = redis_reply_string)return-1;    Result.clear (); Result.assign (Reply->str, Reply->len);return 1;}intRedisobj::statusresult (outSTD::string&result, in redisreply* reply) {if(reply = = NULL)return-1;if(Reply->type! = redis_reply_status)return-1;    Result.clear (); Result.assign (Reply->str, Reply->len);return 1;}intRedisobj::stringarrayresult (outSTD:: Vector<std::string>&result, in redisreply* reply) {if(reply = = NULL)return-1;if(Reply->type = = Redis_reply_nil)return 0;if(Reply->type! = Redis_reply_array)return-1; Result.clear (); for(size_t i=0; i<reply->elements; i++) {redisreply* r = reply->element[i];if(R->type = = redis_reply_string)        {Result.emplace_back (r->str, R->len); }    }if(result.size () = =0)return 0;return 1;}intRedisobj::arrayresult (outSTD:: vector<redisReply*>&result, in redisreply* reply) {if(reply = = NULL)return-1;if(Reply->type! = Redis_reply_array)return-1; Result.clear (); for(size_t i=0; i<reply->elements;        i++) {redisreply* r = reply->element[i];    Result.emplace_back (R); }if(result.size () = =0)return 0;return 1;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Database connection pool Dbpool analysis (vii): Redis connection Control Redisobj

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.