hiredisはインストールからプロジェクトまで実戦で操作します。


HredisはRedisのCクライアントライブラリ関数であり、基本的にRedisのプロトコルの最小セットを実現している。
二分間で私と一緒にhiredisを配置します。
最新バージョンのredisをダウンロードした時、実はすでにC++バージョンの操作ライブラリを持っています。ただ一部の人が気づかなかっただけです。
deps->hiredisディレクトリに入ります。(あなたのredis解凍ディレクトリの下にdepsがあります。)
その後:make install一歩が所定の位置につく。
実はテスト関数まで用意してくれました。hedisフォルダの中にはフォルダがあります。example、中にはexample.cファイルがあります。
このようにコンパイルして、もしできないならば、まず中の頭のファイルを変えなければなりません。
コンパイルする時は依存項を持ってきてください。
gcc example.c-o example-L/usr/local/lib-lhiredis
あなたが運行している時に、(運行しないと言わないでください)/example)事故がないと、依存項目が見つからないとあなたに言います。
正常に、標的を治す方法を教えてあげます。
ファイルのusr-libs.com nfを/etc/d.ディレクトリの下で新規作成します。内容は:/usr/local/libです。
コマンド/sbin/ldconfigで設定を更新すればいいです。
これは配置が終わったら、仮想マシンを再起動したらなくなります。永久配置は私のもう一つのブログにあります。ダイナミックライブラリのコラムの下にあります。
最後の運転効果:
在这里插入图片描述
redisのC/C++API

redisContext* redisConnect(const char *ip, int port);
パラメータの意味:
この関数はredisデータベースに接続するために使用され、二つのパラメータはそれぞれredisデータベースのipとポートであり、ポート番号は一般的に6379である。

void *redisCommand(redisContext *c, const char *format...);
この関数は、redisデータベース内のコマンドを実行するために使用されます。最初のパラメータはデータベースに接続して返したredis Contectです。残りのパラメータは参照になります。
この関数の戻り値はvoid*ですが、強制的にredisReplyタイプに変換され、さらなる処理が行われます。

void freeReplyObject(void *reply);
redisCommand実行後に戻ってきたredisReplyが占有するメモリをリリースします。

void redisFree(redisContext *c)
redisConnect()による接続をリリースします。
実働コードの例

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<hiredis/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
", hostname); } } int port = (argc > 2) ? atoi(argv[2]) : 6379; struct timeval timeout = { 1, 500000 }; // 1.5 seconds if (isunix) { c = redisConnectUnixWithTimeout(hostname, timeout); // redis , redis ip , 6379。 } else { c = redisConnectWithTimeout(hostname, port, timeout); } if (c == NULL || c->err) { if (c) { printf("Connection error: %s
", c->errstr); redisFree(c); // redisConnect() 。 } else { printf("Connection error: can't allocate redis context
"); } exit(1); } /* PING server */ reply = redisCommand(c,"PING"); // redis , redisContext, .。 // void*, redisReply , 。 printf("PING: %s
", reply->str); freeReplyObject(reply); // redisCommand redisReply 。 /* Set a key */ reply = redisCommand(c,"SET %s %s", "foo", "hello world"); printf("SET: %s
", 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
", reply->str); freeReplyObject(reply); /* Try a GET and two INCR */ reply = redisCommand(c,"GET foo"); printf("GET foo: %s
", reply->str); freeReplyObject(reply); reply = redisCommand(c,"INCR counter"); printf("INCR counter: %lld
", reply->integer); freeReplyObject(reply); /* again ... */ reply = redisCommand(c,"INCR counter"); printf("INCR counter: %lld
", 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
", j, reply->element[j]->str); } } freeReplyObject(reply); /* Disconnects and frees the context */ redisFree(c); return 0; }
この記事では、hiredisのインストールからプロジェクトの実戦操作に関する記事を紹介します。hiredisのインストール内容については、以前の文章を検索したり、下記の関連記事を見たりしてください。これからもよろしくお願いします。