Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated source code. #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# vim files
*.swp
tags
build/*

*.vcxproj

*.filters

*.user
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
The MIT License (MIT)

Copyright (c) 2014 Alex Nekipelov
Copyright (c) 2019 Nick P (Kurieita)

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
157 changes: 45 additions & 112 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,129 +1,62 @@
redisclient
===========
A header-only redis client library based on redisclient (https://github.com/nekipelov/redisclient/) that was modified to remove boost.asio as a dependency. Simple but powerfull.
Currently the only dependency is asio but there are future plans to remove networking as a dependency.

Build master status: [![Build travis status](https://travis-ci.org/nekipelov/redisclient.svg?branch=master)](https://travis-ci.org/nekipelov/redisclient)
[![Build appveyor status](https://ci.appveyor.com/api/projects/status/github/nekipelov/redisclient?branch=master)](https://ci.appveyor.com/project/nekipelov/redisclient/branch/master)

Build develop status: [![Build travis status](https://travis-ci.org/nekipelov/redisclient.svg?branch=develop)](https://travis-ci.org/nekipelov/redisclient)
[![Build appveyor status](https://ci.appveyor.com/api/projects/status/github/nekipelov/redisclient?branch=develop)](https://ci.appveyor.com/project/nekipelov/redisclient/branch/develop)

Current version: 1.0.2.

Boost.asio based Redis-client header-only library. Simple but powerfull.
Also you can build the library like a shared library. Just use

This version requires С++11 compiler. If you want to use this library with old compiler, use version 0.4: https://github.com/nekipelov/redisclient/tree/v0.4.
-DREDIS_CLIENT_DYNLIB and -DREDIS_CLIENT_BUILD to build the library and
-DREDIS_CLIENT_DYNLIB to build your project.

Have amalgamated sources. See files in the `amalgamated` directory.

Get/set example:
Synchronous get/set example:

```cpp
#include <string>
#include <vector>
#include <iostream>
#include <boost/asio/io_service.hpp>
#include <boost/asio/ip/address.hpp>
#include <redisclient/redissyncclient.h>

int main(int, char **)
{
boost::asio::ip::address address = boost::asio::ip::address::from_string("127.0.0.1");
const unsigned short port = 6379;
boost::asio::ip::tcp::endpoint endpoint(address, port);

boost::asio::io_service ioService;
redisclient::RedisSyncClient redis(ioService);
boost::system::error_code ec;

redis.connect(endpoint, ec);

if(ec)
{
std::cerr << "Can't connect to redis: " << ec.message() << std::endl;
return EXIT_FAILURE;
}

redisclient::RedisValue result;

result = redis.command("SET", {"key", "value"});

if( result.isError() )
{
std::cerr << "SET error: " << result.toString() << "\n";
return EXIT_FAILURE;
}

result = redis.command("GET", {"key"});

if( result.isOk() )
{
std::cout << "GET: " << result.toString() << "\n";
return EXIT_SUCCESS;
}
else
{
std::cerr << "GET error: " << result.toString() << "\n";
return EXIT_FAILURE;
}
}
```

Async get/set example:
#include <asio/io_service.hpp>
#include <asio/ip/address.hpp>

```cpp
#include <string>
#include <iostream>
#include <functional>
#include <boost/asio/io_service.hpp>
#include <boost/asio/ip/address.hpp>

#include <redisclient/redisasyncclient.h>

static const std::string redisKey = "unique-redis-key-example";
static const std::string redisValue = "unique-redis-value";

void handleConnected(boost::asio::io_service &ioService, redisclient::RedisAsyncClient &redis,
boost::system::error_code ec)
{
if( !ec )
{
redis.command("SET", {redisKey, redisValue}, [&](const redisclient::RedisValue &v) {
std::cerr << "SET: " << v.toString() << std::endl;

redis.command("GET", {redisKey}, [&](const redisclient::RedisValue &v) {
std::cerr << "GET: " << v.toString() << std::endl;

redis.command("DEL", {redisKey}, [&](const redisclient::RedisValue &) {
ioService.stop();
});
});
});
}
else
{
std::cerr << "Can't connect to redis: " << ec.message() << std::endl;
}
}
#include "redis/sync_client.h"
#include "redis/async_client.h"

int main(int, char **)
{
boost::asio::ip::address address = boost::asio::ip::address::from_string("127.0.0.1");
const unsigned short port = 6379;
boost::asio::ip::tcp::endpoint endpoint(address, port);
int main(int, char**) {
asio::ip::address address = asio::ip::address::from_string("127.0.0.1");
const unsigned short port = 6379;

boost::asio::io_service ioService;
redisclient::RedisAsyncClient redis(ioService);
asio::ip::tcp::endpoint endpoint(address, port);

redis.connect(endpoint,
std::bind(&handleConnected, std::ref(ioService), std::ref(redis),
std::placeholders::_1));
asio::io_service io;
redis::sync_client client(io);

ioService.run();
asio::error_code ec;
std::string msg = "something";

return 0;
}
```
client.connect(endpoint, msg);

Also you can build the library like a shared library. Just use
-DREDIS_CLIENT_DYNLIB and -DREDIS_CLIENT_BUILD to build redisclient
and -DREDIS_CLIENT_DYNLIB to build your project.
if (ec) {
std::cerr << "Can't connect to redis: " << ec.message() << std::endl;
return EXIT_FAILURE;
}

redis::value result;
result = client.command("SET", { "key", "value" });

if (result.isError()) {
std::cerr << "SET error: " << result.toString() << "\n";
std::cin.get();
return EXIT_FAILURE;
}

result = client.command("GET", { "key" });

if (result.isOk()) {
std::cout << "GET: " << result.toString() << "\n";
std::cin.get();
return EXIT_SUCCESS;
} else {
std::cerr << "GET error: " << result.toString() << "\n";
std::cin.get();
return EXIT_FAILURE;
}
}
30 changes: 0 additions & 30 deletions examples/CMakeLists.txt

This file was deleted.

52 changes: 52 additions & 0 deletions examples/async_basic_pub-sub.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <iostream>

#include <redis/async_client.hpp>

static const std::string channelName = "unique-redis-channel-name-example";

using namespace redis;

void subscribeHandler(asio::io_service& ioService, const std::vector<char>& buf) {
std::string msg(buf.begin(), buf.end());
std::cout << msg << std::endl;

if (msg == "stop") ioService.stop();
}

void publishHandler(async_client& publisher, const value&) {
publisher.publish(channelName, "First hello", [&](const value&) {
publisher.publish(channelName, "Last hello", [&](const value&) {
publisher.publish(channelName, "stop");
});
});
}

int main(int, char**) {
asio::ip::address address = asio::ip::address::from_string("127.0.0.1");
const unsigned short port = 6379;

asio::io_service ioService;
async_client publisher(ioService);
async_client subscriber(ioService);

publisher.connect(address, port, [&](bool status, const std::string& err) {
if (!status) {
std::cerr << "Can't connect to to redis" << err << std::endl;
std::cin.get();
} else {
subscriber.connect(address, port, [&](bool status, const std::string& err) {
if (!status) {

std::cerr << "Can't connect to to redis" << err << std::endl;
std::cin.get();
} else {
subscriber.subscribe(channelName, std::bind(&subscribeHandler, std::ref(ioService), std::placeholders::_1), std::bind(&publishHandler, std::ref(publisher), std::placeholders::_1));
}
});
}
});

ioService.run();

return 0;
}