I have found the following libraries to develop redis applications with c++ :
- hiredis (only available for c)
- redis-plus-plus (based on hiredis, available for C++)
- cpp-redis Asynchronous Multi-Platform, no dependency
Installation of redis-plus-plus
First lets install hiredis
1 2 3 4 5 |
git clone https://github.com/redis/hiredis.git cd hiredis mkdir build cd build cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX:PATH=~/usr .. && make -j8 all install |
Then we install redis-plus-plus
1 2 3 4 5 6 |
git clone https://github.com/sewenew/redis-plus-plus.git cd redis-plus-plus mkdir build cd build cmake -DCMAKE_BUILD_TYPE=Release -DREDIS_PLUS_PLUS_BUILD_TEST=ON -DREDIS_PLUS_PLUS_CXX_STANDARD=17 -DCMAKE_INSTALL_PREFIX:PATH=~/usr .. && make -j8 all install |
Using redis-plus-plus in your application
I have created the following CMake to make use smoother:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
set(ENV{PKG_CONFIG_PATH} "$ENV{HOME}/usr/lib/pkgconfig:" $ENV{PKG_CONFIG_PATH}) MESSAGE("PKG_CONFIG_PATH:" $ENV{PKG_CONFIG_PATH}) find_package(PkgConfig) pkg_check_modules(HIREDIS hiredis) if(${HIREDIS_FOUND}) MESSAGE("HIREDIS_FOUND:" ${HIREDIS_FOUND}) MESSAGE("HIREDIS_VERSION:" ${HIREDIS_VERSION}) MESSAGE("HIREDIS_LIBRARIES:" ${HIREDIS_LIBRARIES}) MESSAGE("HIREDIS_INCLUDE_DIRS:" ${HIREDIS_INCLUDE_DIRS}) MESSAGE("HIREDIS_LIBRARY_DIRS:" ${HIREDIS_LIBRARY_DIRS}) INCLUDE_DIRECTORIES(${HIREDIS_INCLUDE_DIRS}) LINK_DIRECTORIES(${HIREDIS_LIBRARY_DIRS}) endif() pkg_check_modules(REDIS_PLUS_PLUS redis++) if(${REDIS_PLUS_PLUS_FOUND}) MESSAGE("REDIS_PLUS_PLUS_FOUND:" ${REDIS_PLUS_PLUS_FOUND}) MESSAGE("REDIS_PLUS_PLUS_VERSION:" ${REDIS_PLUS_PLUS_VERSION}) MESSAGE("REDIS_PLUS_PLUS_LIBRARIES:" ${REDIS_PLUS_PLUS_LIBRARIES}) MESSAGE("REDIS_PLUS_PLUS_INCLUDE_DIRS:" ${REDIS_PLUS_PLUS_INCLUDE_DIRS}) MESSAGE("REDIS_PLUS_PLUS_LIBRARY_DIRS:" ${REDIS_PLUS_PLUS_LIBRARY_DIRS}) INCLUDE_DIRECTORIES(${REDIS_PLUS_PLUS_INCLUDE_DIRS}) LINK_DIRECTORIES(${REDIS_PLUS_PLUS_LIBRARY_DIRS}) endif() if(${REDIS_PLUS_PLUS_FOUND} AND ${HIREDIS_FOUND} ) add_executable(redis++_demo src/redis++_demo.cpp) target_link_libraries(redis++_demo ${HIREDIS_LIBRARIES} ${REDIS_PLUS_PLUS_LIBRARIES} -pthread) else() MESSAGE("redis++ or hiredis are not available") endif() |
Installation of cpp_redis
Because this repository uses some sub-modules we have to pull them as well,
1 2 3 4 5 6 |
git clone https://github.com/cpp-redis/cpp_redis/ git submodule update --init --recursive git submodule update --recursive --remote mkdir build cd build cmake -DCMAKE_CXX_FLAGS=-std=c++1z -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX:PATH=~/usr .. && make -j8 all install |
Using cpp_redis in your application
Since cpp_redis is not coming with a CMake, I wrote a CMake for that:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
project(cpp_redis_demo) cmake_minimum_required(VERSION 3.16) set(ENV{PKG_CONFIG_PATH} "$ENV{HOME}/usr/lib/pkgconfig:" $ENV{PKG_CONFIG_PATH}) MESSAGE("PKG_CONFIG_PATH:" $ENV{PKG_CONFIG_PATH}) find_package(PkgConfig) pkg_check_modules(CPP_REDIS cpp_redis) if(${CPP_REDIS_FOUND}) MESSAGE("CPP_REDIS_FOUND:" ${CPP_REDIS_FOUND}) MESSAGE("CPP_REDIS_VERSION:" ${CPP_REDIS_VERSION}) MESSAGE("CPP_REDIS_LIBRARIES:" ${CPP_REDIS_LIBRARIES}) MESSAGE("CPP_REDIS_INCLUDE_DIRS:" ${CPP_REDIS_INCLUDE_DIRS}) MESSAGE("CPP_REDIS_LIBRARY_DIRS:" ${CPP_REDIS_LIBRARY_DIRS}) INCLUDE_DIRECTORIES(${CPP_REDIS_INCLUDE_DIRS}) LINK_DIRECTORIES(${CPP_REDIS_LIBRARY_DIRS}) add_executable(cpp_redis_demo src/cpp_redis_demo.cpp) target_link_libraries(cpp_redis_demo ${CPP_REDIS_LIBRARIES} -pthread) else() MESSAGE("cpp redis not found") endif() |
Now you can run the redis from docker and expose the port:
1 |
docker run -p 6379:6379 --name redis-server -it redis |