佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > hiredis從安裝到項目實戰(zhàn)操作

hiredis從安裝到項目實戰(zhàn)操作

熱門標簽:呼叫中心市場需求 AI電銷 Linux服務(wù)器 服務(wù)外包 鐵路電話系統(tǒng) 百度競價排名 網(wǎng)站排名優(yōu)化 地方門戶網(wǎng)站

Hiredis是一個Redis的C客戶端庫函數(shù),基本實現(xiàn)了Redis的協(xié)議的最小集。

花個兩分鐘跟我一起配置hiredis

當我們下載了最新版redis的時候,其實就已經(jīng)自帶了C++版本的操作庫,只不過有些人沒發(fā)現(xiàn)罷了。

進入到deps->hiredis目錄下(在你的redis解壓目錄下有deps)

然后:make install

一步到位。

其實連測試函數(shù)他們都給你準備好了,在hedis文件夾中還有個文件夾,example,里面有個example.c文件。

這樣編譯,如果不會的話:首先需要把里面的頭文件改一下:#includehiredis/hiredis.h>
編譯的時候記得帶上依賴項:
gcc example.c -o example -L/usr/local/lib -lhiredis

當你運行的時候,(別給我說你不會運行:./example)如果不出意外,會跟你說依賴項找不著。
正常,教你一個治標的辦法:

在/etc/ld.so.conf.d/目錄下新建文件usr-libs.conf,內(nèi)容是:/usr/local/lib

然后使用命令/sbin/ldconfig更新一下配置即可。

這東西配置完,你虛擬機重啟之后就沒了,永久配置好像在我的另一篇博客里有,動態(tài)庫專欄下。

最后的運行效果:

redis的C/C++ API

redisContext* redisConnect(const char *ip, int port);

參數(shù)釋義:
該函數(shù)用來連接redis數(shù)據(jù)庫, 兩個參數(shù)分別是redis數(shù)據(jù)庫的ip和端口,端口號一般為6379。

void *redisCommand(redisContext *c, const char *format...);

該函數(shù)用于執(zhí)行redis數(shù)據(jù)庫中的命令,第一個參數(shù)為連接數(shù)據(jù)庫返回的redisContext,剩下的參數(shù)為變參.。

此函數(shù)的返回值為void*,但是一般會強制轉(zhuǎn)換為redisReply類型,以便做進一步的處理。

void freeReplyObject(void *reply);

釋放redisCommand執(zhí)行后返回的的redisReply所占用的內(nèi)存。

void redisFree(redisContext *c)

釋放redisConnect()所產(chǎn)生的連接。

實操代碼示例

#include stdio.h>
#include stdlib.h>
#include string.h>
#includehiredis/hiredis.h>

int main(int argc, char **argv) {
 unsigned int j, isunix = 0;
 redisContext *c;		
 redisReply *reply;		:
 const char *hostname = (argc > 1) ? argv[1] : "127.0.0.1";

 if (argc > 2) {
  if (*argv[2] == 'u' || *argv[2] == 'U') {
   isunix = 1;
   /* in this case, host is the path to the unix socket */
   printf("Will connect to unix socket @%s\n", hostname);
  }
 }

 int port = (argc > 2) ? atoi(argv[2]) : 6379;
 
	struct timeval timeout = { 1, 500000 }; // 1.5 seconds
 if (isunix) {
  c = redisConnectUnixWithTimeout(hostname, timeout);
  //該函數(shù)用來連接redis數(shù)據(jù)庫, 兩個參數(shù)分別是redis數(shù)據(jù)庫的ip和端口,端口號一般為6379。
 } else {
  c = redisConnectWithTimeout(hostname, port, timeout);
 }
 if (c == NULL || c->err) {
  if (c) {
   printf("Connection error: %s\n", c->errstr);	
   redisFree(c);	//釋放redisConnect()所產(chǎn)生的連接。
  } else {
   printf("Connection error: can't allocate redis context\n");
  }
  exit(1);
 }

	 /* PING server */
 reply = redisCommand(c,"PING");	
 //該函數(shù)用于執(zhí)行redis數(shù)據(jù)庫中的命令,第一個參數(shù)為連接數(shù)據(jù)庫返回的redisContext,剩下的參數(shù)為變參.。
	//此函數(shù)的返回值為void*,但是一般會強制轉(zhuǎn)換為redisReply類型,以便做進一步的處理。
 
 printf("PING: %s\n", reply->str);
 freeReplyObject(reply);	//釋放redisCommand執(zhí)行后返回的的redisReply所占用的內(nèi)存。

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

 /* Disconnects and frees the context */
 redisFree(c);

 return 0;
}

到此這篇關(guān)于hiredis從安裝到項目實戰(zhàn)操作的文章就介紹到這了,更多相關(guān)hiredis安裝內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

標簽:崇左 蘭州 湖南 湘潭 衡水 仙桃 銅川 黃山

巨人網(wǎng)絡(luò)通訊聲明:本文標題《hiredis從安裝到項目實戰(zhàn)操作》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    泗洪县| 新兴县| 勃利县| 九江市| 肇东市| 合江县| 沅江市| 民勤县| 西和县| 怀宁县| 太仓市| 芒康县| 启东市| 德保县| 西和县| 象山县| 贵定县| 上高县| 景洪市| 南安市| 旺苍县| 白玉县| 区。| 大丰市| 罗山县| 龙里县| 湖南省| 兴宁市| 盐津县| 蒙自县| 宁国市| 济源市| 雅安市| 信阳市| 张家港市| 迁安市| 靖西县| 汤阴县| 广南县| 庆城县| 健康|