I've implement a C++ redis client lib supporting 150+ redis command and redis3.0 cluster

1,473 views
Skip to first unread message

ShuXin Zheng

unread,
Mar 3, 2015, 5:33:11 PM3/3/15
to redi...@googlegroups.com
Hi, all, I've implement a C++ redis client lib, which supporting 150+ redis command and redis3.0 cluster. Below are some samples of the lib:

1)  simple example for redis string:
void test_redis_string(acl::redis_string& cmd, const char* key)
{
    acl::string val("test_value");

    // call redis-server: SET key value
    if (cmd.set(key, val.c_str()) == false)
    {
        printf("redis set error\r\n");
        return;
    }

    // clear the string buf space
    val.clear();

    // reset the redis command object for reusing it
    cmd.reset();

    // call redis-server: GET key
    if (cmd.get(key, val) == false)
        printf("get key error\r\n");
}

void test_redis_key(acl::redis_key& cmd, const char* key)
{
    if (cmd_key.exists(key) == false)
        printf("key not exists\r\n");
    else
        printf("key exists\r\n");
}

void test_redis()
{
    const char* redis_addr = "127.0.0.1:6379";
    int  conn_timeout = 10, rw_timeout = 10;

    // the redis client connection
    acl::redis_client conn(redis_addr, conn_timeout, rw_timeout);

    const char* key = "test_key";

    // test redis STRING command
    // bind redis_string command with redis connection
    acl::redis_string cmd_string(&conn);
    test_redis_string(cmd_string, key);

    // test redis KEY command with the same redis connection
    acl::redis_key cmd_key(&conn);
    test_redis_key(cmd_key, key);
}

I just modify test_redis() to support redis3.0 cluster, show below:

2) redis client cluster example for redis3.0

void test_redis()
{
    const char* redis_addr = "127.0.0.1:6379";
    int  conn_timeout = 10, rw_timeout = 10, max_conns = 100;

    // declare redis cluster ojbect
    acl::redis_cluster cluster;
    cluster.set(redis_addr, max_conns);

    // redis operation command
    acl::redis_string cmd_string;
    acl::redis_key cmd_key;

    // bind redis command with redis cluster
    cmd_string.set_cluster(&cluster, max_conns);
    cmd_key.set_cluster(&cluster, max_conns);

    const char* key = "test_key";

    // call redis server
    test_redis_string(cmd_string, key);
    test_redis_key(cmd_key, key);
}
The redis cluster support caching the redis hash-slot in client for performance, and can dynamic add redis server nodes in running.

This redis lib is not same as the hiredis, hiredis can only support limit interface for users,but the new lib provides full redis command supporting.

Ok, the samples are very easily for user to use redis-server. Below are some resources url:
The C++ HEADER in https://github.com/zhengshuxin/acl/tree/master/lib_acl_cpp/include/acl_cpp/redis
The samples in https://github.com/zhengshuxin/acl/tree/master/lib_acl_cpp/samples/redis
The redis cluster sample in https://github.com/zhengshuxin/acl/blob/master/lib_acl_cpp/samples/redis/redis_cluster/redis_cluster.cpp
The redis client source codes in https://github.com/zhengshuxin/acl/tree/master/lib_acl_cpp/src/redis

Thanks
zsxxsz

Josiah Carlson

unread,
Mar 3, 2015, 5:49:35 PM3/3/15
to redi...@googlegroups.com
If you haven't done so already, you should submit a pull request to include a reference to your client at: http://redis.io/clients

 - Josiah

--
You received this message because you are subscribed to the Google Groups "Redis DB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to redis-db+u...@googlegroups.com.
To post to this group, send email to redi...@googlegroups.com.
Visit this group at http://groups.google.com/group/redis-db.
For more options, visit https://groups.google.com/d/optout.

ShuXin Zheng

unread,
Mar 3, 2015, 8:51:50 PM3/3/15
to redi...@googlegroups.com
Ok, I've submit a pull request at https://github.com/antirez/redis-doc/pull/500.
Thanks

在 2015年3月4日星期三 UTC+8上午6:49:35,Josiah Carlson写道:

fj

unread,
Mar 5, 2015, 5:14:33 AM3/5/15
to redi...@googlegroups.com
Hi ShuXin,

Great to have a cluster aware C++ API, I am actually looking for just that.
I looked quickly through the source-code and saw a lot of comments which is nice.
It is however not so nice that many of the comments as well as the documentation is in chinese(I guess?) which is quite tricky for most non-chinese to read ;-)
I would recommend you to translate both parts into English if you would like more contributions to your project and more users to use your API (ofcourse just speaking on behalf of the non-chinese part of the world).

Regards,

Flemming

ShuXin Zheng

unread,
Mar 5, 2015, 8:48:14 AM3/5/15
to redi...@googlegroups.com
Yes, these comments are writen in Chinese. I'll try to translate these comments to English and wish those no-chinese can understand.

zsx .

在 2015年3月5日星期四 UTC+8下午6:14:33,fj写道:

ShuXin Zheng

