This code connects to a Redis server running on localhost at port 6379 and saves the key-value pair "key": "value" to the database. 


Note: Before running this code, you need to install and run a Redis server on your machine, and also install the cpp_redis library.

#include <iostream>
#include <cpp_redis/cpp_redis>

int main()
{
    cpp_redis::client client;

    client.connect("127.0.0.1", 6379, [] (const std::string& host, std::size_t port, cpp_redis::connect_state status) {
        if (status == cpp_redis::connect_state::dropped) {
            std::cout << "client disconnected from " << host << ":" << port << std::endl;
        }
    });

    client.set("key", "value", [] (cpp_redis::reply& reply) {
        std::cout << "set command replied: " << reply << std::endl;
    });

    client.sync_commit();

    return 0;
}




  • No labels