Mset Binary Data Interface encapsulation Scheme for C + + Access Redis _c language

Source: Internet
Author: User
Tags redis

Demand

The Hiredis Client interface is used in C + + to access Redis;
Need to use Mset to set multiple binary data at once

The following three kinds of package implementation scheme;

Simple splicing Scheme

In Redis-cli, the syntax of Mset is this:

Copy Code code as follows:

/opt/colin$./redis-cli Mset A-b C 333

Ok

Following this syntax concatenation, the direct use of the Hiredis string interface Rediscommand pass:

void Msetnotbinary (Rediscontext *c, const vector<string> &vtkey, const vector<string> & vtVal) {i
  F (vtkey.size ()!= vtval.size ()) {throw Runtime_error ("Redis error");
  } string strcmd = "Mset";
  for (int i = 0; i < vtkey.size (); i++) {strcmd = "" +vtkey[i]+ "" +vtval[i];

  } cout << "Strcmd:" << strcmd << Endl;
  void * r = Rediscommand (c, Strcmd.c_str ());
  if (!r) throw Runtime_error ("Redis error");
Freereplyobject (R);
  } void Do_test (Rediscontext *c) {vector<string> vtkey;

  Vector<string> Vtval;
  Vtkey.push_back ("A");
  Vtval.push_back ("AAAA");
  Vtkey.push_back ("B");
  Vtval.push_back ("bbbb");
  Vtkey.push_back ("C");
  Vtval.push_back ("CCCC");
  Add a binary data vtkey.push_back ("D");
  Vtval.push_back ("");
  Char a[] = "ABCDE";
  A[2] = 0;

  Vtval[3].assign (a,5);
    try {msetnotbinary (c, Vtkey, vtval);
    Mset1 (c, Vtkey, vtval);
  Mset2 (c, Vtkey, vtval);catch (Runtime_error &) {cout << "error" << Endl;

  int main (int argc, char *argv[]) {Rediscontext *c;
  c = Redisconnect ("127.0.0.1", 6379);
    if (c->err) {cout << "Connection error:" << c->errstr << Endl;
  return-1;

  } do_test (c);

  Redisfree (c);
return 0;

 }

This method can handle mset multiple string data, but the data content for binary data is powerless;

REDISCOMMANDARGV Interface Transfer Scheme

For multiple parameter passes, the HIREDIS provides the following interface in which the last parameter is the length of the content of all incoming data,
This means that the interface is binary safe:

void *rediscommandargv (Rediscontext *c, int argc, const char **ARGV, const size_t *argvlen);
The main task is to construct a dynamic two-dimensional array char * * argv, which involves the conversion of char * to the const char * *, with certain risks,
The previous article on this point has been talked about;

void Mset1 (Rediscontext *c, const vector<string> &vtkey, const vector<string> & vtval) {if (Vtkey
  . Size ()!= vtval.size ()) {throw Runtime_error ("Redis error");
  char * * argv = new Char*[vtkey.size () + vtval.size () + 1];

  size_t * Argvlen = new Size_t[vtkey.size () + vtval.size () + 1];
  int j = 0;
  ARGV[J] = new CHAR[5];
  memcpy (Argv[j], "Mset", 4);
  ARGVLEN[J] = 4;


  ++j;
    for (int i = 0; i < vtkey.size (); i++) {Argvlen[j] = Vtkey[i].length ();
     ARGV[J] = new Char[argvlen[j]];
    memset ((void*) argv[j],0,argvlen[j]);
    memcpy ((void*) Argv[j],vtkey[i].data (), vtkey[i].length ());

    j + +;
    ARGVLEN[J] = Vtval[i].length ();
    ARGV[J] = new Char[argvlen[j]];
    memset ((void*) argv[j],0,argvlen[j]);
    memcpy ((void*) Argv[j],vtval[i].data (), vtval[i].length ());
  j + +; //if not with Const_cast<const char**>, compile error//for why assign to const char** error, My blog ... void *r = RediscommaNDARGV (c, vtkey.size () + vtval.size () + 1, const_cast<const char**> (argv), Argvlen);
  if (!r) throw Runtime_error ("Redis error");

  Freereplyobject (R);
    for (int i = 0;i < Vtkey.size (); i++) {delete [] argv[i];
  Argv[i] = NULL;
  } Delete []argv;
  delete []argvlen;
argv = NULL;

 }

Vector Scheme for REDISCOMMANDARGV interface transfer

or using the REDISCOMMANDARGV interface, the vector is used to construct this const char * *, a method that is learned from reference 1:

void Mset2 (Rediscontext *c, const vector<string> &vtkey, const vector<string> & vtval) {if (Vtkey).
  Size ()!= vtval.size ()) {throw Runtime_error ("Redis error");
  } vector<const char *> argv (vtkey.size () + vtval.size () + 1);
  Vector<size_t> Argvlen (vtkey.size () + vtval.size () + 1);

  int j = 0;
  static char msetcmd[] = "Mset";
  ARGV[J] = Msetcmd;
  ARGVLEN[J] = sizeof (msetcmd)-1;

  ++j;
    for (int i = 0;i< vtkey.size (); ++i) {Argvlen[j] = Vtkey[i].length ();
     ARGV[J] = new Char[argvlen[j]];
    memset ((void*) argv[j],0,argvlen[j]);
    memcpy ((void*) Argv[j],vtkey[i].data (), vtkey[i].length ());

    j + +;
    ARGVLEN[J] = Vtval[i].length ();
    ARGV[J] = new Char[argvlen[j]];
    memset ((void*) argv[j],0,argvlen[j]);
    memcpy ((void*) Argv[j],vtval[i].data (), vtval[i].length ());
  j + +;
  } void *r = REDISCOMMANDARGV (c, Argv.size (), & (Argv[0)), & (Argvlen[0)); if (!r) throw Runtime_error ("Redis error");
  Freereplyobject (R);

 }

In this way, the transfer of binary data is realized;

Binary checksum

After the program executes, you can use REDIS-CLI to verify:

For non-binary security implementations, binary content is truncated:

Copy Code code as follows:

/OPT/APP/COLIN$./REDIS-CLI Get D
"AB"

and binary security Implementation interface, binary data 0 by escape mode display:

Copy Code code as follows:

/OPT/APP/COLIN$./REDIS-CLI Get D
"AB\X00DE"

Complete executable code as detailed in Github:https://github.com/me115/cppset/tree/master/2dimarray

The above mentioned is the entire content of this article, I hope you can enjoy.

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.