windows下使用redis c++
redis是高效key-value NOSQL 数据库 代码开源
windows下使用需要使用微软在redis官方上的改进版
地址 https://redis.io/download
寻找windows的版本

https://github.com/MicrosoftArchive/redis
我这里下载的是windows版本redis 3.0版本 使用vs2017编译
由于是微软官方的redis改变版本 使用VS编译基本无问题
如图
需要注意的是hiredis工程 生成的是LIB文件供其他连接redis的项目使用
但是该工程调用了Win32_Interop项目代码,所以使用时候需要同时提供Win32_Interop.lib hiredis.lib。
此时项目编译的结果应该是 成功编译redis-server.exe 并且有Win32_Interop.lib hiredis.lib可供我们编写客户端使用。
参考 https://www.cnblogs.com/chinxi/p/6184885.html 添加模板结构
客户端的编写
新建工程。工程里面放入 Win32_Interop.lib hiredis.lib
加入 hiredis相关和Win32_Interop相关头文件
注意 hiredis.h头文件中
#include "../../src/Win32_Interop/win32_types_hiredis.h"
两个头文件的相对路径可能需要修改
代码如下
// MyRedisTest.cpp: 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <assert.h>
#include <winsock2.h>
#include <string.h>
#include <tuple>
#include <iostream>
#include <sstream>
#include <string.h>
#include"hiredis/hiredis.h"
class RedisConnect {
public:
RedisConnect() :redisCon(nullptr), reply(nullptr) {}
bool Init(const std::string& ip, int port) {
if (nullptr != redisCon) {
return false;
}
redisCon = redisConnect(ip.c_str(), port);
if (redisCon->err) {
std::cerr << "error code : " << redisCon->err << ". " << redisCon->errstr << std::endl;
return false;
}
return true;
}
void freeReply()
{
if (nullptr != reply)
{
::freeReplyObject(reply);
reply = nullptr;
}
}
template<class T,class... Args>
bool HashSet(const std::string command,T head, Args... rest) {
std::stringstream ss;
ss << command << " " << head << " " ;
return HashSetInner(ss,rest...);
}
template<typename T>
bool Set(const std::string & key, const T& value)
{
bool bret = false;
std::stringstream ss;
ss << "SET " << key << " " << value;
std::string s;
getline(ss, s);
return Set(s);
}
bool InitWithTimeout(const std::string& ip, int port, int seconds) {
if (nullptr != redisCon) {
return false;
}
struct timeval tv;
tv.tv_sec = seconds;
tv.tv_usec = 0;
redisCon = redisConnectWithTimeout(ip.c_str(), port, tv);
if (redisCon->err) {
std::cerr << "error code : " << redisCon->err << ". " << redisCon->errstr << std::endl;
return false;
}
return true;
}
~RedisConnect() {
freeReply();
if(nullptr == redisCon){
redisFree(redisCon);
redisCon = nullptr;
}
}
private:
bool HashSetInner(std::stringstream& ss)
{
std::string data;
getline(ss, data);
//std::cout << __FUNCTION__ << " " << data << std::endl;
bool bret = false;
freeReply();
reply = (redisReply*)::redisCommand(redisCon, data.c_str());
if (reply->type == REDIS_REPLY_ERROR ||
(reply->type == REDIS_REPLY_STATUS && _stricmp(reply->str, "OK") != 0))
{
if (reply->str != nullptr) {
std::cout << reply->str << std::endl;
}
std::cout << "Failed to execute " << __FUNCTION__ << std::endl << std::endl;
return bret;
}
bret = true;
return bret;
}
template<class T, class... Args>
bool HashSetInner(std::stringstream& ss, T head, Args... rest)
{
ss << head << " ";
return HashSetInner(ss, rest...);
}
bool Set(std::string data)
{
bool bret = false;
freeReply();
reply = (redisReply*)::redisCommand(redisCon, data.c_str());
if (!(reply->type == REDIS_REPLY_STATUS && _stricmp(reply->str, "OK") == 0))
{
std::cout << reply->str << std::endl;
std::cout << "Failed to execute " << __FUNCTION__ << std::endl;
return bret;
}
bret = true;
return bret;
}
redisContext* redisCon;
redisReply * reply;
};
int main()
{
RedisConnect r;
bool b = r.InitWithTimeout("127.0.0.1", 6379,1);
if (!b)
return -1;
r.Set("testtimes",1);
r.Set("float:pi", 3.14159265);
r.Set("string","test");
r.HashSet("hset", "myhash", "field1", 123.2342343);
r.HashSet("hmset", "myhash", "field1",1111,"field2","f2");
r.HashSet("hset", "myhash", "field1", 123.2342343);
r.HashSet("hmset", "myhash", "field1", 1111, "field2", "f2");
//wrong command
r.HashSet("hset", "myhash", "field1",1, 123.2342343);
r.HashSet("hmset", "myhash", "field1",1, 1111, "field2", "f2");
return 0;
}
windows下使用redis c++的更多相关文章
- Windows下安装Redis
1.首先,Redis官方是支持Linux系统的,我这里不多说,需要的可以参考:http://www.oschina.net/question/12_18065/ 2.Windows 64位下载地址:h ...
- <转>windows下安装redis
1.redis简介redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(so ...
- Windows下安装redis,并与PHP使用
一.在windows下安装redis: redis的官方网站下载地址:http://redis.io/download 进入以上网址之后,请见以下的图片操作下载redis: 第一步: 第二步:在对应的 ...
- windows下使用redis,Redis入门使用,Redis基础命令
windows下使用redis,Redis入门使用,Redis基础命令 >>>>>>>>>>>>>>>> ...
- windows下安装redis和php的redis扩展
1.redis简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(s ...
- windows下安装Redis并部署成服务
windows下安装Redis并部署成服务 Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存和消息中间件. 一:下载 下载地址: windows版本: http ...
- Windows下 搭建redis集群
Windows下搭建redis集群教程 一,redis集群介绍 Redis cluster(redis集群)是在版本3.0后才支持的架构,和其他集群一样,都是为了解决单台服务器不够用的情况,也防止了主 ...
- windows下安装redis(转)
add by zhj: redis相比memcached相比,性能上并没有绝对的优势.我们用redis的是因为它支持更多的数据类型,而且在分配给redis的内存用满了之后, redis也不会删除没有过 ...
- Windows下安装Redis及php的redis拓展教程
一.安装前必读 Windows 64位操作系统 Redis 安装包(版本3.0.5,截止2017-05-29最新redis版本为3.2.9) 注意事项: 1.在window下如果你还需安装php的re ...
- windows下配置redis集群,启动节点报错:createing server TCP listening socket *:7000:listen:Unknown error
windows下配置redis集群,启动节点报错:createing server TCP listening socket *:7000:listen:Unknown error 学习了:https ...
随机推荐
- 解决ubuntu16.04桌面左侧栏和顶部栏消失的问题
重要事情说三遍! 不要轻易重装系统! 不要轻易重装系统! 不要轻易重装系统! 问题所在:误删了unity桌面. 解决方法: $sudo apt-get install unity
- minIni: A minimal INI file parser
https://www.compuphase.com/minini.htm https://github.com/compuphase/minIni
- 并发之lock的condition接口
13.死磕Java并发-----J.U.C之Condition 12.Condition使用总结 11.Java并发编程系列之十七:Condition接口 === 13.死磕Java并发-----J. ...
- json null
{ "ResourceId": 0, "JsonKey": "Account", "GroupId": 21, &quo ...
- python第三方库Requests的基本使用
Requests 是用python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTT ...
- ActiveMQ(2)---ActiveMQ原理分析之消息发送
持久化消息和非持久化消息的发送策略 消息同步发送和异步发送 ActiveMQ支持同步.异步两种发送模式将消息发送到broker上.同步发送过程中,发送者发送一条消息会阻塞直到broker反馈一个确认消 ...
- 如何查看java的class文件
1.首先拿到javac文件 例如:test.class 2.可以使用文本编辑器用二进制的方式打开() cafe babe 0000 0034 0056 0a00 1200 3209 0010 0033 ...
- 尚硅谷springboot学习24-错误处理
1.SpringBoot默认的错误处理机制 默认效果: 1).浏览器,返回一个默认的错误页面
- postgresql数据库varchar、char、text的比较
名字 描述character varying(n), varchar(n) 变长,有长度限制character(n), char(n) 定长,不足补空白text 变长,无长度限制简单来说,varcha ...
- MYSQL性能优化(3)
优化数据库对象 1.优化表的数据类型 select * from tbl1 procedure analyse(16,256) ,会输出优化建议,结合情况优化 2.拆分表(仅Myisam) 2.1 纵 ...