1、redis服务端安装、客户端源码下载
下载地址:https://github.com/MSOpenTech/redis/releases
1)Redis 支持 32 位和 64 位。这个需要根据你系统平台的实际情况选择
2)下载Redis-x64-3.2.100.msi进行安装并启动redis服务端,这里不阐述
3)安装后服务中新增了Redis服务
4)安装路径文件列表
5)将redis 的安装路径添加到系统的环境变量
6)启动命令行窗口,连接redis服务端,增加测试验证字符串键值对
7)下载客户端源码Source code(zip),下载文件名为:redis-win-3.2.100.zip,解压
2、源码编译
1)进入解压目录redis-win-3.2.100\msvs下,使用vs2015打开项目文件RedisServer.sln
2)提示升级,确定升级即可
3)分别编译Debug和Release平台的x86和x64版本
3、新建VS2015控制台应用程序,工程名:TestRedis
1)头文件及Lib库引入说明
头文件引入路径:redis-win-3.2.100\src、redis-win-3.2.100\deps\hiredis,可把这2个目录拷贝至工程目录下
Lib库引入路径:redis-win-3.2.100\msvs\x64\Debug或redis-win-3.2.100\msvs\x64\Release
Lib库引入文件:hiredis.lib、Win32_Interop.lib,可把这2个文件拷贝至工程目录下
2)测试代码:
- // TestRedis.cpp : 定义控制台应用程序的入口点。
- //
-
- #include "stdafx.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <hiredis.h>
- //#include <Win32_Interop\win32fixes.h>
-
- int main() {
- unsigned int j;
- redisContext *c;
- redisReply *reply;
- c = redisConnect("127.0.0.1", 6379);
- if (c == NULL || c->err) {
- if (c) {
- printf("Connection error: %s\n", c->errstr);
- redisFree(c);
- }
- else {
- printf("Connection error: can't allocate redis context\n");
- }
- exit(1);
- }
- else {
- printf("connect redis successed!\n");
- }
-
- /* PING server */
- reply = (redisReply *)redisCommand(c, "PING");
- printf("PING: %s\n", reply->str);
- freeReplyObject(reply);
-
- /* Set a key */
- reply = (redisReply *)redisCommand(c, "SET %s %s", "foo", "hello world");
- printf("SET: %s\n", reply->str);
- freeReplyObject(reply);
-
- /* Set a key using binary safe API */
- reply = (redisReply *)redisCommand(c, "SET %b %b", "bar", (size_t)3, "hello", (size_t)5);
- printf("SET (binary API): %s\n", reply->str);
- freeReplyObject(reply);
-
- /* Try a GET and two INCR */
- reply = (redisReply *)redisCommand(c, "GET foo");
- printf("GET foo: %s\n", reply->str);
- freeReplyObject(reply);
-
- for (j = 0; j < 5; j++)
- {
- reply = (redisReply *)redisCommand(c, "INCR counter");
- printf("INCR counter: %lld\n", reply->integer);
- freeReplyObject(reply);
- }
-
- /* Create a list of numbers, from 0 to 9 */
- reply = (redisReply *)redisCommand(c, "DEL mylist");
- freeReplyObject(reply);
- for (j = 0; j < 10; j++) {
- char buf[64];
-
- snprintf(buf, 64, "%u", j);
- reply = (redisReply *)redisCommand(c, "LPUSH mylist element-%s", buf);
- freeReplyObject(reply);
- }
-
- /* Let's check what we have inside the list */
- reply = (redisReply *)redisCommand(c, "LRANGE mylist 0 -1");
- if (reply->type == REDIS_REPLY_ARRAY) {
- for (j = 0; j < reply->elements; j++) {
- printf("%u) %s\n", j, reply->element[j]->str);
- }
- }
- freeReplyObject(reply);
-
- /* Disconnects and frees the context */
- redisFree(c);
-
- system("pause");
- return 0;
- }
-
3)引入头文件
4)引入库目录:
5)引入库文件:
6)编译工程并运行