Skip to content

A simple, high-performance C++ Client for Redis with native support for connection pool and replication environment.(Redis客户端库)

License

Notifications You must be signed in to change notification settings

shpilu/cloRedis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

95 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

中文版

cloRedis

Cloredis is a simple, high-performance C++ Client for Redis with native support for connection pool and master/slave instances access, and its main goal is to provide a more convenient way to access redis in production environment than general C++ client.

Cloredis is not a standalone project, the design of which references, integrates and takes advantage of some leading Redis Clients in C++/Golang language, e.g. hiredis, redigo, redis3m and brpc, and has its own features.

Features

  • Support connection pool and memory pool naturally -- All connection operations are based on connection pool.
  • Automatic memory management and connection reclaim -- You do not need any memory operation, nor need you put the connection back to connection pool when it is no longer required.
  • Support master/slave replication -- You can choose access master or slave instance in redis replication environment.

Cloredis's features make it well adapted for production environment. Now cloredis is used in ofo Inc. and works very well.

Usage at a glance

Basic usage:

//
// The following shows how to get a connection from connection pool, select specific db 
// and run redis command by using redis connection
//
#include <cloredis/cloredis.h>
using namespace cloris;

RedisManager *manager = RedisManager::instance();
if (!manager->Init("172.17.224.212:6379", "cloris520", 100)) {
    std::cout << "init redis manager failed" << std::endl;
    return;
}
{
    // Note! You do not need to put 'conn1' back to connection pool as cloredis will 
    // do it automatically in RedisConnection's destructor function
    RedisConnection conn1 = manager->Get(1); 
    conn1->Do("SET tkey1 %d", 100);
    std::cout << conn1->Do("GET tkey1").toString() << std::endl; // 100
}
...

Advanced usage:

// Specify connection pool options and access master/slave instance
#include <cloredis/cloredis.h>
using namespace cloris;

ConnectionPoolOption option;
option.max_idle = 40;
option.max_active = 100;
option.idle_timeout_ms = 180 * 1000;   
RedisManager *manager = RedisManager::instance();
// '172.17.224.212:6379' is master host, and "172.17.224.212:6380,172.17.224.212:6381" is two slave hosts
if (!manager->InitEx("172.17.224.212:6379", "172.17.224.212:6380,172.17.224.212:6381", "cloris520", 100, &option)) {
    std::cout << "init redis manager failed" << std::endl;
    return;
}

{
    // access redis master instance, you can also write as 'RedisConnection conn1 = manager->Get(5, NULL, MASTER)'
    RedisConnection conn1 = manager->Get(5);
    conn1->Do("NOCOMMAND city %s", "Beijing");
    // use 'ok' or 'error' method to determine whether the command has run success
    // true
    if (conn1->error()) {
        std::cout << "run command error:" << conn1->err_str() << std::endl;
    } else {
        std::cout << "run command ok" << std::endl;
    }
    conn1->Do("set tkey 100");
    sleep(5);
    // access redis slave instance
    conn2 = manager->Get(5, NULL, SLAVE);
    std::cout << conn2->Do("GET tkey").toString() << std::endl;
}

Installation

On Linux system you can build cloredis compile and runtime environment by running the following commands:

# by default, cloredis will install in /usr/local/cloredis
cd src
make
sudo make install

Or you can customize install directory by running command "make install PREFIX=$TARGET_DIR".

The following example shows how to compile with cloredis(assume cloredis is installed in '/usr/local/cloredis' directory)

g++ tutorial.cc -I/usr/local/cloredis/include -L/usr/local/cloredis/lib/ -lcloredis -o main  -std=c++11 -Wl,-rpath=/usr/local/cloredis/lib

Noteworthy is that hiredis has been intergrated into cloredis, so you do not need to install hiredis separately.

API Reference

Cloredis's API document will come soon, and now you can refer to tutorial temporarily.

Who Is Using CloRedis?

  • ofo 小黄车 - ofo Inc., a Beijing-based bicycle sharing company.

Authors

Go back to top

About

A simple, high-performance C++ Client for Redis with native support for connection pool and replication environment.(Redis客户端库)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published