Giter Site home page Giter Site logo

zjsxwc / cloredis Goto Github PK

View Code? Open in Web Editor NEW

This project forked from shpilu/cloredis

0.0 0.0 0.0 670 KB

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

License: MIT License

Makefile 2.10% C++ 81.43% C 16.47%

cloredis's Introduction

中文版

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

cloredis's People

Contributors

shpilu avatar virgood avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.