1
0
Fork 0
mirror of https://github.com/KingDuckZ/incredis synced 2024-12-23 21:25:44 +00:00
A lightweight C++ wrapper around hiredis.
Find a file
King_DuckZ ead1736e55 Update cmake find script
At least on Arch, I think the config.h has been
split into smaller files and version number is
in its own file now.
2021-05-02 13:47:15 +02:00
cmake/Modules Update cmake find script 2021-05-02 13:47:15 +02:00
include/incredis Don't copy nul-terminators if any. 2018-10-13 14:47:28 +01:00
lib Use the new int_conv instead of the old lexical_cast. 2018-10-13 12:34:56 +01:00
pkgconfig Improve installation, but there are some cmake warnings now. 2017-05-20 02:19:22 +01:00
src Use the new int_conv instead of the old lexical_cast. 2018-10-13 12:34:56 +01:00
test/integration Add performance test calling SET 1.5 mil times. 2016-12-05 13:01:40 +00:00
.gitignore Optimization - Get rid of the vector that held the unique_ptr. 2016-12-06 18:39:41 +00:00
.gitmodules Duckhandy has moved, update location and fetch latest. 2018-10-12 21:15:00 +01:00
CMakeLists.txt Fix install paths 2020-05-13 00:22:38 +02:00
COPYING First commit 2016-07-14 16:36:14 +01:00
flat_git.yml Also ignore better-enums 2016-07-15 15:16:30 +01:00
README.md First writing of a readme file. 2016-12-02 19:28:57 +00:00

IncRedis is a C++ wrapper around the official hiredis library that aims to facilitate its use. It's intended to be easy to use and fast in performance.

Main features:

  • async - communication is handled by a separate thread
  • easy to use - declare your IncRedis object, connect and you're good to go!
  • batch operations made easy - request a batch object, run your commands through it and let the library sort out the details
  • synchronous operations still available - just run your commands on the main IncRedis object and the function will only return when the command is complete (communication still happens in a separate thread)

Example

    #include "incredis/incredis.hpp"
    #include <iostream>

    int main() {
        //Create main object and connect
        redis::IncRedis incredis("127.0.0.1", 6379);
        incredis.connect();

        //Block until connection is done (you should use
        //is_connected() to check the outcome)
        incredis.wait_for_connect();

        //Request a batch
        auto batch = incredis.make_batch();

        //Run your commands on it - feel free to flood it if needed! :)
        batch.select(2); //Choose DB 2
        batch.client_setname("IncRedisExampleCode");

        //The following line will block until all commands
        //are executed and will throw if any of the commands
        //issued on the batch failed
        batch.throw_if_failed();

        //You can still run individual commands synchronously
        incredis.set("SomeKey", "Hello IncRedis!");

        //And of course you can retrieve data as well
        auto my_value = incredis.get("SomeKey");
        if (my_value)
            std::cout << *my_value << '\n';

        //Not really required since we're about to
        //go out of scope
        incredis.disconnect();
        incredis.wait_for_disconnect();
        return 0;
    }

If you want to check the outcome of your commands instead of just generically throwing, you can:

    const std::vector<Reply>& replies = batch.replies();

Replies will be in the same order as that of the commands that generated them.

run()

Ideally all commands available in the supported version of Redis will have a corresponding C++ method, so for example you can just call set() or dbsize() and get some build-time checks for free. however, please keep in mind that this library is still at an early stage and more development is needed. If you find that the command you need is not implemented yet, you can still call the generic run() method.

    //These lines are equivalent
    batch.select(2);
    batch.run("SELECT", 2);

Please refer to the official documentation of Redis for more details about the available commands.