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
2017-05-20 02:19:22 +01:00
cmake/Modules Update shared_git_project and duckhandy to latest. 2016-07-14 17:01:16 +01:00
include/incredis Fix return value of hmset and update duckhandy, which fixes expire 2017-04-25 09:39:51 +01:00
lib Update duckhandy 2017-05-11 18:48:32 +01:00
pkgconfig Improve installation, but there are some cmake warnings now. 2017-05-20 02:19:22 +01:00
src Add hmset and expire commands 2017-04-25 00:08:17 +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 Import catch and write a first integration test. 2016-12-02 14:17:37 +00:00
CMakeLists.txt Improve installation, but there are some cmake warnings now. 2017-05-20 02:19:22 +01: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.