1.首先介绍windows下的环境安装,
redis支持32和64位的。下载安装包和源码。
msi版本的用来直接安装redis服务,在windows服务中操作开关。
下载地址:https://github.com/MSOpenTech/redis/releases
server版:
exe版:(忽略其中的报错,这是因为集群没有启动全,单个redis已经成功启动)
这个版本可以通过编译,或者拿msi安装以后安装文件夹获取文件,都可以。
2.配置环境变量,path中添加
3.此时启动好的redist,可以通过运行redis-cli.exe来访问redis-server.exe。
4.如果想要编译源码,可以继续。
进入解压目录redis-win-3.2.100\msvs下,使用vs2017打开项目文件RedisServer.sln
这里边有客户端,服务端等,可以逐个编译。碰到的问题也做了记录。
4.1编译报错时
各种与其他库的使用冲突,请右击项目->属性->配置属性->C/C++->代码生成->运行库->改成多线程调试(/MTd)或多线程(/MT)
4.2编译时缺少release.h问题
https://github.com/redis/redis/issues/4492
4.3编译时cmd.exe已退出,代码为 9009问题
https://blog.youkuaiyun.com/yuyeshijie/article/details/80176840
删掉这些
5.windows客户端
需要下载目录下的一些文件
5.1新建一个vs工程,RedisTest
c/c++ -》常规 -》 附加包含目录
链接器 -》 常规 -》附加库目录
链接器 -》 输入 -》 附加依赖项
然后编码即可
// RedisTest.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include
#include
#include
#include
#include
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", "汉字测试");
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;
}