C++接入redis

项目地址

https://github.com/sewenew/redis-plus-plus

https://github.com/redis/hiredis

# 1、编译安装 hiredis 项目
wget https://github.com/redis/hiredis/archive/refs/tags/v1.2.0.tar.gz
tar -xvf v1.2.0.tar.gz
mv hiredis-1.2.0 hiredis && rm -rf v1.2.0.tar.gz && cd hiredis
# 编译安装到 build 目录下
make && make PREFIX=./build install

# 2、编译安装 redis-plus-plus 项目
wget https://github.com/sewenew/redis-plus-plus/archive/refs/tags/1.3.10.tar.gz
tar -xvf redis-plus-plus-1.3.10.tar.gz && rm redis-plus-plus-1.3.10.tar.gz

cd redis-plus-plus-1.3.10 && mkdir build && cd build
cmake -DCMAKE_PREFIX_PATH=hiredis的静/动态库所在的目录 -DCMAKE_INSTALL_PREFIX=redis-plus-plus的安装目录 ..
# cmake -DCMAKE_PREFIX_PATH=hiredis的静/动态库所在的目录 -DCMAKE_INSTALL_PREFIX=. ..
make && make install

使用

#include <iostream>
#include "sw/redis++/redis++.h"

int main(int argc, char const *argv[]) {
    sw::redis::ConnectionOptions option{};
    option.host = "0.0.0.0";
    option.port = 6379;
    option.password = "jtzlredis";
    sw::redis::Redis redis = sw::redis::Redis(option);
    std::cout <<  redis.ping() << std::endl;
    // 其它命令
    return 0;
}

特别的编译选项

  • -lredis++
  • -lhiredis
  • -std=c++17
  • -lpthread
  • -Wall
posted @ 2023-10-16 21:48  黄河大道东  阅读(47)  评论(0编辑  收藏  举报