- compile
- use
- initialize
- to connect to a Redis database
Rediscontext * pconn = Red Isconnect (Redisip.c_str (), redisport);
if (m_clocal = = NULL)
{
return 1;
}
if (Pconn->err)
{
cout << "Connection to redis[" << redisip << "] Error:" << pCo nn->errstr;
Redisfree (Pconn);
Return 2;
}
- execute command
String cmd= "Set foo bar;
Redisreply * reply= (redisreply *) Rediscommand (Pconn, Cmd.c_str ());
if (reply = = NULL)
{
cout << "redis[" << redisip << "] excute" << cmd << "fa iled, error is: "<< pconn->errstr;
Redisfree (pconn);
Pconn=null;
Return 3;
}
Freereplyobject (reply);
- Parse results
- different commands return different types of results, which need to be resolved according to the Reply->type field
- Considerations
Once An error is returned the context cannot being reused and you should set up a new connection.
When the command is executed in the example above, if an error is returned, the connection is not available and the connection needs to be re-established.
For more articles, welcome to my Site: 01 streams.
[Go] hiredis:redis C Client Usage note