【数据库开发】windows下使用c++调用redis
不废话,unix下c++调用 redis可以看这个:
http://blog.csdn.net/youngqj/article/details/8266177
==================================================================================
redis的官网版本并没有为vc开发提供接口,不过微软对redis好像很感兴趣,自己弄了一个 ,完整的英文说明在这里:
https://gist.github.com/MS-Interop/1439660 根据说明,一套完整下来,你就可以自己搭一个VC版本的 redis。
因为流程比较复杂,怕以后自己要用又忘记,趁记得写下来。
==========================================================================================
下面的步骤其实就是要弄出 MSOpenTech/redis(https://github.com/MSOpenTech/redis)里面的redis/msvs中的sln,链接中有下载,但我打不开,如果你能打开请无视下面的,直接使用。
或者下载这个:http://download.csdn.net/detail/biantaiwangzi/7864413
==========================================================================================
1.首先要先配置好git ,详细的内容在这里:http://www.cnblogs.com/sixbeauty/p/3954223.html
2.新建一个文件夹(名为redis_build好了),打开cmd,cd进去,使用git弄一个antirez/redis的备份。
1
|
git //github.com/antirez/redis.git |
3.接下来的几个命令照打就好:
1
2
|
cd git |
1
|
git |
4.下载redis24_win_uv.patch,(其实就是英文说明最下面那个。)必须要先下载才能执行成功。
把 (redis24_win_uv.patch) 拉到之前创建的目录redis_bulid下的redis里面,执行:
1
|
git |
如果有下面的warning提示可以忽略:
1
2
|
warning: warning: |
5.继续执行:
1
|
curl //raw.github.com/gist/1439660/d729b823a7ef50ef8ba54393675fb678e740ca4b/redis24_win_uv.patch |
到这一步执行完,在redis文件夹下面的msvs里面,我们就能得到RedisServer.sln文件。
但现在还是没用搞定。
6.下载:ftp://sourceware.org/pub/pthreads-win32/pthreads-w32-2-8-0-release.exe,执行。在redis/deps
下 建 pthreads-win32 文件夹。
6.1、把pre-built.2的include复制到 pthreads-win32里面。
6.2、把pre-built.2的lib中的 "pthreadVC2.dll"和"pthreadVC2.lib" 复制到 pthreads-win32/lib/debug 中,并把 "pthreadVC2.lib"改名为"pthread.lib" 。(如果是release版就复制到 pthreads-win32/lib/release 中)
7.现在可以打开 RedisServer.sln 编译生成了。
======================================分割线================================================
使用:
编译完成后,在msvs中的Debug中有hiredis的lib,使用它我们就能建立windows下redis的c++开发环境了:
1.配置:
a. 添加包含目录
【项目->属性->配置属性->VC++ 目录->包含目录】 中添加两个文件目录:
**/redis/src;**/redis/deps/hiredis
注:这两个文件就是刚刚我们的sln目录中的
b. 添加库目录
【项目->属性->配置属性->VC++ 目录->库目录】添加 **/redis\msvs\Debug
c. 添加依赖库
项目->属性->链接器->输入->附加依赖项->ws2_32.lib;hiredis.lib;
d. 最后把/**/redis/src/下的win32fixes.c放到项目目录下(即main.cpp文件所在位置)
2.使用:

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdarg.h>
#include <string.h>
#include <assert.h>
#include "hiredis.h" void doTest()
{
//redis默认监听端口为6387 可以再配置文件中修改
redisContext* c = redisConnect("127.0.0.1", 6379);
if ( c->err)
{
printf("Connect to redisServer faile:%s\n",c->errstr);
redisFree(c);
return ;
}
printf("Connect to redisServer Success\n"); const char* command1 = "set stest1 value1";
redisReply* r = (redisReply*)redisCommand(c, command1); if( NULL == r)
{
printf("Execut command1 failure\n");
redisFree(c);
return;
}
if( !(r->type == REDIS_REPLY_STATUS && (strcmp(r->str,"OK")==0 || strcmp(r->str,"ok")==0 ) ))
{
printf("Failed to execute command[%s]\n",command1);
freeReplyObject(r);
redisFree(c);
return;
}
freeReplyObject(r);
printf("Succeed to execute command[%s]\n", command1); const char* command2 = "strlen stest1";
r = (redisReply*)redisCommand(c, command2);
if ( r->type != REDIS_REPLY_INTEGER)
{
printf("Failed to execute command[%s]\n",command2);
freeReplyObject(r);
redisFree(c);
return;
}
int length = r->integer;
freeReplyObject(r);
printf("The length of 'stest1' is %d.\n", length);
printf("Succeed to execute command[%s]\n", command2); const char* command3 = "get stest1";
r = (redisReply*)redisCommand(c, command3);
if ( r->type != REDIS_REPLY_STRING)
{
printf("Failed to execute command[%s]\n",command3);
freeReplyObject(r);
redisFree(c);
return;
}
printf("The value of 'stest1' is %s\n", r->str);
freeReplyObject(r);
printf("Succeed to execute command[%s]\n", command3); const char* command4 = "get stest2";
r = (redisReply*)redisCommand(c, command4);
if ( r->type != REDIS_REPLY_NIL)
{
printf("Failed to execute command[%s]\n",command4);
freeReplyObject(r);
redisFree(c);
return;
}
freeReplyObject(r);
printf("Succeed to execute command[%s]\n", command4); redisFree(c); } int main()
{
WSADATA wsaData;
int nRet;
if((nRet = WSAStartup(MAKEWORD(2,2),&wsaData)) != 0){
printf("WSAStartup failed\n");
exit(0);
}
doTest();
return 0;
}

redis C接口hiredis 简单函数使用介绍:http://www.cnblogs.com/sixbeauty/p/3955581.html
参考:
Redis在Windows下编译 :http://blog.chinaunix.net/uid-15063109-id-3063848.html
Redis在Windows上编译(Visual C++2010):http://blog.sina.com.cn/s/blog_73c52fda01011c72.html
【数据库开发】windows下使用c++调用redis的更多相关文章
- windows下使用c++调用redis
不废话,unix下c++调用 redis可以看这个: http://blog.csdn.net/youngqj/article/details/8266177 ==================== ...
- windows下安装和配置redis
1.windows下安装和配置redis 1.1 下载: 官网(linux下载地址):https://redis.io/ Windows系统下载地址:https://github.com/MSOpen ...
- windows下安装,配置redis以及可视化客户端redisClient的安装及基本使用
一. Window 下安装 下载地址:https://github.com/MSOpenTech/redis/releases. Redis 支持 32 位和 64 位.这个需要根据你系统平台的实际情 ...
- Windows下安装并设置Redis
Redis对于Linux是官方支持的,安装和使用没有什么好说的,普通使用按照官方指导,5分钟以内就能搞定.详情请参考: http://redis.io/download 但有时候又想在windows下 ...
- Windows下C语言调用dll动态链接库
dll是windows下的动态链接库文件,下面记录一下在windows下如何调用C语言开发的dll动态链接库. 1.dll动态链接库的源代码 hello_dll.c #include "st ...
- windows下C语言调用系统文件选择对话框
代码片段,在windows下用C语言调用文件选择对话框,以备忘 #define DEFAULT_DIR "" char extraction_path[MAX_PATH] = DE ...
- mysql数据库在windows下安装与配置
mysql是一种开源源代码的关系型数据库系统(RDBMS),使用最常用的数据库管理语言--结构化查询语句(SQL)进行数据库管理. MySQL是开放源代码的,因此任何人都可以在General Pu ...
- MySQL 数据库在 Windows 下修复 only_full_group_by 的错误
本机上新安装了个MySQL数据库,在插入数据的时候一直提示这个错误: [Err] 1055 - Expression #1 of ORDER BY clause is not in GROUP BY ...
- windows下php7.1安装redis扩展以及redis测试使用全过程
最近做项目,需要用到redis相关知识.在Linux下,redis扩展安装起来很容易,但windows下还是会出问题的.因此,特此记下自己实践安装的整个过程,以方便后来人. 一,php中redis扩展 ...
随机推荐
- iptables 相关命令
1. 清除已有iptables规则 iptables -F iptables -X iptables -Z 2. 开放指定的端口(添加规则) iptables -A INPUT -s 127.0.0. ...
- PHP爬虫之queryList
根据queryList 自己花了一个下午的时间写了一个爬星座数据的类,完全手写.附上代码 <?php require '../vendor/autoload.php'; use QL\Query ...
- unity Assetboundle 工具
Unity Asset Bundle Browser tool https://github.com/Unity-Technologies/AssetBundles-Browser assetboun ...
- ST表(模板)「 查询区间最值 」
The Water Problem HDU - 5443 「 第一部分nlogn预处理 第二部分O(1)询问 」 #include <iostream> #include <bi ...
- 在docker容器中编译hadoop 3.1.0
在docker容器中编译hadoop 3.1.0 优点:docker安装好之后可以一键部署编译环境,不用担心各种库不兼容等问题,编译失败率低. Hadoop 3.1.0 的源代码目录下有一个 `sta ...
- [bzoj 4939][Ynoi 2016]掉进兔子洞
传送门 Description 一个长为 n 的序列 a. 有 m 个询问,每次询问三个区间,把三个区间中同时出现的数一个一个删掉,问最后三个区间剩下的数的个数和,询问独立. 注意这里删掉指的是一个一 ...
- Hadoop mapreduce过程分析
原理图: 中间结果的排序与溢出(spill)流程图 map分析: (1).输入分片(input split):在进行mapreduce之前,mapreduce首先会对输入文件进行输入分片(input ...
- ORACLE数据库误删恢复
一.表的恢复 对误删的表,只要没有使用PURGE永久删除选项,那么从flash back区恢复回来希望是挺大的.一般步骤有: 1.从flash back里查询被删除的表 select * from r ...
- docker install and minikube install
1.选择国内的云服务商,这里选择阿里云为例 curl -sSL http://acs-public-mirror.oss-cn-hangzhou.aliyuncs.com/docker-engine/ ...
- Web安全测试 — 手工安全测试方法&修改建议
常见问题 1.XSS(CrossSite Script)跨站脚本攻击 XSS(CrossSite Script)跨站脚本攻击.它指的是恶意攻击者往Web 页面里插入恶意 html代码,当用户浏览该页之 ...