C base redis cache access, c base redis Cache

Source: Internet
Author: User

C base redis cache access, c base redis Cache

Introduction

Let's talk about redis installation. The environment used here 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 12 22:03:46 UTC 2016

It is very simple to install redis on ubuntu. The installation code is as follows:

wget http://download.redis.io/releases/redis-3.0.6.tar.gztar xzf redis-3.0.6.tar.gzcd redis-3.0.6make

After installation, my environment is

Test the installation result. Start the redis-server first.

Restart the redis-cli client.

Let's test it.

After the test, everything is normal. redis linux installation is basically complete. For more details, refer

Redis official website tutorials very detailed http://www.redis.net.cn/tutorial/3501.html

 

Preface

Now we have installed the driver for accessing redis c. hiredis. It was downloaded and installed at the beginning. I downloaded and installed it directly from the official website of hiredis git.

Hiredis source https://github.com/redis/hiredis

wget https://github.com/redis/hiredis/archive/master.zipunzip master.zip

After installation, you will see this environment

Run the installation command

makesudo make install

 

Essentially, the following steps are performed for make install.

mkdir -p /usr/local/include/hiredis /usr/local/libcp -a hiredis.h async.h read.h sds.h adapters /usr/local/include/hirediscp -a libhiredis.so /usr/local/lib/libhiredis.so.0.13cd /usr/local/lib && ln -sf libhiredis.so.0.13 libhiredis.socp -a libhiredis.a /usr/local/libmkdir -p /usr/local/lib/pkgconfigcp -a hiredis.pc /usr/local/lib/pkgconfig

At this moment, basically the hiredis driver has been installed. The following describes the APIS provided by the driver.

Common APIs are as follows.

/** The redis connection function returns the redis context. * ip: ip Address * port: Connection port *: returns the redis context. NULL indicates an error occurred while obtaining the link */redisContext * redisConnect (const char * ip, int port) /** execute the redis Operation Command and return the result set * context: redis context object returned by redisConnect * format: equivalent to the printf format controller *...: The variable parameter that follows must correspond to the format character *: returned result set */void * redisCommand (redisContext * context, const char * format ,...); /** release the result set returned by the redis command operation * reply: result set returned by redisCommand */void freeReplyObject (void * reply);/** release link context * context: link context returned by redisConnect */void redisFree (redisContext * context );

For more detailed explanations, see the source code interface file hiredis/hiredis. h. For example:

The first one is the redisContext Context Structure returned by redisConnect/* Context for a connection to Redis */typedef struct redisContext {int err;/* Error flags, 0 when 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 command set returned by redisCommand/* This is the reply object returned by redisCommand () */typedef struct redisReply {int type;/* REDIS_REPLY _ **/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;

The basic C-Drive Interface of hiredis has been explained. The demo test will be written later. The best way to understand it is to check the official source code and test code.

 

Body

First, let's take 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 result is


It indicates that the process is running. Here we can extend it. Sometimes it is difficult to find a function or macro definition declaration in Linux. How can I use it?

 find . -name *.h | xargs grep 'REDIS_REPLY_STRING'

 

The stupid method is also quite practical. The search result is that the extracted part of the REDIS_REPLY_STRING definition in hiredis/read. h is 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

 

Use these macro enumerations to differentiate returned values. In fact, here we are basically getting started with using the redis interface. Next we will refer to the operation code setlist. c for the list operation.

# Include <stdio. h> # include <stdlib. h> # include <signal. h> # include 

 

Compile code

gcc -Wall -ggdb -o setlist.out setlist.c -lhiredis

 

The running result is as follows:

For more details, refer to the source code on hiredis git.

 

Postscript

Here, we have basically finished talking about the simple use of C to control the redis server. errors are inevitable. please correct me.

/*************************************** *******************************

** Learn from the masters rather than their students directly.

** -- Abel

**************************************** *******************************/

 

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.