Linux installation Redis

Source: Internet
Author: User

Today, Mayuyu to learn how to operate a Redis database in C + +. Implemented through the Hiredis.h interface, currently available only in Linux environments.

The hiredis.h is: https://github.com/redis/hiredis

The following four methods are mainly included

1. rediscontext* redisconnect (const char *IP, int port)

This function is used to connect to the Redis database, two parameters are the IP and port of the Redis database, the port number is generally 6379. Similar

also provides a function for the connection time-out limit, which is

rediscontext* redisconnectwithtimeout (const char *IP, int port, Timeval TV).

2. void *rediscommand (Rediscontext *c, const char *format ...)

This function is used to execute commands in the Redis database, the first parameter is the Rediscontext returned by the connected database, and the remaining parameters

As a prinf () function in the C language.

The return value of this function is void*, but is generally cast to the redisreply type for further processing.

3. void Freereplyobject (void *reply)

Releases the memory occupied by the Redisreply returned after Rediscommand execution.

4. void Redisfree (Rediscontext *c)

Releases the connection generated by the Redisconnect ().

Next is to let Mayuyu to teach you how to install Hiredis it!

First download the hiredis.tar.gz package on the website, extract found inside there is a makefile file, and then perform make to compile, get

Next put the libhiredis.so into the/usr/local/lib/, put the hiredis.h into the/usr/local/inlcude/hiredis/.

Or use the command make install configuration directly. Such as

The next step is to use it directly in the program. Include # include

In order to operate conveniently, generally we need to write a header file class, the method of this class according to their own project needs to be added appropriately. As follows

Code: REDIS.H
#ifndef _redis_h_#define _redis_h_#include <iostream>#include <string.h>#include <string>#include <stdio.h>#include {This->_connect = NULL; this->_reply = NULL;}boolConnect (std::string host,int port){This->_Connect = Redisconnect (Host.c_str (), port);  if (this->_Connect! = NULL && this->_CONNECT-&GT;ERR) {  printf"Connect Error:%s\n ", this->_CONNECT-&GT;ERRSTR);   Return0; }  Return1;} std::string get (std::string key){ This->_reply = (redisreply*) Rediscommand (this->_ConnectThe GET%s ", Key.c_str ());   std::string str = this->_reply->str;  Freereplyobject (this->_reply);   return str;}  void set (std::string key, std::string value)  { Rediscommand (this->_connect,  "SET %s %s ", Key.c_str (), Value.c_str ()); }private:rediscontext* _connect; redisreply* _ Reply    }; #endif//_redis_h_              
Redis.cpp
#include "Redis.h"int main () {Redis*r = new Redis (); if (!r->connect ( "192.168.13.128", 6379))  {  printf ( Span class= "string" > "connect error!\n");   return 0;}  R->set ( "name", printf ( "Get the name is %s\n", R->get ( "name"). C_STR ());  delete r; return 0;}           

Makefile file

Redis:redis.cpp redis.h  g++ redis.cpp-o redis-l/usr/local/lib/-lhiredisclean:  rm redis.o redis< /c4> 

Note that before g++ and RM are all a TAB key.

You need to add parameters at compile time, assuming the file is redis.cpp, then compile the command as follows

g++ Redis.cpp-o redis-l/usr/local/lib/-lhiredis

If the dynamic library fails to load at the time of execution, the following configuration is required

New file usr-libs.conf in /etc/ld.so.conf.d/ directory, content is:/usr/local/lib

As shown

Then use the command /sbin/ldconfig to update the configuration.

Linux installation Redis

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.