十分簡單的redis使用說明及效能測試

來源:互聯網
上載者:User

轉載請註明出處:http://blog.csdn.net/jmppok/article/details/18085181

redis相比很多人都知道,是一個記憶體式的key-value資料庫,存取速度極快,使用非常簡單,支援多種語言。本文對其使用進行一個簡要說明,並進行簡單測試。

1.下載與編譯

可以從redis官網下載最新的源碼包:http://www.redis.io/

編譯十分簡單make既可。

2.redis安裝與配置

實際上並不需要安裝。redis編譯後會在src目錄下產生redis-server,它是一個可執行檔,即啟動redis服務。不過它需要一個設定檔。設定檔寫法網上很多了,這裡直接給出一個樣本:

daemonize yespidfile /tmp/redis/var/redis.pidport 6379timeout 300loglevel debuglogfile /tmp/redis/var/redis.logdatabases 16save 900 1save 300 10save 60 10000rdbcompression yesdbfilename dump.rdbdir /tmp/redis/var/appendonly noappendfsync always#glueoutputbuf yes#shareobjects no#shareobjectspoolsize 1024
將其儲存為redis.conf
然後直接運行./redis-server redis.conf就可以啟動redis服務了,是不是很方便呢?

3.C/C++訪問redis

在redis源碼目錄下有一個deps目錄,下面有一個hiredis目錄。redis編譯時間會自動編譯該目錄產生libhiredis.a,通過引用hiredis.h 和 libhiredis.a就可以訪問redis了。具體步驟如下:

1)建立一個redisContext

2)通過redisContext執行命令

3)從返回redisReply中擷取所需資料

代碼如下:

redisContext *  c = redisConnect((char *)"192.168.150.135",6379);const char * pData = "this is a test";
redisReply *reply1 = (redisReply *)redisCommand(c,"SET 100 %s",pData);
freeReplyObject(reply1);


redisReply *reply2 = (redisReply *)redisCommand(c,"GET 100");
printf("%s\n",reply2->str);
freeReplyObject(reply2);
是不是非常簡單呢?

不過需要注意的是,redis接受的資料是字串,對於位元據,可以通過base64編碼來解決。具體可參看我的另一篇文章。

4.Java訪問redis

redis可以支援多種語言,當然也可以支援Java。

首先需要下載redis的java包。jedis.jar。這裡提供一個:redis的Java用戶端jedis

使用如下:

Jedis jedis = new Jedis("192.168.150.135");jedis.set("100","this is a test");String data = jedis.get("100");


5.效能測試

測試方法:向redis寫一個1M的資料,分別寫10次,讀10次,計算其耗時。分C++和Java兩個版本進行測試。

C++測試代碼

#include <stdio.h>#include "hiredis.h"#include <string.h>#include <time.h>int main(int argc, char **argv){printf("CLOCKS_PER_SEC:%d\n",CLOCKS_PER_SEC);redisContext *c;    redisReply *reply;    c = redisConnect((char *)"one-60",6379);char * pData;        reply = (redisReply *)redisCommand(c,"GET 0");int size = strlen(reply->str);pData = new char[size+1];strcpy(pData,reply->str);    freeReplyObject(reply);clock_t start, finish;start = clock();for(int i=0;i<10; i++){reply = (redisReply *)redisCommand(c,"GET %d",i);freeReplyObject(reply);}finish = clock();double duration = (double)(finish - start) / CLOCKS_PER_SEC*1000;printf("GET Time used:%f ms.\n",duration);start = clock();for(int i=0;i<10; i++){reply = (redisReply *)redisCommand(c,"SET %d %s",i,pData);freeReplyObject(reply);}finish = clock();duration = (double)(finish - start) / CLOCKS_PER_SEC*1000;printf("SET Time used:%f ms.\n",duration);delete []pData;redisFree(c);}
測試結果

CLOCKS_PER_SEC:1000000GET Time used:190.000000 ms.SET Time used:70.000000 ms.


Java測試代碼

import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.util.Date;import redis.clients.jedis.Jedis;public class JedisTest {public static void main(String[] args){Jedis jedis = new Jedis("10.100.211.232");String f = "/tmp/e2.txt.backup";try{File file = new File(f);BufferedReader reader = new BufferedReader(new FileReader(file));String data = reader.readLine();reader.close();Date start = new Date();for(int i=0; i<10; i++){jedis.set(i+"", data);}Date end = new Date();System.out.println("Set used(ms):"+(end.getTime()-start.getTime()));start = new Date();for(int i=0; i<10; i++){String v = jedis.get(i+"");}end = new Date();System.out.println("Get used(ms):"+(end.getTime()-start.getTime()));}catch (Exception e){e.printStackTrace();}jedis.disconnect();}}

測試結果

Set used(ms):1212Get used(ms):1437

6.總結

redis效率還是非常高的,讀寫1M資料的資料,耗時都在10ms左右。


相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.