windows下redis 和 hiredis的编译与使用
編譯環(huán)境,32位windows7,VS2013
獲取redis windows版
MS Open Technologies 官方主頁(yè)
GitHub上的MSOpenTech/redis項(xiàng)目地址
編譯 redis:
1.解壓到redis(文件夾名字自定義),打開(kāi)msvs用vs 打開(kāi)RedisServer.sln項(xiàng)目,編譯全部。
2.編譯完成后在DUBUG里面找5個(gè)EXE文件,分別是redis-benchmark.exe,redis-check-aof.exe,redis-check-dump.exe,redis-cli.exe,redis-server.exe
把5個(gè)EXE復(fù)制到redis的根目錄下即可(與bin文件夾同目錄),它們的作用如下:
redis-server:Redis服務(wù)器的daemon啟動(dòng)程序
redis-cli:Redis命令行操作工具。也可以用telnet根據(jù)其純文本協(xié)議來(lái)操作
redis-benchmark:Redis性能測(cè)試工具,測(cè)試Redis在當(dāng)前系統(tǒng)下的讀寫性能
redis-check-aof:數(shù)據(jù)修復(fù)
redis-check-dump:檢查導(dǎo)出工具
3.修改redis配置文件,根目錄下的redis.conf文件
參數(shù)介紹:
daemonize:是否以后臺(tái)daemon方式運(yùn)行
pidfile:pid文件位置
port:監(jiān)聽(tīng)的端口號(hào)
timeout:請(qǐng)求超時(shí)時(shí)間
loglevel:log信息級(jí)別
logfile:log文件位置
databases:開(kāi)啟數(shù)據(jù)庫(kù)的數(shù)量
save * *:保存快照的頻率,第一個(gè)*表示多長(zhǎng)時(shí)間,第三個(gè)*表示執(zhí)行多少次寫操作。在一定時(shí)間內(nèi)執(zhí)行一定數(shù)量的寫操作時(shí),自動(dòng)保存快照。可設(shè)置多個(gè)條件。
rdbcompression:是否使用壓縮
dbfilename:數(shù)據(jù)快照文件名(只是文件名,不包括目錄)
dir:數(shù)據(jù)快照的保存目錄(這個(gè)是目錄)
appendonly:是否開(kāi)啟appendonlylog,開(kāi)啟的話每次寫操作會(huì)記一條log,這會(huì)提高數(shù)據(jù)抗風(fēng)險(xiǎn)能力,但影響效率。
appendfsync:appendonlylog如何同步到磁盤(三個(gè)選項(xiàng),分別是每次寫都強(qiáng)制調(diào)用fsync、每秒啟用一次fsync、不調(diào)用fsync等待系統(tǒng)自己同步)
4.啟動(dòng)redis
進(jìn)入redis目錄后 開(kāi)啟服務(wù) ?(注意加上redis.conf)
redis-server.exe redis.conf這個(gè)窗口要保持開(kāi)啟 ?關(guān)閉時(shí)redis服務(wù)會(huì)自動(dòng)關(guān)閉redis會(huì)自動(dòng)保存數(shù)據(jù)到硬盤?
5.測(cè)試使用
另外開(kāi)啟一個(gè)命令行窗口 進(jìn)入redis目錄下 (注意修改自己的ip)
1.redis-cli.exe -h 192.168.10.61 -p 6379編譯 hiredis(其實(shí)上面編譯全部reids的時(shí)候已經(jīng)編譯過(guò)的):
1.編譯兩個(gè)lib: hiredis.lib和Win32_Interop.lib
打開(kāi)從GitHub上clone下來(lái)的文件夾,打開(kāi)里面的msvs文件夾中的RedisServer.sln
從解決方案資源管理器窗口編譯hiredis工程和Win32_Interop工程(調(diào)試的時(shí)候請(qǐng)?jiān)赿ebug模式下編譯這兩個(gè)庫(kù)),此時(shí)便會(huì)在Debug/Release文件夾下生成這兩個(gè)工程編譯的lib
2.在自己的工程中使用
(1)添加上一步編譯的這兩個(gè)lib到工程中
(2)復(fù)制GItHub redis項(xiàng)目文件夾中src/Win32_Interop下所有頭文件
(3)以及deps/hiredis下所有頭文件(其中fmacros.h用src文件夾下的fmacros.h文件替代)
(4)再?gòu)?fù)制src/Win32_Interop/win32fixes.c到自己的工程目錄,包含到工程文件中
(5)調(diào)整各個(gè)文件include的路徑
(6)示例代碼
#include <stdio.h> #include <stdlib.h> #include <string.h>#include <hiredis.h> #define NO_QFORKIMPL //這一行必須加才能正常使用 #include <Win32_Interop\win32fixes.h> #pragma comment(lib,"hiredis.lib") #pragma comment(lib,"Win32_Interop.lib")int main() {unsigned int j;redisContext *c;redisReply *reply;struct timeval timeout = { 1, 500000 }; // 1.5 secondsc = redisConnectWithTimeout((char*)"127.0.0.1", 6379, timeout);if (c->err) {printf("Connection error: %s\n", c->errstr);exit(1);}/* 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; }PS.可能會(huì)碰到的編譯錯(cuò)誤
1.必須定義入口點(diǎn),請(qǐng)?jiān)趙in32fixes.h之前加上#define NO_QFORKIMPL
2.各種與其他庫(kù)的使用沖突,請(qǐng)右擊項(xiàng)目->屬性->配置屬性->C/C++->代碼生成->運(yùn)行庫(kù)->改成多線程調(diào)試(/MTd)或多線程(/MT)
并且在右擊項(xiàng)目->屬性->配置屬性->連接器->命令行中輸入/NODEFAULTLIB:libcmt.lib
3.error C4996,各種unsafe報(bào)錯(cuò)啊,請(qǐng)右擊項(xiàng)目->屬性->配置屬性->C/C++->預(yù)處理器->預(yù)處理器定義->添加“_CRT_SECURE_NO_WARNINGS”(不帶引號(hào))
作者:Sky_Raker
出處:https://www.cnblogs.com/raker/p/4368741.html
總結(jié)
以上是生活随笔為你收集整理的windows下redis 和 hiredis的编译与使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 多问问自己想成为什么样的人
- 下一篇: 坚持,这两个字非常重要!