unread,
Mar 5, 2015, 9:12:28 AM3/5/15
to redi...@googlegroups.com
Before I complete the comments in English, you can run the cluster sample as below:
1) Download the acl project(https://github.com/zhengshuxin/acl/archive/master.zip) to your local linux machine and extract the source codes to local disk
2) Enter acl directory(you should see lib_acl, lib_protocol, lib_acl_cpp, etc, which are the base lib) and write "make all"
3) Enter acl/lib_acl_cpp/samples/redis/redis_cluster, run "make" to compile the redis_cluster sample
4) run: ./redis_cluster -h, show the help below:
usage: ./redis_cluster -h[help]
-s redis_addr_list[127.0.0.1:6379]
-n count[default: 10]
-C connect_timeout[default: 10]
-I rw_timeout[default: 10]
-c max_threads[default: 10]
-w wait_for_cluster_resume[default: 500 ms]
-r retry_for_cluster_resnum[default: 10]
-a cmd[set|get|expire|ttl|exists|type|del]

If you want to connect the redis-server cluster to set/get data, you can let the program to connect any one of the cluster(for example given the ip: 127.0.0.1:6379):
./redis_cluster -s 127.0.0.1:6379 -n 100000 -c 2 -a set  # start two threads to set 100000 record to the redis-server cluster
./redis_cluster -s 127.0.0.1:6379 -n 100000 -c 5 -a get  # start five threads to get 100000 record from the redis-server cluster

zsx

在 2015年3月5日星期四 UTC+8下午9:48:14,ShuXin Zheng写道:

fj

unread,
Mar 9, 2015, 6:06:53 AM3/9/15
to redi...@googlegroups.com
Thanks,

I tried it and it compiles and runs fine on my 3-node Reds cluster.
What is the license terms for it?
The sourceforge page mentions both GPLv2 and IBM public license.
I want to use it for a commercial product but I cant distribute my own source code using acl redis library.

Regards,

Flemming

ShuXin Zheng

unread,
Mar 9, 2015, 8:03:56 AM3/9/15
to redi...@googlegroups.com
I release acl both in github and sourceforge. And use GPLv2 and IBM public license. You can use it in commercial product. If you modify some acl code in your product, you maybe open the part source code, and you needn't your source code except for that.
Thanks for using acl and acl redis for your application, and giving some advise for acl.

zsx

在 2015年3月9日星期一 UTC+8下午6:06:53,fj写道:

ShuXin Zheng

unread,
Mar 10, 2015, 2:55:37 AM3/10/15
to redi...@googlegroups.com
Today I make a decision that  the acl project (include redis module) will use BSD & IBM public license. And I've changed it in sourceforge.

Regards

zsx

在 2015年3月9日星期一 UTC+8下午6:06:53,fj写道:
Thanks,

TDK

unread,
Mar 10, 2015, 5:52:59 AM3/10/15
to redi...@googlegroups.com
Hi zsx,

I am completely new to Redis, but I really like what I see. I am making a C++ program using ZeroMQ and Protocol Buffers. I think I can use your library very nicely in my code. I am currently working on a Ubuntu machine and I can't seem to get the Redis code running. Your help would really be appreciated.
So far, here are the exact commands that I have run to try out your libraries, but I can't get the code to run:


git clone https://github.com/zhengshuxin/acl.git

cd lib_acl

make

cd ..

cd lib_protocol

make

cd ..

cd lib_acl_cpp

make

cd ..

cd lib_acl_cpp/samples/redis

make 

Now I want to run the sample code that you have provided in the README section, I am not sure how to go about that. Any help would be really appreciated.


Many Thanks,
TDK

ShuXin Zheng

unread,
Mar 10, 2015, 9:02:48 AM3/10/15
to redi...@googlegroups.com
In lib_acl_cpp\samples\redis there are many samples for redis client. You can enter any directory to run the sample in it. For example, enter samples redis_cluster,  you can ran ./redis_cluster -h some help info will shown. Before run the samples, redis-server should be running at first. And you can run as follow:
./redis_cluster -s 127.0.0.1:6379 -n 1000 -c 10 -a set
That is to say you run the sample which connect redis-server with 127.0.0.1:6379 addr, and using redis command SET with 10 threads and 1000 times each thread.

zsx

在 2015年3月4日星期三 UTC+8上午6:33:11,ShuXin Zheng写道:

fj

unread,
Mar 10, 2015, 10:27:31 AM3/10/15
to redi...@googlegroups.com
Wow, changing to BSD is great - but how come you have two different licenses BSD and IBM public license.
Can we choose which license to use or do they both apply or how does that part work?
(Sorry, I am not too familiar with these legal aspects)

Btw: it seems IBM public license is superseeded by CPL which is superseeded by EPL.

Regards,

Flemming

ShuXin Zheng

unread,
Mar 10, 2015, 10:22:00 PM3/10/15
to redi...@googlegroups.com
In the acl, I've used some source code from Postfix of IBM, which uses IBM public license.
Any other suggestion? Thanks

Regards.
zsx

在 2015年3月10日星期二 UTC+8下午10:27:31,fj写道:

ShuXin Zheng

unread,
Apr 15, 2015, 11:07:07 AM4/15/15
to redi...@googlegroups.com
I've translated most of the comments in acl redis headers to English now, which're in https://github.com/zhengshuxin/acl/tree/master/lib_acl_cpp/include/acl_cpp/redis

zsx


在 2015年3月5日星期四 UTC+8下午6:14:33,fj写道:
Hi ShuXin,
Reply all
Reply to author
Forward
0 new messages