Simple use of Redis database under Linux

Source: Internet
Author: User
Tags memcached git clone redis server

I. Introduction of Redis

Redis is a key-value storage system. Similar to memcached, but solves the situation where the data is completely lost after a power outage, and she supports more untyped value types, in addition to string, supports lists (linked list), sets (collection), and Zsets (ordered collection) data types. These data types support Push/pop, Add/remove, and intersection-set and difference sets, and richer operations, and these operations are atomic.

Second, the installation of Redis

First download the Redis source installation package from the official website: https://github.com/antirez/redis.git

Currently the latest branch is 3.2

1 3.2  HTTPS://github.com/antirez/redis.git

After the download is complete, enter the Redis directory and the compiled installation is directly

Make

Make install

Compile and install to run directly

Redis-server can enable Redis service, you can use the Netstat command to see the Redis listening port (default is 6379)

After Redis service, I can test the database to see if it was installed successfully.

To enable the Redis client at the command line input REDIS-CLI:

Enter the command to test if Redis is installed successfully

127.0. 0.1:6379> pingpong127.0. 0.1:6379set  test Hellook127.0. 0.1:6379get  test"hello"127.0 . 0.1:6379>


For more Redis commands, refer to the Redis command manual, which details the usage of REDIS-CLI commands

http://doc.redisfans.com/

At this point, the Redis database installation is complete

Third, the installation of the Redis C interface Library

Redis is a high-performance Key-value database. The emergence of Redis, to a large extent, compensates for the lack of memcached such key/value storage, in some cases can be a good complement to the relational database. It provides the Java,c/c++,c#,php,javascript,perl,object-c,python,ruby,erlang and other clients, the use is very convenient

Here is a brief introduction to Redis's C interface library:

Hiredis is the C-interface Library of Redis, we need to download the installation before using Hiredis,:https://github.com/redis/hiredis.git

git clone https://github.com/redis/hiredis.git


Enter Hiredis directory after download

Make

Make install

Here are some common functions introduced in the Hredis library

(1) Redisconnect function

This function is used to connect a Redis database

Function Prototypes:

1  Rediscontext *redisconnect (constcharint  port); 2 // Note: This function is used to connect the Redis database with the IP address and port of the database, and the general Redis database has a port of 6379 which returns a struct rediscontext. 

The REDISCONTEXT structure is defined as follows:

1 /*Context for a connection to Redis*/2typedefstructRediscontext {3     intErr/*error flags, 0 when there is no Error*/4     Charerrstr[ -];/*String representation of error when applicable*/5     intFD;6     intflags;7     Char*obuf;/*Write Buffer*/8Redisreader *reader;/*Protocol Reader*/9 Ten     enumRedisconnectiontype Connection_type; One     structTimeval *timeout; A  -     struct { -         Char*host; the         Char*source_addr; -         intPort; - } TCP; -  +     struct { -         Char*path; + } unix_sock; A  at} Rediscontext;

(2) Rediscommand function

This function is used to execute REDIS commands;

Function Prototypes:

1 void Const Char *format, ...); 2 /* Description: The function executes a command, just like an SQL statement in a SQL database, only executes the operations command in the Redis database. 3 The first parameter is the Rediscontext returned when the database is connected, 4 followed by a mutable argument list, similar to the C language printf function, 5 The return value is void*, and the generic cast becomes the redisreply type for further processing. */

Usage:

Redisreply *reply = (redisreply*) rediscommand (c, CMD);

(3) Redisrelpyobject function

The function is also used to release the memory occupied by the Rediscommand return value redisreply

1 /*  */2void freereplyobject (void *reply)3//  This function is used to reclaim the memory occupied by releasing the Rediscommand return value redisreply

(4) Redisfree function

This function is used to release a connection to a Redis database

1 void redisfree (rediscontext *c)2// for releasing Redisconnect-generated connections

Below is a simple Redis test program for Hredis C interface:

#include <stdio.h>#include<string.h>#include<stddef.h>#include<stdarg.h>#include<string.h>#include<assert.h>#include#defineRedis_host "127.0.0.1"#defineRedis_port 6379voidredis_cli () {Rediscontext*c =NULL; Redisreply*r =NULL; C=Redisconnect (Redis_host, Redis_port); if(NULL = =c) {printf ("Connect Redis Server failure\n"); return; } printf ("redis Connect sucess IP:%s Port:%d\n", Redis_host, Redis_port); Char*CMD1 ="Set Test Hello"; R= (redisreply*) Rediscommand (c, CMD1); if(NULL = =r) {printf ("Redis Command Error [%s]\n", CMD1);          Redisfree (c); return; }       if(R->type = =redis_reply_error) {printf ("Redis command[%s], error:%s\n", CMD1, r->str);        Freereplyobject (R);          Redisfree (c); return; } printf ("redis Command Execute success[%s]\n", CMD1);    Freereplyobject (R); Char*CMD2 ="Get Test"; R= (redisreply*) Rediscommand (c, CMD2); if(NULL = =r) {printf ("Redis Command Error [%s]\n", CMD2);          Redisfree (c); return; }       if(R->type = =redis_reply_error) {printf ("Redis command[%s], error:%s\n", CMD2, r->str);        Freereplyobject (R);          Redisfree (c); return; } printf ("Get test value is:%s\n", r->str);    Freereplyobject (R); Const Char* Cmd3 ="strlen Test"; R= (redisreply*) Rediscommand (c, CMD3); if(R->type! =Redis_reply_integer) {printf ("Failed to execute command[%s]\n", CMD3);          Freereplyobject (R);          Redisfree (c); return; } printf ("The length of ' test ' is%d.\n", r->integer);      Freereplyobject (R);      Redisfree (c); return;}intMainintargcChar**argv)        {redis_cli (); return 0;}


Operation Result:

[Email protected] 3rd]#./127.0. 0.1  6379 redis Command Execute success[set  test Hello] Get  is  'test'is5

Simple use of Redis database under Linux

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.