Hiredis is the C interface of redis database. Currently, it can only be used in Linux. Several Basic functions can be used to operate redis database.
Function prototype: rediscontext * redisconnect (const char * IP, int port)
Description: This function is used to connect to the redis database. The parameter is the IP address and port of the database. Generally, the port of the redis database is 6379.
This function returns a structure rediscontext.
Function prototype: void * rediscommand (rediscontext * C, const char * format ,...);
Note: This function executes commands, just like SQL statements in the SQL database, but only COMMANDS IN THE redis database. The first parameter is the rediscontext returned when the database is connected, the remaining parameters are variable parameters, just like the C standard function printf. The return value is void *, which is forcibly converted to the redisreply type for further processing.
Function prototype void freereplyobject (void * reply );
Note: The Memory occupied by the redisreply returned after rediscommand execution is released
Function prototype: void redisfree (rediscontext * C );
Note: releases the connection generated by redisconnect.
The following is a simple example:
[CPP]View plaincopyprint?
- # Include <stdio. h>
- # Include <stdlib. h>
- # Include <stddef. h>
- # Include <stdarg. h>
- # Include <string. h>
- # Include <assert. h>
- # Include
- Void dotest ()
- {
- // The default listening port of redis is 6387, which can be modified in the configuration file.
- Rediscontext * c = redisconnect ("127.0.0.1", 6379 );
- If (c-> ERR)
- {
- Redisfree (C );
- Printf ("connect to redisserver faile \ n ");
- Return;
- }
- Printf ("connect to redisserver success \ n ");
- Const char * command1 = "set stest1 value1 ";
- Redisreply * r = (redisreply *) rediscommand (C, command1 );
- If (null = r)
- {
- Printf ("execut command1 failure \ n ");
- Redisfree (C );
- Return;
- }
- If (! (R-> type = redis_reply_status & strcasecmp (R-> STR, "OK") = 0 ))
- {
- Printf ("failed to execute command [% s] \ n", command1 );
- Freereplyobject (R );
- Redisfree (C );
- Return;
- }
- Freereplyobject (R );
- Printf ("succeed to execute command [% s] \ n", command1 );
- Const char * command2 = "strlen stest1 ";
- R = (redisreply *) rediscommand (C, command2 );
- If (R-> type! = Redis_reply_integer)
- {
- Printf ("failed to execute command [% s] \ n", command2 );
- Freereplyobject (R );
- Redisfree (C );
- Return;
- }
- Int length = r-> integer;
- Freereplyobject (R );
- Printf ("the length of 'stest1' is % d. \ n", length );
- Printf ("succeed to execute command [% s] \ n", command2 );
- Const char * command3 = "Get stest1 ";
- R = (redisreply *) rediscommand (C, command3 );
- If (R-> type! = Redis_reply_string)
- {
- Printf ("failed to execute command [% s] \ n", command3 );
- Freereplyobject (R );
- Redisfree (C );
- Return;
- }
- Printf ("the value of 'stest1' is % s \ n", R-> Str );
- Freereplyobject (R );
- Printf ("succeed to execute command [% s] \ n", command3 );
- Const char * command4 = "Get stest2 ";
- R = (redisreply *) rediscommand (C, command4 );
- If (R-> type! = Redis_reply_nil)
- {
- Printf ("failed to execute command [% s] \ n", command4 );
- Freereplyobject (R );
- Redisfree (C );
- Return;
- }
- Freereplyobject (R );
- Printf ("succeed to execute command [% s] \ n", command4 );
- Redisfree (C );
- }
- Int main ()
- {
- Dotest ();
- Return 0;
- }
The execution result is: