Redis,GetStart on windows

原创
2013/06/04 11:42
阅读数 1.6K

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

这里先说一下测试环境

系统:window xp sp3

编译器:VC6

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

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

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


  1. 将long long 修改成__int64
  2. win32fixes.c 496行由 
    d = _strtod_l(nptr, &leptr, clocale);
     改成 
    d = strtod(nptr, &leptr);
  3. 删除win32fixes.c 495,494,490三行
  4. 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

展开阅读全文
加载中
点击加入讨论🔥(3) 发布并加入讨论🔥
3 评论
3 收藏
0
分享
返回顶部
顶部