2018年10月18日 星期四

Redis on Windows Visual Studio C++



https://blog.csdn.net/LG1259156776/article/details/54645745


https://github.com/MicrosoftArchive/redis
Download and extract redis-3.0.zip
Open redis-3.0\msvs\RedisServer.sln with Visual Studio Community 2017 v15.8.3
Compile hiredis and Win32_interop projects to generate hiredis.lib and Win32_Interop.lib in redis-3.0\msvs\x64\Debug
Visual Studio > New > Visual C++ > Empty Project > Project1

1. Copy hiredis.lib and Win32_Interop.lib to Project1 folder
2. Copy *.h from redis-3.0\deps\hiredis\ to Project1\deps\hiredis

async.h
dict.h
fmacros.h
hiredis.h
net.h
sds.h
win32_hiredis.h
zmalloc.h

3. Overwrite fmacros.h from redis-3.0\src\fmacros.h to Project1\deps\hiredis\fmacros.h
4. Copy *.h from redis-3.0\src\Win32_Interop\ to Project1\src\Win32_Interop\
5. Copy win32fixes.c from redis-3.0\src\Win32_Interop\win32fixes.c to Project1\win32fixes.c
6. Select x64, Project1 > Properties > C/C++ > General > Additional Include Directories > .\deps\hiredis\;.\src\;

7. create a.cpp and compile


#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
#include <ctime>

#include "hiredis.h"
#define NO_QFORKIMPL // must add this line
#include "Win32_Interop\win32fixes.h"
#pragma comment(lib,"hiredis.lib")  
#pragma comment(lib,"Win32_Interop.lib")  

int main() {

 using namespace std;

 unsigned int j;
 redisContext *c;
 redisReply *reply;

 struct timeval timeout = { 1, 500000 }; // 1.5 seconds  
 c = redisConnectWithTimeout((char*)"127.0.0.1", 6379, timeout);
 if (c->err) {
  printf("Connection error: %s\n", c->errstr);
  exit(1);
 }

 reply = (redisReply *)redisCommand(c, "AUTH foobared");
 printf("AUTH: %s\n", reply->str);
 freeReplyObject(reply);

 clock_t begin = clock();
 for (int i = 0;i<1000; ++i) {
  reply = (redisReply *)redisCommand(c, "publish redisChat %d", i);
  //printf("publish: %s\n", reply->str);
  freeReplyObject(reply);
 }
 clock_t end = clock();
 double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
 printf("elapsed_secs = %f\n", elapsed_secs); // 0.1s for 1000 commands

 /* 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", 3, "hello", 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);

 reply = (redisReply *)redisCommand(c, "INCR counter");
 printf("INCR counter: %lld\n", reply->integer);
 freeReplyObject(reply);
 /* again ... */
 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];

  sprintf_s(buf, 64, "%d", 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);
   getchar();
  }
 }
 freeReplyObject(reply);

 return 0;

}

沒有留言:

張貼留言

2007 to 2023 HP and Dell Servers Comparison

  HP Gen5 to Gen11  using ChatGPT HP ProLiant Gen Active Years CPU Socket Popular HP CPUs Cores Base Clock Max RAM Capacity Comparable Dell ...