A C++ Redis client

Overview

redis3m

Build Status Gitter

A C++ Redis client, born to bring my experience using Redis and C++ on a opensource library.

Main goals

  1. Provide a simple and efficient wrapper of hiredis, with C++ facilities like memory management
  2. A connection pooling system, with support for high availability using sentinel
  3. A set of useful patterns ready to use and composable with other code. For example scheduler, orm, counters or message queueing

Dependencies

redis3m requires hiredis and boost libraries.

Install

First step install all required dependencies, on a Debian system you can use:

sudo apt-get install libmsgpack-dev libboost-thread-dev libboost-date-time-dev libboost-test-dev libboost-filesystem-dev libboost-system-dev libhiredis-dev cmake build-essential libboost-regex-dev

Then checkout the code and compile it

git clone https://github.com/luca3m/redis3m
cd redis3m
cmake
make
sudo make install

Documentation

See examples directory for some examples, you can compile them with:

g++ <example.cpp> $(pkg-config --cflags --libs redis3m) -o <example.bin>

You can find all classes reference here

Versioning

This project uses semantic versioning. In short words versions are named X.Y[.Z]. Changing X means break API changes, Y means new features without breaking old code, Z means bug fixing.

Comments
  • Why not establish multiple connections with Master/Slave?

    Why not establish multiple connections with Master/Slave?

    In APIs create_slave_connection() and create_master_connection(), they will return directly after establishing only one connection, so we have at most 2 connections available any time.

    Why not make the connection to all Masters & Slaves, then redis3m users have more connections available, which might be better for load sharing.

    opened by xiaotaoz 8
  • Are there plans to support the AUTH command?

    Are there plans to support the AUTH command?

    We're using a redis installation that requires authentication but it seems there is no support for authentication in the current version. Is this planned?

    opened by rbraud 7
  • Support building statically linked library

    Support building statically linked library

    Hi, I was trying to get cmake to build a .a instead of a .so but I could not seem to be able to do it. However, it seems if you change the add_library call in CMakeLists.txt to:

    add_library(${PROJECT_NAME} ${SRC_FILES} ${CMAKE_CURRENT_BINARY_DIR}/datadir.cpp ${INCLUDE_FILES})

    (ie, remove SHARED), then the default behavior will still be to generate a shared library. But if you want a static library, you can run cmake -DBUILD_SHARED_LIBS:BOOL=OFF and then it will build a static library when running "make." Do you think this change is acceptable?

    opened by rbraud 3
  • How to implement lock using redis3m.

    How to implement lock using redis3m.

    Lets say my use case is, There are 2 processes trying to acquire lock on some key. One of them succeed, while other will just wait. Once first one is done updating the value it should release lock and then 2nd process can acquire lock if want or use the value updated by 1st one. I am talking about similar methods from StackExchange.Redis for c#

    1. LockTake
    2. LockRelease
    3. LockExtend
    4. LockQuery
    question 
    opened by nidhijha 3
  • update for c++11+ visual studio build

    update for c++11+ visual studio build

    hi, I added Visual studio 2013 solution and also updated the code to c++11 where I could.

    I have defined a NO_BOOST preprocessor macro (most of the functionality except boost::format seems to be in c++11 and for various reasons I can't use boost in my code). This does litter the code a bit, so feel free to make suggestions on how to resolve those.

    My preference would be to remove boost completely where it can be removed. The one big area I didn't touch is scheduler.cpp/.h where I believe std::chrono could be used instead of boost..

    thanks.

    opened by gaz-amoldeshpande 3
  • Can sentinels have different ports?

    Can sentinels have different ports?

    I'm looking at the connection_pool::create() function that receives a list of sentinel hostnames separated by commas. But then it seems that the port number has to be the same for all sentinels. Is there a way to specify different port numbers for each sentinel?

    Thank you!

    opened by GrossoMoreira 2
  • How to unsubscribe?

    How to unsubscribe?

    Hi,

    The subscriber.cpp example shows how to make an infinite loop that receives messages, but how do I break out of that loop?

    I'm running the subcriber in a separate thread, with a connection that is not being used for anything else. Whenever the get_reply() function returns, it always returns 3 elements: {"message", channelname, content }. But how do I make it stop?

    I tried the following in the main thread: conn->run(command("UNSUBSCRIBE") << channelname);

    That is, using the same connection that is being used by the subscriber thread. After this line of code is executed, the get_reply() function in the subscriber thread returns this: { "unsubscribe", channelname, "" }.

    So, I found a way for the subcriber thread to unblock and know when to break out of the loop and return: it just needs to check if the first element of the reply is "message" or "unsubscribe".

    However, now the call to the unsubscribe command in the main thread blocks forever! How am I supposed to do this then?

    Thank you very much!

    opened by GrossoMoreira 2
  • exception safe locks/unlocks

    exception safe locks/unlocks

    I just wonder, why sometimes is explicit locking on a mutex object is used instead of a unique_lock with explicit unlock call.

    In the file: redis3m/src/connection_pool.cpp or redis3m/src/simple_pool.cpp I code like that:

    connection::ptr_t simple_pool::get()
    {
        connection::ptr_t ret;
    
        access_mutex.lock();
        std::set<connection::ptr_t>::iterator it = connections.begin();
        if (it != connections.end())
        {
            ret = *it;
            connections.erase(it);
        }
    
        access_mutex.unlock();
    
        if (!ret)
        {
            ret = connection::create(_host, _port);
            // Setup connections selecting db
            if (_database != 0)
            {
                ret->run(command("SELECT") << _database);
            }
        }
        return ret;
    }
    

    Why not writing it like:

    connection::ptr_t simple_pool::get()
    {
        connection::ptr_t ret;
    
        std::unique_lock<std::mutex> lock(access_mutex);
        std::set<connection::ptr_t>::iterator it = connections.begin();
        if (it != connections.end())
        {
            ret = *it;
            connections.erase(it);
        }
    
        lock.unlock();
    
        if (!ret)
        {
            ret = connection::create(_host, _port);
            // Setup connections selecting db
            if (_database != 0)
            {
                ret->run(command("SELECT") << _database);
            }
        }
        return ret;
    }
    

    In fact unique_lock is used 30 lines below...

    I understand that set::erase(iterator) version has the nothrow guarantee, but still I think consistency would be better. And there is no performance drawback in that case.

    opened by ovanes 2
  • Binary Strings (having a \0) not propery retrieved

    Binary Strings (having a \0) not propery retrieved

    Hi,

    When I store a binary string (in redis-cli this looks like "\r\x04\x00\x04\x05") it will not be properly retrieved. The reply will be cut off and look like this. {_type=STRING (1) _str="\r\x4" _integer=0 ...}

    Is there any workaround or pending fix? Otherwise I have to use another library :(

    Regards

    opened by egbertn 1
  • SET value with

    SET value with "-" is not able to go through

    Hey, I was trying to set a key value having "-" in it. But it doesn't work with reidis3m library.

    Below command doesn't set a key-value pair in redis. However I am able to set this value using Redis-client. connection->run(command("SET")<<"mykey"<<"myvalue-1");

    opened by nidhijha 1
  • Error during make can't find hiredis.h

    Error during make can't find hiredis.h

    Hello,

    When I'm attempting to build the package, the cmake works perfectly fine. When I run make however, it gets to about 17% then starts erroring hiredis/hiredis.h no such file or directory.

    I've included all output from the git clone to the make in the following pastebin.

    Thanks!! http://pastebin.com/XfxkdwW0

    PS. I handbuilt this on a different system (worked fine) and it caused a DRASTIC reduction in wait times & greatly improved performance. So great job & thanks again.

    opened by whynotkeithberg 1
  • CMake - Boost flag

    CMake - Boost flag

    This is a great project and very solid. Thank you. Also, I have a query...

    I build the VS solution and have a problem with the makefile examples project. The lib builds fine.

    Also when I try and build with CMAKE I am struggling to disable BOOST using a flag. Seems strange.

    Best wishes

    opened by lefig 2
  • Cannot install dependencies on Centos

    Cannot install dependencies on Centos

    sudo apt-get install libmsgpack-dev libboost-thread-dev libboost-date-time-dev libboost-test-dev libboost-filesystem-dev libboost-system-dev libhiredis-dev cmake build-essential

    Unable to install these dependencies on redis3m on Centos 7. Is there a way to get around with this?

    opened by NidaTariq45 1
  • Comparison  function is error for struct host_t

    Comparison function is error for struct host_t

    Below code in cluster_pool.h is to define less operator for host_t. The class host_t is used as key in a map.
    I think the implemetion has a error. Below two redis host are different address, they should be as different values and can be inserted into a map. But the current implemention will treat them as the same value ,since the port are the same, the less function will return false. Please have a look at it. redis1 : address: 192.168.10.10, port: 6379 redis2 : address: 192.168.10.11, prot: 6379

    inline bool operator < (const host_t& rvalue) const
    {
          return (address <= rvalue.address) && ( port < rvalue.port);
    }
    
    opened by MatthewButterfly 1
  • CMake issue when compiling redis3m as submodule (use CMake add_subdirectory)

    CMake issue when compiling redis3m as submodule (use CMake add_subdirectory)

    I use redis3m as git submodule in my project:

    $ tree -L 1 ch1/
    ch1/
    ├── build
    ├── CMakeCache.txt
    ├── CMakeFiles
    ├── cmake_install.cmake
    ├── CMakeLists.txt
    ├── gtest/
    ├── main.cc
    ├── Makefile
    ├── redis3m/
    └── test
    

    I add CMake add_subdirectory to compile redis3m:

    add_subdirectory(redis3m)
    

    Then I got the following error message:

    [ 41%] Built target redis3m
    make[2]: *** No rule to make target `Doxyfile', needed by `redis3m/docs'.  Stop.
    make[1]: *** [redis3m/CMakeFiles/apidoc.dir/all] Error 2
    make: *** [all] Error 2
    

    I fix the error by changing CMAKE_SOURCE_DIR to CMAKE_CURRENT_SOURCE_DIR:

     FIND_PACKAGE(Doxygen QUIET)
     IF (DOXYGEN_FOUND)
    -        SET(DOXYGEN_INPUT "${CMAKE_SOURCE_DIR}/Doxyfile")
    +      SET(DOXYGEN_INPUT "${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile")
       SET(DOXYGEN_OUTPUT "docs")
    

    I think CMAKE_CURRENT_SOURCE_DIR better than CMAKE_SOURCE_DIR when compiling redis3m in a project with multiple dependencies (When compiling redis3m alone, the two variable is same).

    opened by zyy17 0
  • Can reply object be resused ?

    Can reply object be resused ?

    I am running multiple commands and reusing reply object sometimes i find abnormal behavior e.g.

    command t = command("SET") << "foor" << "bar" ; reply myreply = conn->run(t);

    t = command("GET") << "foor"; myreply = conn->run(t);

    opened by aidevr 2
Owner
Luca Marturana
Software engineer, photographer, swimmer, runner and chef (more or less)
Luca Marturana
Minimalistic C client for Redis >= 1.2

This Readme reflects the latest changed in the master branch. See v1.0.0 for the Readme and documentation for the latest release (API/ABI history). HI

Redis 5.5k Jan 5, 2023
Modern, asynchronous, and wicked fast C++11 client for Redis

redox Modern, asynchronous, and wicked fast C++11 client for Redis [] (https://travis-ci.org/hmartiro/redox) Redox is a C++ interface to the Redis key

Hayk Martiros 380 Jan 7, 2023
C++11 Lightweight Redis client: async, thread-safe, no dependency, pipelining, multi-platform

C++11 Lightweight Redis client: async, thread-safe, no dependency, pipelining, multi-platform

Simon Ninon 1k Jan 8, 2023
Redis client written in C++

redis-plus-plus Overview Features Branches Installation Install hiredis Install redis-plus-plus Run Tests (Optional) Use redis-plus-plus In Your Proje

null 1k Dec 27, 2022
cpp_redis is a C++11 Asynchronous Multi-Platform Lightweight Redis Client

C++11 Lightweight Redis client: async, thread-safe, no dependency, pipelining, multi-platform

CPP Redis 561 Jan 3, 2023
Aredis - a clean redis C++ client

aredis a clean redis C++ client redis_conn rc; if (rc.connect("127.0.0.1"/*host*/, 6379/*port*/, nullptr/*password*/, 1/*db*/)) { redis_command cmd;

Evan 26 Dec 10, 2021
A C++ Redis client

redis3m A C++ Redis client, born to bring my experience using Redis and C++ on a opensource library. Main goals Provide a simple and efficient wrapper

Luca Marturana 183 Dec 16, 2022
The official C++ client API for PostgreSQL.

libpqxx Welcome to libpqxx, the C++ API to the PostgreSQL database management system. Home page: http://pqxx.org/development/libpqxx/ Find libpqxx on

Jeroen Vermeulen 718 Jan 3, 2023
Beryl-cli is a client for the BerylDB database server

Beryl-cli is a client for the BerylDB database server. It offers multiple commands and is designed to be fast and user-friendly.

BerylDB 11 Oct 9, 2022
The PostgreSQL client API in modern C++

C++ client API to PostgreSQL {#mainpage} Dmitigr Pgfe (PostGres FrontEnd, hereinafter referred to as Pgfe) - is a C++ client API to PostgreSQL servers

Dmitry Igrishin 137 Dec 14, 2022
C++ client library for PostgreSQL

Welcome to taoPQ taoPQ is a lightweight C++ client library for accessing a PostgreSQL➚ database. It has no dependencies beyond libpq➚, the C applicati

The Art of C++ 232 Dec 22, 2022
Trilogy is a client library for MySQL-compatible database servers, designed for performance, flexibility, and ease of embedding.

Trilogy is a client library for MySQL-compatible database servers, designed for performance, flexibility, and ease of embedding.

GitHub 482 Dec 31, 2022
redis-cpp is a header-only library in C++17 for Redis (and C++11 backport)

redis-cpp - lightweight C++ client library for Redis redis-cpp is a C++17 library for executing Redis commands with support for pipelines and the publ

null 77 Dec 11, 2022
A redis module, similar to redis zset, but you can set multiple scores for each member to support multi-dimensional sorting

TairZset: Support multi-score sorting zset Introduction Chinese TairZset is a data structure developed based on the redis module. Compared with the na

Alibaba 60 Dec 1, 2022
Minimalistic C client for Redis >= 1.2

This Readme reflects the latest changed in the master branch. See v1.0.0 for the Readme and documentation for the latest release (API/ABI history). HI

Redis 5.5k Jan 5, 2023
Modern, asynchronous, and wicked fast C++11 client for Redis

redox Modern, asynchronous, and wicked fast C++11 client for Redis [] (https://travis-ci.org/hmartiro/redox) Redox is a C++ interface to the Redis key

Hayk Martiros 380 Jan 7, 2023
Build a redis client by cpp:)

Redix A light redis client implement by c++. Develop Environment cmake:3.16.3 g++:9.3.0 os:ubuntu 20.04.01 reids:5.0.7 Welcome to see my talk. Benchma

null 3 Apr 15, 2022
C++11 Lightweight Redis client: async, thread-safe, no dependency, pipelining, multi-platform

C++11 Lightweight Redis client: async, thread-safe, no dependency, pipelining, multi-platform

Simon Ninon 1k Jan 8, 2023
Redis client written in C++

redis-plus-plus Overview Features Branches Installation Install hiredis Install redis-plus-plus Run Tests (Optional) Use redis-plus-plus In Your Proje

null 1k Dec 27, 2022
cpp_redis is a C++11 Asynchronous Multi-Platform Lightweight Redis Client

C++11 Lightweight Redis client: async, thread-safe, no dependency, pipelining, multi-platform

CPP Redis 561 Jan 3, 2023