Redis安裝及使用介紹

2021-07-04 11:48:48 字數 3909 閱讀 5703

tar -xvzf redis-3.0.3.tar.gz

cd redis-3.0.3

make && make install

測試執行;

開啟服務端:/usr/local/bin/redis-server (使用6379埠號)

開啟客戶端:/usr/local/bin/redis-cli

set name haha

get name

del name

exists name 

客戶端程式設計:

cd /deps/hiredis

make

cp libhiredis.so /usr/lib 

更新一下動態鏈結庫執行命令ldconfig

在/deps/hiredis建立mytest.c

#include #include #include #include #include #include #include "hiredis.h"

void dotest()

const char* command1 = "set stest1 value9";

redisreply* r = (redisreply*)rediscommand(c,command1);

//需要注意的是,如果返回的物件是null,則表示客戶端和伺服器之間出現嚴重錯誤,必須重新鏈結。

//這裡只是舉例說明,簡便起見,後面的命令就不再做這樣的判斷了。

if (null == r)

//不同的redis命令返回的資料型別不同,在獲取之前需要先判斷它的實際型別。

//至於各種命令的返回值資訊,可以參考redis的官方文件,或者檢視該系列部落格的前幾篇

//有關redis各種資料型別的部落格。:)

//字串型別的set命令的返回值的型別是redis_reply_status,然後只有當返回資訊是"ok"

//時,才表示該命令執行成功。後面的例子以此類推,就不再過多贅述了。

if (!(r->type == redis_reply_status && strcasecmp(r->str,"ok") == 0))

//由於後面重複使用該變數,所以需要提前釋放,否則記憶體洩漏。

freereplyobject(r);

printf("succeed to execute command[%s].\n",command1);

const char* command2 = "strlen stest1";

r = (redisreply*)rediscommand(c,command2);

if (r->type != redis_reply_integer)

int length = r->integer;

freereplyobject(r);

printf("the length of 'stest1' is %d.\n",length);

printf("succeed to execute command[%s].\n",command2);

const char* command3 = "get stest1";

r = (redisreply*)rediscommand(c,command3);

if (r->type != redis_reply_string)

printf("the value of 'stest1' is %s.\n",r->str);

freereplyobject(r);

printf("succeed to execute command[%s].\n",command3);

const char* command4 = "get stest2";

r = (redisreply*)rediscommand(c,command4);

//這裡需要先說明一下,由於stest2鍵並不存在,因此redis會返回空結果,這裡只是為了演示。

if (r->type != redis_reply_nil)

freereplyobject(r);

printf("succeed to execute command[%s].\n",command4);

const char* command5 = "mget stest1 stest2";

r = (redisreply*)rediscommand(c,command5);

//不論stest2存在與否,redis都會給出結果,只是第二個值為nil。

//由於有多個值返回,因為返回應答的型別是陣列型別。

if (r->type != redis_reply_array)

int i;

for (i = 0; i < r->elements; ++i)

//對於每乙個子應答,無需使用者單獨釋放,只需釋放最外部的redisreply即可。

freereplyobject(r);

printf("succeed to execute command[%s].\n",command5);

printf("begin to test pipeline.\n");

//該命令只是將待傳送的命令寫入到上下文物件的輸出緩衝區中,直到呼叫後面的

//redisgetreply命令才會批量將緩衝區中的命令寫出到redis伺服器。這樣可以

//有效的減少客戶端與伺服器之間的同步等候時間,以及網路io引起的延遲。

//至於管線的具體效能優勢,可以考慮該系列部落格中的管線主題。

redisfree(c);

return;

}*/ redisreply* reply = null;

//對pipeline返回結果的處理方式,和前面**的處理方式完全一直,這裡就不再重複給出了。

if (redis_ok != redisgetreply(c,(void**)&reply))

freereplyobject(reply);

printf("succeed to execute command[%s] with pipeline.\n",command1);

if (redis_ok != redisgetreply(c,(void**)&reply))

freereplyobject(reply);

printf("succeed to execute command[%s] with pipeline.\n",command2);

if (redis_ok != redisgetreply(c,(void**)&reply))

freereplyobject(reply);

printf("succeed to execute command[%s] with pipeline.\n",command3);

if (redis_ok != redisgetreply(c,(void**)&reply))

freereplyobject(reply);

printf("succeed to execute command[%s] with pipeline.\n",command4);

if (redis_ok != redisgetreply(c,(void**)&reply))

freereplyobject(reply);

printf("succeed to execute command[%s] with pipeline.\n",command5);

//由於所有通過pipeline提交的命令結果均已為返回,如果此時繼續呼叫redisgetreply,

//將會導致該函式阻塞並掛起當前執行緒,直到有新的通過管線提交的命令結果返回。

//最後不要忘記在退出前釋放當前連線的上下文物件。

redisfree(c);

return;}

int main()

gcc -g -o mytest mytest.c -lhiredis

redis 介紹及安裝

為了解決高併發 高可擴充套件 高可用 大資料儲存問題而產生的資料庫解決方案,就是nosql資料庫。nosql,泛指非關係型的資料庫,nosql即not only sql,它可以作為關係型資料庫的良好補充。2.1 鍵值 key value 儲存資料庫 voldemort berkeley db 典型應...

redis 安裝及配置介紹

1.2 解壓縮剛下好的redis包tar zxvf redis 5.0.3.tar.gz 1.3 進入剛才解壓好的redis資料夾中cd redis 5.0.3 1.4 make,友情提示,如果make被中斷,最好是刪除解壓好的資料夾,重新解壓縮一遍,否則可能make失敗 1.5 實驗啟動cd sr...

Redis基礎介紹及安裝

redis的特性非常多,我們先羅列出來然後一項一項介紹 速度快 支援持久化 支援多種資料結構 支援多種客戶端語言 功能豐富 使用簡單 支援主從複製 支援高可用和分布式。redis的使用場景沒有特定的乙個規定,在實際的工作動作,可以結合redis的功能特性和實際的需求使用,不侷限於某些經典的使用場景,...