Redis client written in C++
This is a C++ client library for Redis. It's based on hiredis, and is compatible with C++11 and later versions. Besides Redis, it also works with other key-value stores supporting RESP (REdis Serialization Protocol), e.g. Valkey, DragonflyDB, KeyDB.
NOTE: I'm not a native speaker. So if the documentation is unclear, please feel free to open an issue or pull request. I'll response ASAP.
Since redis-plus-plus is based on hiredis, you should install hiredis first. The minimum version requirement for hiredis is v0.12.1. However, the latest stable release of hiredis is always recommended.
NOTE: You must ensure that there's only 1 version of hiredis installed. Otherwise, you might get some weird problems. Check the following issues for example: issue 135, issue 140 and issue 158.
Normally, you can install hiredis with a C++ package manager, and that's the easiest way to do it, e.g. sudo apt-get install libhiredis-dev. However, if you want to install the latest code of hiredis, or a specified version (e.g. async support needs hiredis v1.0.0 or later), you can install it from source.
Note again: DO NOT INSTALL MULTIPLE VERSIONS OF HIREDIS.
git clone https://github.com/redis/hiredis.git
cd hiredis
make
sudo make installBy default, hiredis is installed at /usr/local. If you want to install hiredis at non-default location, use the following commands to specify the installation path.
make PREFIX=/non/default/path
make PREFIX=/non/default/path installredis-plus-plus is built with CMAKE.
git clone https://github.com/sewenew/redis-plus-plus.git
cd redis-plus-plus
mkdir build
cd build
cmake ..
make
sudo make install
cd ..If hiredis is installed at non-default location, you should use CMAKE_PREFIX_PATH to specify the installation path of hiredis. By default, redis-plus-plus is installed at /usr/local. However, you can use CMAKE_INSTALL_PREFIX to install redis-plus-plus at non-default location.
cmake -DCMAKE_PREFIX_PATH=/path/to/hiredis -DCMAKE_INSTALL_PREFIX=/path/to/install/redis-plus-plus ..Since version 1.3.0, by default, redis-plus-plus is built with the -std=c++17 standard. So that we can use the std::string_view and std::optional features. However, it can also be built with the -std=c++11 or -std=c++14 standard, and in that case, we have our own simple implementation of std::string_view and std::optional. In order to explicitly specify c++ standard, you can use the following cmake flag: -DREDIS_PLUS_PLUS_CXX_STANDARD=11.
cmake -DCMAKE_PREFIX_PATH=/path/to/hiredis -DCMAKE_INSTALL_PREFIX=/path/to/install/redis-plus-plus -DREDIS_PLUS_PLUS_CXX_STANDARD=11 ..NOTE: You should build redis-plus-plus and your application with the same standard, e.g. if you build redis-plus-plus with C++17 standard, you MUST also build your application code with C++17 standard.
When compiling redis-plus-plus, it also compiles a test program, which might take a while. However, you can disable building test with the following cmake option: -DREDIS_PLUS_PLUS_BUILD_TEST=OFF.
cmake -DCMAKE_PREFIX_PATH=/path/to/hiredis -DCMAKE_INSTALL_PREFIX=/path/to/install/redis-plus-plus -DREDIS_PLUS_PLUS_BUILD_TEST=OFF ..By default, redis-plus-plus builds both a static library and a shared library. If you only want to build one of them, you can disable the other with -DREDIS_PLUS_PLUS_BUILD_STATIC=OFF or -DREDIS_PLUS_PLUS_BUILD_SHARED=OFF.
redis-plus-plus builds static library with -fPIC option, i.e. Position Independent Code, by default. However, you can disable it with -DREDIS_PLUS_PLUS_BUILD_STATIC_WITH_PIC=OFF.
You can also use cmake's FetchContent feature to install redis-plus-plus. Check this for an example.
Now hiredis has Windows support, and since Visual Studio 2017, Visual Studio has built-in support for CMake. So redis-plus-plus also supports Windows platform now. It has been fully tested with Visual Studio 2017 and later on Win 10. I'm not familiar with Visual Studio environment, and the following doc might not be accurate. If you're familiar with the Windows platform, feel free to update this doc on how to install redis-plus-plus on Windows.
The following are some links on how to build CMake project with Visual Studio 2017 or later. If you're not familiar with it, you'd better read these instructions first:
NOTE: IMHO, Visual Studio 2017's support for CMake project is not very mature, and I recommend you to build hiredis and *redis-plus-plus with Visual Studio 2019.
First of all, you need to get the latest code of hiredis on master branch. Older version might not support Windows platform. hiredis' CMakeLists.txt uses add_compile_definitions method, which is only supported by cmake 3.12 or later. However, Visual Studio 2017's cmake version is older than that. So if you're using Visual Studio 2017, you need to comment the following line in the CMakeLists.txt file:
#IF(WIN32)
# ADD_COMPILE_DEFINITIONS(_CRT_SECURE_NO_WARNINGS WIN32_LEAN_AND_MEAN)
#ENDIF()You can use the Open Folder feature to open hiredis project, and build it with the instructions (links) mentioned above.
Since redis-plus-plus depends on hiredis, we need to specify the installation paths of hiredis before building it. You can use the Open Folder feature to open redis-plus-plus project. You need to edit the CMakeSetting.json file (automatically generated by Visual Studio) to set HIREDIS_HEADER, HIREDIS_LIB and TEST_HIREDIS_LIB variables to specify the installation path of hiredis headers, installation path of hiredis dynamic library and installation path of hiredis static library. The following is an example of CMakeSetting.json file:
…Then you can build it with the instructions (links) mentioned above.
NOTE:
REDIS_PLUS_PLUS_CXX_STANDARD cmake option to 11.If you want to build the project with Visual Studio and have questions about it, please follow the steps below. The following is tested on Visual Studio 2022 Community.
# download two projects into this folder
mkdir redis++
cd redis++
# make sure you create a hiredis first to work as a library
mkdir hiredis-lib
cd hiredis-lib
mkdir lib
git clone https://github.com/redis/hiredis.git
cd hiredisSo far it should be fine with each step. Then open CMakeLists.txt file. Modify the following line and comment it out
...
# SET(CMAKE_DEBUG_POSTFIX d)
...Then go back to hiredis project folder
mkdir build
cd build
# convert project into visual studio 2022, if necessary choose you version e.g 19 2019 etc.
cmake -G "Visual Studio 17 2022" ..
./hiredis.slnSet hiredis as Startup Project then click Build Solution in Debug Mode
After successfull build, copy all the files under Debug into hiredis-lib/lib folder
Here the work for hiredis should be finished.
Then go back to redis++ folder. Open Terminal here
git clone https://github.com/sewenew/redis-plus-plus.git
cd redis-plus-plus
mkdir build
cd buildNow you should always have openssl on your PC, otherwise can use chocolatey to install it. For Visual Studio 2022 please install pthread separately using vpckg, following this link
After all preparation. If you want to convert all projects then
cmake -DCMAKE_PREFIX_PATH="$(ABSOLUTE_PATH)\hiredis-lib" -G "Visual Studio 17 2022" ..
cd build
./redis++.slnset redis++_static as Startup Project then click Build Solution
So far build has been successfully finished!
On Windows platform, if your application code also needs to include windows.h. You must ensure that sw/redis++/redis++.h is included before windows.h. Check this issue for detail.
Basic support for building a GNU/Debian package is supplied with the use of cmake. The following example shows how to build the Debian package:
mkdir build; cd build
cmake ..
cpack -G DEBThe install prefix may be modified as follows:
mkdir build; cd build
cmake -DCMAKE_INSTALL_PREFIX=/usr ..
cpack -G DEBredis-plus-plus has been fully tested with the following compilers:
…If you build redis-plus-plus with -DREDIS_PLUS_PLUS_BUILD_TEST=ON (the default behavior, and you can disable building test with -DREDIS_PLUS_PLUS_BUILD_TEST=OFF), you'll get a test program in build/test directory: build/test/test_redis++.
In order to run the tests, you need to set up a Redis instance, and a Redis Cluster. Since the test program will send most of Redis commands to the server and cluster, you need to set up Redis of the latest version. Otherwise, the tests m
No open issues yet, or sync has not completed.