您的位置:首页 > 数据库 > Redis

Redis,GetStart on windows

2013-06-04 11:42 405 查看
redis 一个可扩展的高性能的nosql数据库,越来越多的产品在使用它,redis是目前只能在linux下编译,这么好的东西肯定会有人移植到window平台的,点击msopentech移植的版本.

这里先说一下测试环境

系统:window xp sp3

编译器:VC6

由于Redis是独立的进程,不需要嵌入到程序,所以不涉及到兼容性,这里直接使用Redis-server的win32移植版.

但是客户端是需要嵌入到其他的程序的,所以VC6能编译,那基本上msvc所有版本的都可以使用了.

客户端代码使用hiredis,编译的时候需要做一些修改.

将long long 修改成__int64

win32fixes.c 496行由
d = _strtod_l(nptr, &leptr, clocale);
改成
d = strtod(nptr, &leptr);


删除win32fixes.c 495,494,490三行

win32fixes.c 115行的intptr_t 改成 int

新建一个win32 console工程,然后将
hiredis 目录下面的代码都放进去,新建一个test.c将一下代码复制进去,将工程由Debug SingleThread 改成Debug MutliThread(Project->Settings->C/C++ Code Generation).

#include "win32fixes.h"
#include "fmacros.h"
#include "hiredis.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <time.h>
#pragma comment(lib,"ws2_32.lib")

int main()
{
unsigned int j;
redisContext *c;
redisReply *reply;

WSADATA wsa;
struct timeval t = { 1, 500000 }; // 1.5 seconds

WSAStartup(MAKEWORD(2,2),&wsa);

c = redisConnectWithTimeout("127.0.0.1",6379, t);
if (c->err) {
printf("Connection error: %s\n", c->errstr);
exit(1);
}

/* PING server */
reply = redisCommand(c,"PING");
printf("PING: %s\n", reply->str);
freeReplyObject(reply);

/* Set a key */
reply = redisCommand(c,"SET %s %s", "foo", "hello world");
printf("SET: %s\n", reply->str);
freeReplyObject(reply);

/* Set a key using binary safe API */
reply = redisCommand(c,"SET %b %b", "bar", 3, "hello", 5);
printf("SET (binary API): %s\n", reply->str);
freeReplyObject(reply);

/* Try a GET and two INCR */
reply = redisCommand(c,"GET foo");
printf("GET foo: %s\n", reply->str);
freeReplyObject(reply);

reply = redisCommand(c,"INCR counter");
printf("INCR counter: %lld\n", reply->integer);
freeReplyObject(reply);
/* again ... */
reply = redisCommand(c,"INCR counter");
printf("INCR counter: %lld\n", reply->integer);
freeReplyObject(reply);

/* Create a list of numbers, from 0 to 9 */
reply = redisCommand(c,"DEL mylist");
freeReplyObject(reply);
for (j = 0; j < 10; j++) {
char buf[64];

snprintf(buf,64,"%d",j);
reply = redisCommand(c,"LPUSH mylist element-%s", buf);
freeReplyObject(reply);
}

/* Let's check what we have inside the list */
reply = 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);

WSACleanup();
return 0;
}




源码地址:test.rar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: