如何在 C++ 程序中使用 Redis?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2181474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:26:01  来源:igfitidea点击:

How to use Redis within a C++ program?

c++databaseredis

提问by dan

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

在 C++ 程序中使用 Redis DB 的最佳方法是什么?

采纳答案by Kornel Kisielewicz

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

使用C 绑定库?似乎没有任何地方可用的 C++ 包装器。

回答by Ludger Sprenker

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 :-)

我已经分叉了虚构的 redis-cplusplus-client,使其与 redis-server v2.0 兼容,添加了缺少的 api 调用并实现了一致的散列。还有一个早期状态的高级类,在不久的将来可以像 stl 类型一样使用(shared_string、shared_int、shared_set 等)。尚未准备好生产,但提供的测试已成功运行:-)

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

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

回答by brian watling

https://github.com/brianwatling/redispp

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.

我刚刚在 github 上发布了我的 c++ redis 客户端。它现在的主要功能是流水线,我将很快添加更多功能,接下来可能是分片/一致散列。

回答by feuGene

Official list of C++ clients

C++ 客户端的官方列表

Explore a full listof 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++.

探索一个完整列表Redis的C ++客户端上redis.io。您会发现有基于 boost、Qt 等的不同客户端。请注意,此时没有任何 C++ 客户端实现被标记为“推荐”。但是有一个推荐的 C 客户端,hiredis,它在 C++ 中应该可以正常工作。

回答by z8000

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

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.

这个 C++ 客户端库没有维护,但是因为很少有人真正使用 C++ 来与 Redis 通信。

回答by for_stack

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

我写了一个 C++ Redis 客户端:redis-plus-plus。它基于hiredis,并用C++11 编写。它支持以下功能:

  • 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.
  • Redis 的大多数命令。
  • 连接池。
  • Redis 脚本。
  • 除非另有说明,否则线程安全。
  • Redis 发布/订阅。
  • Redis 管道。
  • Redis 事务。
  • Redis 集群。
  • Redis 哨兵。
  • Redis 流。
  • 类似 STL 的接口。
  • 通用命令接口。

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 docfor details.

查看文档以获取详细信息。

回答by Peter Hizalev

https://github.com/petrohi/hiredispp

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.

还可以查看hiredispp。它远非完整但非常简单的实现,它围绕着基于 C 的hiredis。Hiredis 负责处理低级协议和网络内容,而hiredispp 包装器只是使其对 C++ 友好。

回答by Luca Marturana

Another C++ client can be found here: https://github.com/luca3m/redis3m

另一个 C++ 客户端可以在这里找到:https: //github.com/luca3m/redis3m

It's a wrapper of hiredis, with nice C++ classes, an high availability connection pooling and a set of patterns already implemented and ready to use.

它是hiredis 的包装器,具有漂亮的C++ 类、高可用性连接池和一组已经实现并可以使用的模式。

回答by Ivan Baidakou

If you care about performance, give a try for bredis. It uses c++ 14 and boost::asioand has not other dependencies (i.e. no hiredisnor libevetc.). 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。它使用 c++ 14 并且boost::asio没有其他依赖项(即没有hiredis也没有libev等)。它的使用可能不如其他 C++ 库方便,但为了性能和最大灵活性,这是设计上的折衷。

bredismuch more easy to use on Windows, as it has no hiredisdependency.

bredis在 Windows 上更容易使用,因为它没有hiredis依赖性。