C/C++编程:redis的cpp库

1059 篇文章 276 订阅

redis官方推荐的CPP库
在这里插入图片描述

hiredis

这里我们只用了hiredis
使用方法如下:

  1. linux安装Redis4
  2. 安装redis自带的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/下

  1. 使用方法
    在这里插入图片描述
    CMakeLists.txt
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 <iostream>
#include <string.h>
#include <string>
#include <stdio.h>

#include <hiredis/hiredis.h>
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 <iostream>
#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的简单例子

redisconnect【使用最简单】

https://gitee.com/xungen/redisconnect.git
1、RedisConnect是基于C++11实现的简单易用的Redis客户端。
2、源码只包含一个头文件与一个命令行工具源文件,无需编译安装,真正做到零依赖。
3、自带连接池功能,调用Setup方法初始化连接池,然后执行Instance方法就可以获取一个连接。
4、RedisConnect包装了常用的redis命令,对于未包装的命令你可以使用可变参模板方法(execute)进行调用。

使用方法:

  • git clone https://gitee.com/xungen/redisconnect.git

  • 然后将对应文件复制到工程中,如下
    在这里插入图片描述
    CMakeLists.txt

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 <string>
#include "RedisConnect.h"
#include<memory>
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_redis(1.8K⭐)

acl学习入门

整个工程偏向C风格 , C++工程若选择该库,在使用何种风格和多线程字符串等使用方式上会收到一定的约束

cpp_redis(355⭐)

项目地址

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)

redis-plus-plus(294⭐)

地址

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

概叙

这是一个C++客户端,用于Redis。它是基于HiRedis,用C++ 11/C++ 17编写的。

redisclient(282⭐)

redisclient(282⭐)
Boost.asio based Redis-client header-only library. Simple but powerfull.

This version requires С++11 compiler

  1. Linux下搭建boost环境,并使用CLion搭建工程

  2. 下载

$ git clone https://github.com/nekipelov/redisclient.git

$ cd redisclient && mkdir build && cd build
  1. 安装
$  cmake -DCMAKE_BUILD_TYPE=Release -DBOOST_ROOT=boost_include_dir \
	-DBOOST_LIBRARYDIR=boost_lib_dir ..

当前使用的boost是1.74.0
在这里插入图片描述
redisclient安装与使用

Aedis(9⭐)

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

在这里插入图片描述

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值