Giter Site home page Giter Site logo

asio_redis_client's Introduction

asio_redis_client

an easy to use, thread-safe, cross-platform async redis client implemented in c++11.

The best c++ redis client!

dependcy

asio

future extention(submodule)

how to use

git clone https://github.com/topcpporg/asio_redis_client.git

git submodule update --init --recursive

quick example

async interface

//create redis client
std::shared_ptr<asio_redis_client> create_client(){
  auto client = std::make_shared<asio_redis_client>(ios);
  client->enable_auto_reconnect(true);

  client->set_error_callback([](RedisValue value){
    std::cout<<value.toString()<<'\n';
  });

  bool r = client->connect(host_name, 6379);
  assert(r);
  return client;
}

void get_set() {
  auto client = create_client();

  client->auth("123456", [](RedisValue value) {
    if(value.isError()){
      std::cout<<"redis error:"<<value.toString()<<'\n';
    }

    std::cout << "auth: " << value.toString() << '\n';
  });

  client->set("hello", "world", [](RedisValue value) {
    std::cout << "set: " << value.toString() << '\n';
  });

  client->get("hello", [](RedisValue value) {
    std::cout << "get: " << value.toString() << '\n';
  });

  client->command("info", {"stats"}, [](RedisValue value) {
    std::cout << "info stats: " << value.toString() << '\n';
  });

  client->command("get", {"hello"}, [](RedisValue value) {
    std::cout << "get result: " << value.toString() << '\n';
  });

  client->del("hello", [](RedisValue value) {
    std::cout << "del: " << value.inspect() << '\n';
  });

  client->publish("mychannel", "hello world", [](RedisValue value) {
    std::cout << "publish mychannel ok, number:" << value.inspect() << '\n';
  });
}

sync interface

fun with future!

void future(){
  auto client = create_client();

  auto auth_future = client->auth("123456");
  auto set_future = client->set("hello", "world");
  auto get_future = client->get("hello");
  auto del_future = client->del("hello");

  std::cout<<"future------------\n";
  std::cout<<"auth result:"<<auth_future.Get().toString()<<"\n";
  std::cout<<"set result:"<<set_future.Get().toString()<<'\n';
  std::cout<<"get result:"<<get_future.Get().toString()<<'\n';
  std::cout<<"del result: ok"<<del_future.Get().toString()<<'\n';
  client->close();
}

void future_then(){
  auto client = create_client();
  auto future = client->auth("123456").Then([&](RedisValue value){
    std::cout<<"auth result:"<<value.toString()<<'\n';
    return client->set("hello", "world").Get();
  }).Then([&](RedisValue value){
    std::cout<<"set result:"<<value.toString()<<'\n';
    return client->get("hello").Get();
  }).Then([&](RedisValue value){
    std::cout<<"get result:"<<value.toString()<<'\n';
    return client->del("hello").Get();
  }).Then([](RedisValue value){
    std::cout<<value.toString();
    return "del result: ok";
  });

  std::cout<<"future then---------------\n";
  auto result = future.Get();
  std::cout<<result<<'\n';
}

void future_then_finally(){
  auto client = create_client();
  client->auth("123456").Then([=](RedisValue value){
    std::cout<<"auth result:"<<value.toString()<<'\n';
    return client->set("hello", "world").Get();
  }).Then([=](RedisValue value){
    std::cout<<"set result:"<<value.toString()<<'\n';
    return client->get("hello").Get();
  }).Then([=](RedisValue value){
    std::cout<<"get result:"<<value.toString()<<'\n';
    return client->del("hello").Get();
  }).Then([](RedisValue value){
    std::cout<<value.toString();
    return "del result: ok";
  }).Finally([](std::string result){
    std::cout<<result<<'\n';
  });
  std::cout<<"future then finally---------------\n";
}

error handling

auto client = std::make_shared<asio_redis_client>(ios);
client->enable_auto_reconnect(true);

//network error occurred
client->set_error_callback([](RedisValue value){
  std::cout<<value.toString()<<'\n';
});

//redis command error
client->auth("123456", [](RedisValue value) {
  if(value.isError()){
    std::cout<<"redis error:"<<value.toString()<<'\n';
    return;
  }

  std::cout << "auth: " << value.toString() << '\n';
});

important

If you like it, please star it, let more people know it is the best c++ redis client :)

roadmap

  1. pipeline
  2. redis cluster

asio_redis_client's People

Contributors

qicosmos avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.