How to use Redis within a C++ program?

2019-01-24 03:21发布

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

8条回答
欢心
2楼-- · 2019-01-24 03:22

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.
  • STL-like interface.

It's very fast, and easy to use:

#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.

查看更多
我只想做你的唯一
3楼-- · 2019-01-24 03:28

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++.

查看更多
forever°为你锁心
4楼-- · 2019-01-24 03:31

https://github.com/petrohi/hiredispp

Also check out hiredispp. It is far from complete, but very simplistic implementation that wraps around C based hiredis. Hiredis takes care of low level protocol and networking stuff while hiredispp wrappers just make it C++ friendly.

查看更多
Luminary・发光体
5楼-- · 2019-01-24 03:32

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

查看更多
爷的心禁止访问
6楼-- · 2019-01-24 03:38

I have forked the fictorial redis-cplusplus-client, made it compatible to redis-server v2.0, added missing api calls and implemented consistent hashing. There is also an early state of high level classes that will be useable like stl types in the near future (shared_string, shared_int, shared_set, ...). Nothing is production ready yet but the provided tests are succesfully running :-)

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

查看更多
Rolldiameter
7楼-- · 2019-01-24 03:43

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.

查看更多
登录 后发表回答