1、redis簡介git
Redis是一個key-value存儲系統。和 Memcached相似,可是解決了斷電後數據徹底丟失的狀況,並且她支持更多無化的value類型,除了和string外,還支持lists(鏈表)、 sets(集合)和zsets(有序集合)幾種數據類型。這些數據類型都支持push/pop、add/remove及取交集並集和差集及更豐富的操做, 並且這些操做都是原子性的。github
2、redis的安裝redis
首先從在官網下載redis源碼安裝包,下載地址:https://github.com/antirez/redis.gitsql
目前最新分支爲3.2數據庫
1 git clone -b 3.2 https://github.com/antirez/redis.git
下載完成以後進入redis目錄,編譯安裝直接是數組
makeapp
make installtcp
編譯安裝完成直接運行memcached
redis-server就能夠啓用redis服務,能夠用netstat命令查看redis監聽的端口(默認是6379)函數
redis服務起來以後我就能夠進行相應的測試看數據庫是否安裝成功:
在命令行輸入redis-cli啓用redis客戶端:
輸入一下命令測試redis是否安裝成功
127.0.0.1:6379> ping PONG 127.0.0.1:6379> set test hello OK 127.0.0.1:6379> get test "hello" 127.0.0.1:6379>
更多redis命令請參考redis命令手冊,這上面詳細介紹了redis-cli各類命令的用法
至此,redis數據庫安裝完成
3、redis c 接口庫的安裝
Redis 是一個高性能的key-value數據庫。 redis的出現,很大程度補償了memcached這類key/value存儲的不足,在部 分場合能夠對關係數據庫起到很好的補充做用。它提供了Java,C/C++,C#,PHP,JavaScript,Perl,Object-C,Python,Ruby,Erlang等客戶端,使用很方便
在這裏簡單介紹一下redis的C接口庫:
hiredis是redis的C接口庫,使用以前咱們要先下載安裝hiredis,下載地址:https://github.com/redis/hiredis.git
git clone https://github.com/redis/hiredis.git
下載以後進入hiredis目錄
make
make install
下面進行hredis庫中幾個經常使用函數介紹
(1)redisConnect函數
該函數用於鏈接redis數據庫
函數原型:
1 redisContext *redisConnect(const char *ip, int port); 2 //說明:該函數用來鏈接redis數據庫,參數爲數據庫的ip地址和端口,通常redis數據庫的端口爲6379該函數返回一個結構體redisContext。
redisContext結構體定義以下:
1 /* Context for a connection to Redis */ 2 typedef struct redisContext { 3 int err; /* Error flags, 0 when there is no error */ 4 char errstr[128]; /* String representation of error when applicable */ 5 int fd; 6 int flags; 7 char *obuf; /* Write buffer */ 8 redisReader *reader; /* Protocol reader */ 9 10 enum redisConnectionType connection_type; 11 struct timeval *timeout; 12 13 struct { 14 char *host; 15 char *source_addr; 16 int port; 17 } tcp; 18 19 struct { 20 char *path; 21 } unix_sock; 22 23 } redisContext;
(2)redisCommand函數
該函數用於執行redis的命令;
函數原型:
1 void *redisCommand(redisContext *c, const char *format, ...); 2 /*說明:該函數執行命令,就如sql數據庫中的SQL語句同樣,只是執行的是redis數據庫中的操做命令, 3 第一個參數爲鏈接數據庫時返回的redisContext, 4 後面爲可變參數列表,跟C語言printf函數相似, 5 返回值爲void*,通常強制轉換成爲redisReply類型的進行進一步的處理。*/
用法:
redisReply *reply = (redisReply*)redisCommand(c, cmd);
redisReply結構體定義以下:
1 /* This is the reply object returned by redisCommand() */ 2 typedef struct redisReply { 3 int type; /* REDIS_REPLY_* */ 4 long long integer; /* The integer when type is REDIS_REPLY_INTEGER */ 5 size_t len; /* Length of string */ 6 char *str; /* Used for both REDIS_REPLY_ERROR and REDIS_REPLY_STRING */ 7 size_t elements; /* number of elements, for REDIS_REPLY_ARRAY */ 8 struct redisReply **element; /* elements vector for REDIS_REPLY_ARRAY */ 9 } redisReply;
下面是幾種redis的常見錯誤及返回值類型
1 #define REDIS_ERR -1 2 #define REDIS_OK 0 3 #define REDIS_ERR_IO 1 /* Error in read or write */ 4 #define REDIS_ERR_EOF 3 /* End of file */ 5 #define REDIS_ERR_PROTOCOL 4 /* Protocol error */ 6 #define REDIS_ERR_OOM 5 /* Out of memory */ 7 #define REDIS_ERR_OTHER 2 /* Everything else... */ 8 9 #define REDIS_REPLY_STRING 1 //返回字符串,查看str,len字段 10 #define REDIS_REPLY_ARRAY 2 //返回一個數組,查看elements的值(數組個數),經過element[index]的方式訪問數組元素,每一個數組元素是一個redisReply對象的指針 11 #define REDIS_REPLY_INTEGER 3 //返回整數,從integer字段獲取值 12 #define REDIS_REPLY_NIL 4 //沒有數據返回 13 #define REDIS_REPLY_STATUS 5 //表示狀態,內容經過str字段查看,字符串長度是len字段 14 #define REDIS_REPLY_ERROR 6 //表示出錯,查看出錯信息,如上的str,len字段
(3)redisRelpyObject函數
還函數用於釋放redisCommand返回值redisReply所佔用的內存
1 /* Free a reply object */ 2 void freeReplyObject(void *reply) 3 //該函數用於回收釋放redisCommand返回值redisReply所佔用的內存
(4)redisFree函數
該函數用於釋放一個redis數據庫的鏈接
1 void redisFree(redisContext *c) 2 //用於釋放redisConnect產生的鏈接
下面寫一個簡單的hredis c接口的redis測試程序:
1 #include <stdio.h> 2 #include <string.h> 3 #include <stddef.h> 4 #include <stdarg.h> 5 #include <string.h> 6 #include <assert.h> 7 #include <hiredis/hiredis.h> //redis C接口庫 8 9 #define REDIS_HOST "127.0.0.1" 10 #define REDIS_PORT 6379 11 12 void redis_cli() 13 { 14 redisContext *c = NULL; 15 redisReply *r = NULL; 16 17 /*鏈接redis數據庫*/ 18 c = redisConnect(REDIS_HOST, REDIS_PORT); 19 if(NULL == c) { 20 printf("connect redis server failure\n"); 21 return; 22 } 23 printf("redis connect sucess ip: %s port: %d\n", REDIS_HOST, REDIS_PORT); 24 25 /*test爲key, hello爲vlaue*/ 26 char *cmd1 = "set test hello"; 27 r = (redisReply*)redisCommand(c, cmd1); 28 if (NULL == r) { 29 printf("Redis Command error [%s]\n", cmd1); 30 redisFree(c); 31 return; 32 } 33 if (r->type == REDIS_REPLY_ERROR ) { 34 printf("Redis Command[%s], error:%s\n", cmd1, r->str); 35 freeReplyObject(r); 36 redisFree(c); 37 return; 38 } 39 printf("redis command execute success[%s]\n", cmd1); 40 freeReplyObject(r);//釋放redisCommand返回值replsy所佔用的內存 41 42 /*獲取test的值*/ 43 char *cmd2 = "get test"; 44 r = (redisReply*)redisCommand(c, cmd2); 45 if (NULL == r) { 46 printf("Redis Command error [%s]\n", cmd2); 47 redisFree(c); 48 return; 49 } 50 if (r->type == REDIS_REPLY_ERROR ) { 51 printf("Redis Command[%s], error:%s\n", cmd2, r->str); 52 freeReplyObject(r); 53 redisFree(c); 54 return; 55 } 56 printf("get test value is: %s\n", r->str); 57 freeReplyObject(r); 58 59 const char* cmd3 = "strlen test"; 60 r = (redisReply*)redisCommand(c, cmd3); 61 if ( r->type != REDIS_REPLY_INTEGER) 62 { 63 printf("Failed to execute command[%s]\n", cmd3); 64 freeReplyObject(r); 65 redisFree(c); 66 return; 67 } 68 printf("The length of 'test' is %d.\n", r->integer); 69 freeReplyObject(r); 70 71 /*釋放數據庫鏈接*/ 72 redisFree(c); 73 return; 74 } 75 76 int main(int argc, char **argv) 77 { 78 redis_cli(); 79 80 return 0; 81 }
運行結果:
[root@localhost 3rd]# ./test redis connect sucess ip: 127.0.0.1 port: 6379 redis command execute success[set test hello] get test value is: hello The length of 'test' is 5. [root@localhost 3rd]#