How to use Redis within a C++ program?

前端 未结 9 1352
被撕碎了的回忆
被撕碎了的回忆 2020-12-15 06:48

What would be the best way to use a Redis DB within a C++ program?

相关标签:
9条回答
  • 2020-12-15 07:02

    http://github.com/fictorial/redis-cplusplus-client

    This C++ client library is not maintained however as few people actually use C++ to communicate with Redis.

    0 讨论(0)
  • 2020-12-15 07:09

    Official list of C++ clients

    Explore a full list of Redis C++ clients on redis.io. You will find there different clients based on boost, Qt, etc. Note that at this time none of the C++ client implementations are marked as "Recommended." But there is a recommended C client, hiredis, which should work just fine in C++.

    0 讨论(0)
  • 2020-12-15 07:11

    If you care about performance, give a try for bredis. It uses c++ 14 and boost::asio and has not other dependencies (i.e. no hiredis nor libev etc.). Its usage might be not as convenient as the other C++ libraries, but that was trade off by design in the sake of performance and maximum flexibility.

    bredis much more easy to use on Windows, as it has no hiredis dependency.

    0 讨论(0)
  • 2020-12-15 07:12

    https://github.com/brianwatling/redispp

    I've just released my c++ redis client on github. It's main feature right now is pipelining, I'll be adding more features soon, possibly sharding/consistent hashing next.

    0 讨论(0)
  • 2020-12-15 07:13

    Using a C bindings library? There doesn't seem to be a C++ wrapper available anywhere.

    0 讨论(0)
  • 2020-12-15 07:13

    I wrote a C++ Redis client: redis-plus-plus. It's based on hiredis, and written in C++11. It supports the following features:

    • Most commands for Redis.
    • Connection pool.
    • Redis scripting.
    • Thread safe unless otherwise stated.
    • Redis publish/subscribe.
    • Redis pipeline.
    • Redis transaction.
    • Redis Cluster.
    • Redis Sentinel.
    • Redis Stream.
    • STL-like interface.
    • Generic command interface.

    It's very fast, and easy to use. If you have any problem with this client, feel free to let me know. If you like it, also feel free to star it :)

    #include <sw/redis++/redis++.h>
    using namespace sw::redis;
    
    try {
        Redis redis("tcp://127.0.0.1:6379");
    
        redis.set("key", "val");
        auto val = redis.get("key");
        if (val) {
            // dereference val to get the value of string type.
            std::cout << *val << std::endl;
        }   // else key doesn't exist.
    
        redis.rpush("list", {"a", "b", "c"});
        std::vector<std::string> list;
        redis.lrange("list", 0, -1, std::back_inserter(list));
    
        // put a vector<string> to Redis list.
        redis.rpush("another-list", list.begin(), list.end());
    
        auto tx = redis.transaction();
    
        auto tx_replies = tx.incr("num0")
                            .incr("num1")
                            .mget({"num0", "num1"})
                            .exec();
    
        auto redis_cluster = RedisCluster("tcp://127.0.0.1:7000");
    
        // RedisCluster has similar interface as Redis.
        redis_cluster.set("key", "value");
        val = redis_cluster.get("key");
    } catch (const Error &err) {
        // error handling.
    }
    

    Check the doc for details.

    0 讨论(0)
提交回复
热议问题