本项目地址:https://github.com/Sherhang/credis.git
背景:
redis没有c++版本的redis接口,hiredis对于c++来说不是很友好,操作极为麻烦。所以对hiredis进行了封装,提供了和redis命令操作一致的c++接口,并且对连接管理做了优化。
c++的redis的接口 redis版本:Redis server v=3.0.6 sha=00000000:0 malloc=jemalloc-3.6.0 bits=64 build=7785291a3d2152db hiredis版本:https://github.com/redis/hiredis
使用说明:
#依赖于hiredis静态库 #将redis.cpp, redis.h, redis_connect_pool.cpp, redis_connect_pool.h 添加到项目中即可。 makefile写法参见makefile #提供了两个demo文件,demo2是单连接的使用示例,demo4是多连接的使用示例。
接口
//---> 基本接口
//删除key,返回被删除key的个数, 0则表示没有这个key。
int64_t del(const std::string& key);
//key是否存在
bool exists(const std::string& key);
//设置 key 的过期时间。
bool expire(const std::string& key, int64_t seconds);
//以 unix timestamp格式设置 key 的过期时间。key 过期后将不再可用。
bool expireat(const std::string& key, int64_t timestamp);//TODO
//查找所有符合给定模式 pattern 的 key,数据量很大请勿使用,可以使用getKeys。
bool keys(const std::string& pattern, std::vector<std::string>& key_list);
//scan遍历keys,返回下一次游标位置,调用错误返回负数。
int32_t scan(int32_t cursor, const std::string& pattern, int32_t count, std::vector<std::string>& keys);
//获取所有满足模式pattern并且是指定类型的keys, 可以指定获取的最大个数,max = -1 表示获取所有。内部采用游标实现。
//指定类型可以是 none, string, set, zset, list, hash,忽略大小写,请不要添加任何空格。类型为""表示获取所有类型。
//模式为""表示所有模式。
bool getKeys(const std::string& yourType, const std::string& pattern,