linux下Redis以及c++操做

使用不一樣的語言,redis支持不一樣的編程語言,可是調用了不一樣的redis包,例如:

java對應jedis;

php對應phpredis;

C++對應的則是hredis。

 

上篇博客已經寫過,爲了清楚的演示,再寫一遍。php

打開Redis官網,進入下載頁面,選擇一個適合本身電腦的版本下載便可,下載飛機票http://redis.io/download,下載完成後解壓、編譯、安裝,依次在終端下執行以下命令。java

  1. tar -zxvf redis-2.8.7.tar.gz
  2. cd redis-2.8.7
  3. sudo apt-get install tcl(redis測試程序須要tcl版本至少爲8.5)
  4. make 32bit(64位系統直接使用make便可)
  5. sudo make PREFIX=/usr/local/redis install(默認sudo make install將編譯生成的可執行文件拷貝到/usr/local/redis/bin目錄下;PREFIX更改一下目錄)
    把redis-2.8.7目錄中的redis.conf複製到/usr/local/redis/bin目錄,有的道友喜歡放在/bin目錄下,只要本身額能找到就能夠了。
  6. make test(用於確認安裝正確與否)

編譯生成的可執行文件有:
1. redis-server redis服務器
2. redis-cli redis客戶端
3. redis-benchmark redis性能測試工具
4. redis-check-aof aof文件修復工具
5. redis-check-dump rdb文件檢查工具
6. redis-sentinel redis集羣管理工具mysql

編譯、安裝完成後,在終端中輸入redis-server以最簡單的方式啓動redis服務端,而後在另外一個終端中輸入redis-cli來鏈接redis服務端,接下來能夠嘗試各類命令了,能夠在http://try.redis.io預習下redis的各類命令,還能夠在redis官網查看redis支持的命令。linux

須要使用C/C++操做Redis,就須要安裝C/C++ Redis Client Library,這裏我使用的是hiredis,這是官方使用的庫,並且用得人比較多,在終端下依次執行下列命令進行下載、安裝:git

 
  1. git clone https://github.com/redis/hiredis
  2. cd hiredis
  3. make
  4. sudo make install(複製生成的庫到/usr/local/lib目錄下)
  5. sudo ldconfig /usr/local/lib

hiredis是redis數據庫的C接口,目前只能在linux下使用,幾個基本的函數就能夠操做redis數據庫了。github

 

函數原型:redisContext *redisConnect(const char *ip, int port);redis

說明:該函數用來鏈接Redis數據庫,參數爲數據庫的ip地址和端口,通常redis數據庫的端口爲6379;sql

函數返回值:該函數返回一個結構體redisContext;數據庫

相似的提供了一個函數redisContext* redisConnectWithTimeout(const char *ip, int port, timeval tv),以帶有超時的方式鏈接redis服務器,同時獲取與redis鏈接的上下文對象。編程

 

函數原型:void *redisCommand(redisContext *c, const char *format, ...);

說明:該函數執行命令,就如sql數據庫中的SQL語句同樣,只是執行的是redis數據庫中的操做命令,第一個參數爲鏈接數據庫時返回的redisContext,剩下的參數爲變參,就如C標準函數printf函數同樣的變參。

函數返回值:返回值爲void*,通常強制轉換成爲redisReply類型,以便作進一步處理。

 

函數原型void freeReplyObject(void *reply);

說明:釋放redisCommand執行後返回的redisReply所佔用的內存;

 函數返回值:無。

 

函數原型:void redisFree(redisContext *c);

說明:釋放redisConnect()所產生的鏈接。

函數返回值:無。

 

下面用一個簡單的例子說明:

#include <stdio.h>  
#include <stdlib.h>  
#include <stddef.h>  
#include <stdarg.h>  
#include <string.h>  
#include <assert.h>  
#include <hiredis/hiredis.h>  
  
void doTest()  
{  
    //redis默認監聽端口爲6387 能夠再配置文件中修改  
    redisContext* c = redisConnect("127.0.0.1", 6379);  
    if ( c->err)  
    {  
        redisFree(c);  
        printf("Connect to redisServer faile\n");  
        return ;  
    }  
    printf("Connect to redisServer Success\n");  
      
    const char* command1 = "set stest1 value1";  
    redisReply* r = (redisReply*)redisCommand(c, command1);  
      
    if( NULL == r)  
    {  
        printf("Execut command1 failure\n");  
        redisFree(c);  
        return;  
    }  
    if( !(r->type == REDIS_REPLY_STATUS && strcasecmp(r->str,"OK")==0))  
    {  
        printf("Failed to execute command[%s]\n",command1);  
        freeReplyObject(r);  
        redisFree(c);  
        return;  
    }     
    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)  
    {  
        printf("Failed to execute command[%s]\n",command2);  
        freeReplyObject(r);  
        redisFree(c);  
        return;  
    }  
    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("Failed to execute command[%s]\n",command3);  
        freeReplyObject(r);  
        redisFree(c);  
        return;  
    }  
    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);  
    if ( r->type != REDIS_REPLY_NIL)  
    {  
        printf("Failed to execute command[%s]\n",command4);  
        freeReplyObject(r);  
        redisFree(c);  
        return;  
    }  
    freeReplyObject(r);  
    printf("Succeed to execute command[%s]\n", command4);     
      
      
    redisFree(c);  
      
}  
  
int main()  
{  
    doTest();  
    return 0;  
}  

  

  1. http://redis.io/:Redis官網
  2. http://redis.cn/:Redis中文官網
  3. http://try.redis.io/:在線體驗Redis
  4. https://github.com/antirez/redis:Redis開發版本源碼
  5. http://www.redisdoc.com/en/latest/:Redis命令參考
  6. http://blog.nosqlfan.com/topics/redis:Redis系類文章
  7. http://redisbook.readthedocs.org/en/latest/:Redis設計與實現
  8. https://github.com/huangz1990/annotated_redis_source:註釋版Redis源碼
相關文章
相關標籤/搜索