redis-plus-plus的安装与使用
1、先安装hiredis
git clone https://github.com/redis/hiredis.git
cd hiredis
make
sudo make install
cd ..
2、然后安装redis-plus-plus
git clone https://github.com/sewenew/redis-plus-plus.git
cd redis-plus-plus
mkdir build
cd build
cmake ..
make
sudo make install
cd ..
sudo ldconfig
3、写代码测试
#include <sw/redis++/redis++.h>
#include <iostream>
using namespace std;
using namespace sw::redis;
int main()
{
auto redis = Redis("tcp://127.0.0.1:6379");
redis.set("key", "val");
auto val = redis.get("key");
if (val) {
std::cout << *val << std::endl;
}
std::vector<std::string> vec = {"a", "b", "c"};
redis.rpush("list", vec.begin(), vec.end());
redis.rpush("list", {"a", "b", "c"});
vec.clear();
redis.lrange("list", 0, -1, std::back_inserter(vec));
return 0;
}
4、编译链接
g++ xxx.cc -lredis++ -lhiredis -lpthread
g++ xxx.cc -lredis++ -lhiredis -lpthread -std=c++17