这里我们只用了hiredis
使用方法如下:
# cd {redis-src}
# cd deps/hiredis/
# make
# make install
mkdir -p /usr/local/include/hiredis /usr/local/include/hiredis/adapters /usr/local/lib
cp -pPR hiredis.h async.h read.h sds.h /usr/local/include/hiredis
cp -pPR adapters/*.h /usr/local/include/hiredis/adapters
cp -pPR libhiredis.so /usr/local/lib/libhiredis.so.0.14
cd /usr/local/lib && ln -sf libhiredis.so.0.14 libhiredis.so
cp -pPR libhiredis.a /usr/local/lib
mkdir -p /usr/local/lib/pkgconfig
cp -pPR hiredis.pc /usr/local/lib/pkgconfig
现在hiredis已经被安装于/usr/local/include/hiredis/和/usr/local/lib/下
cmake_minimum_required(VERSION 3.16)
project(redis_learning)
set(CMAKE_CXX_STANDARD 11)
#添加库文件搜索路径
link_directories(/usr/local/lib/)
aux_source_directory(. DIR_SRCS)
add_executable(redis_learning ${
DIR_SRCS})
target_link_libraries(redis_learning -lpthread -lhiredis )
redis.h
//
// Created by oceanstar on 2020/11/11.
//
#ifndef REDIS_LEARNING_REDIS_H
#define REDIS_LEARNING_REDIS_H
#include
#include
#include
#include
#include
class Redis{
public:
Redis(){
};
~Redis(){
this->_connect = nullptr;
this->_reply = nullptr;
}
bool connect(std::string host, int port){
this->_connect = redisConnect(host.c_str(), port);
if(this->_connect != nullptr && this->_connect->err){
printf("connect error: %s\n", this->_connect->errstr);
return false;
}
return true; //1
}
std::string get(const std::string& key){
this->_reply = (redisReply*)redisCommand(this->_connect, "GET %s", key.c_str());
std::string str = this->_reply->str;
freeReplyObject(this->_reply);
return str;
}
void set(const std::string& key, const std::string& value)
{
redisCommand(this->_connect, "SET %s %s", key.c_str(), value.c_str());
}
private:
redisContext* _connect;
redisReply* _reply;
};
#endif //REDIS_LEARNING_REDIS_H
main.cpp
#include
#include "redis.h"
int main() {
Redis *r = new Redis();
if(!r->connect("127.0.0.1", 6379)){
return 0;
}
r->set("name", "Andy");
printf("Get the name is %s\n", r->get("name").c_str());
delete r;
std::cout << "Hello, World!" << std::endl;
return 0;
}
C++操作Redis的简单例子
https://gitee.com/xungen/redisconnect.git
1、RedisConnect是基于C++11实现的简单易用的Redis客户端。
2、源码只包含一个头文件与一个命令行工具源文件,无需编译安装,真正做到零依赖。
3、自带连接池功能,调用Setup方法初始化连接池,然后执行Instance方法就可以获取一个连接。
4、RedisConnect包装了常用的redis命令,对于未包装的命令你可以使用可变参模板方法(execute)进行调用。
使用方法:
cmake_minimum_required(VERSION 3.16)
project(redis_learning)
set(CMAKE_CXX_STANDARD 11)
aux_source_directory(. DIR_SRCS)
add_executable(redis_learning ${
DIR_SRCS})
main.cpp
#include
#include "RedisConnect.h"
#include
using namespace std;
int main()
{
string key = "mykey";
string val;
//初始化连接池
RedisConnect::Setup("127.0.0.1", 6379);
//从连接池中获取一个连接
shared_ptr<RedisConnect> redis = RedisConnect::Instance();
if(redis == nullptr || !redis->ping()){
printf("redis connect failed");
return 0;
}
//设置一个键值
redis->set(key, "4r55ewedrfghgfdssd");
//获取键值内容
int code = redis->get(key, val);
if(code != 1){
std::cout << "errorCode:" << redis->getErrorCode() << "\t errorString:" << redis->getErrorString() << "\n";
return 0;
}
std::cout << "get the val " << val << "\n";
//执行expire命令设置超时时间
redis->execute("expire", key, 60);
//获取超时时间(与ttl(key)方法等价)
redis->execute("ttl", key);
//调用getStatus方法获取ttl命令执行结果
printf("超时时间:%d\n", redis->getStatus());
//执行del命令删除键值
redis->execute("del", key);
//获取分布式锁
if (redis->lock("lockey"))
{
puts("获取分布式锁成功");
//释放分布式锁
if (redis->unlock("lockey"))
{
puts("释放分布式锁成功");
}
}
return 0;
}
acl学习入门
整个工程偏向C风格 , C++工程若选择该库,在使用何种风格和多线程字符串等使用方式上会收到一定的约束
https://github.com/Cylix/cpp_redis–旧(797⭐)
https://github.com/cpp-redis/cpp_redis–新(355⭐)
cpp_redis 是一个C++ 11异步多平台轻量级Reis客户端,支持同步操作、流水线、哨兵和高可用性。
cpp_redis没有依赖关系。它的唯一要求是C++ 11。
它没有网络模块,因此您可以自由配置自己的模块,或者使用默认模块(tacopie)
https://github.com/sewenew/redis-plus-plus
这是一个C++客户端,用于Redis。它是基于HiRedis,用C++ 11/C++ 17编写的。
redisclient(282⭐)
Boost.asio based Redis-client header-only library. Simple but powerfull.
This version requires С++11 compiler
Linux下搭建boost环境,并使用CLion搭建工程
下载
$ git clone https://github.com/nekipelov/redisclient.git
$ cd redisclient && mkdir build && cd build
$ cmake -DCMAKE_BUILD_TYPE=Release -DBOOST_ROOT=boost_include_dir \
-DBOOST_LIBRARYDIR=boost_lib_dir ..
当前使用的boost是1.74.0
redisclient安装与使用
GITHUB
Aedis is a redis client designed for
Seamless integration with async code
Easy and intuitive usage
To use this library include aedis.hpp in your project. Current dendencies are Boost.Asio and libfmt. As of C++23 this library will have no external dependencies.
$ git clone https://github.com/mzimbres/aedis.git
$ cd aedis && mkdir build && cd build