Introduction
First said Redis installation, here the environment is.
Linux version 4.4.0-22-generic (buildd@lgw01-41)
(gcc version 5.3.1 20160413 (Ubuntu 5.3.1-14ubuntu2))
#40- Ubuntu SMP Thu may 22:03:46 UTC 2016
It is very easy to install Redis for Ubuntu. Here the source code installation. The installation code is as follows
wget http://download.redis.io/releases/redis-3.0.6.tar.gz
tar xzf redis-3.0.6.tar.gz
CD redis-3.0.6
Make
After the installation my environment is
Let's test it. Installation results. Start the Redis-server server first.
To start the REDIS-CLI client again
Let's start testing.
Everything is fine after the test. Redis Linux on the installation of basic finished. A more detailed reference
Redis Official Website Tutorial very detailed http://www.redis.net.cn/tutorial/3501.html
Objective
Now we install the Redis C Access driver. Hiredis. At first it was a download installation. I downloaded the installation directly from Hiredis git website.
Hiredis Source Https://github.com/redis/hiredis
wget Https://github.com/redis/hiredis/archive/master.zipunzip Master.zip
The installation will see this environment
Execute installation command
Essentially, the following steps are performed for make install
Mkdir-p/usr/local/include/hiredis/usr/local/lib
cp-a hiredis.h async.h read.h sds.h adapters/usr/local/include/ Hiredis
cp-a libhiredis.so/usr/local/lib/libhiredis.so.0.13
cd/usr/local/lib && ln-sf libhiredis.so.0.13 libhiredis.so
cp-a libhiredis.a/usr/local/lib
mkdir-p
CP- A hiredis.pc/usr/local/lib/pkgconfig
Now basically the Hiredis driver has been installed. Explain later, drive the provided API.
The common APIs are as follows.
/
* Redis link function, return redis context.
* IP : Link address ip
* Port : Link Port
* : Returns REDIS context, NULL indicates get failure
/Rediscontext *redisconnect ( const char *IP, int port)
/* Executes the Redis operation command, returning the resulting result
set * context: The Redis contextual object returned by Redisconnect c15/>* format : equivalent to printf format controller
* ... : Back variable parameters, required and format characters in format
* : Returns the resulting result set
*
/void *rediscommand (Rediscontext *context, const char *format, ...);
* * Release the result set returned by the Redis command operation
* Reply : Rediscommand returned result set
/void Freereplyobject (void *reply) ;
* * Free link context
* contexts : redisconnect return link context
/void Redisfree (Rediscontext *context);
More detailed explanation we can see the source code interface file HIREDIS/HIREDIS.H. For example
The first one is the REDISCONTEXT context structure returned by Redisconnect/a connection to Redis/typedef struct REDISCONTEXT {int err; /* Error flags, 0 when the There is no Error * * Char errstr[128];
/* String representation of error when applicable */int fd;
int flags; Char *obuf; /* Write Buffer * * Redisreader *reader;
* Protocol Reader */enum Redisconnectiontype connection_type;
struct Timeval *timeout;
struct {char *host;
Char *source_addr;
int port;
} TCP;
struct {char *path;
} Unix_sock;
} Rediscontext; Another is the Rediscommand returned command set/* This is the reply object returned by Rediscommand () */typedef struct REDISREPLY {int type ; /* redis_reply_* */long long Integer; /* The integer when type is Redis_reply_integer */int len; /* Length of String */char *str; /* Used for both redis_reply_error and redis_reply_string/size_t elements; /* Number of elements, for Redis_reply_array */struct redisreply **element; /* Elements vector for Redis_reply_ARRAY */} redisreply;
For the Hiredis basic C-drive interface, the explanation is complete. Start writing demo test later. The best way to understand this is to look at the official source code and the test codes.
Body
Let's start with a simple demo test. Simpleget.c
#include <stdio.h>
#include <stdlib.h>
#include
The Compile command is
Gcc-wall-ggdb-o Simpleget.out Simpleget.c-lhiredis
The final test results are
The final test results are
This shows that the flow is running through. Here's an extension, and sometimes finding a function or macro definition on Linux is a hassle. The way I used to be
The stupid method is also very practical. The result of the lookup is the redis_reply_string defined above in the Hiredis/read.h excerpt section as follows
#define REDIS_REPLY_STRING 1
#define Redis_reply_array 2
#define Redis_reply_integer 3
#define Redis_ Reply_nil 4
#define REDIS_REPLY_STATUS 5
#define REDIS_REPLY_ERROR 6
The values returned are distinguished by these macro enumerations. Actually here basically about the Redis interface to use the basic primer. The operation code of the Operation list is setlist.c
#include <stdio.h> #include <stdlib.h> #include <signal.h> #include
Compiling code
Gcc-wall-ggdb-o Setlist.out Setlist.c-lhiredis
The results of the operation are as follows
Please refer to Hiredis git on the source code for more detailed information.
Postscript
Here about C Simple Use control Redis server, basically finished. Mistakes are unavoidable. Please correct me.
Above this article C basic Redis cache access is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.