qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請?jiān)L問 http://qaseven.github.io/

          Redis安裝及簡單測試

           摘要: Redis是目前業(yè)界非常受到歡迎的一個(gè)內(nèi)存數(shù)據(jù)庫,一般用作系統(tǒng)的中間緩存系統(tǒng),用以提升整體商業(yè)系統(tǒng)的吞吐量和響應(yīng)速度。本文將簡要介紹安裝的主要過程以及給出一個(gè)簡要的測試代碼。
            1.  系統(tǒng)環(huán)境和版本說明
            操作系統(tǒng)選用Ubuntu 14.04, Redis的版本選取目前的最新穩(wěn)定版本2.8.9. 客戶端選用了Redis的Java版本jedis 2.4.2.
            2.  Redis的安裝步驟
            a. 下載Redis的安裝包
            wget http://download.redis.io/releases/redis-2.8.9.tar.gz
            b. 在目錄下,解壓按照包,生成新的目錄redis-2.8.9
            tar xvfz redis-2.8.9.tar.gz
            c.  進(jìn)入解壓之后的目錄,進(jìn)行編譯
            cd redis-2.8.9
            sudo make
            說明: 如果沒有明顯的錯(cuò)誤,則表示編譯成功
            d.  安裝
            sudo make install
            說明: 一般情況下,在Ubuntu系統(tǒng)中,都是需要使用sudo提升權(quán)限的

           e.   在安裝成功之后,可以運(yùn)行測試,確認(rèn)Redis的功能是否正常
            sudo make test
            f.  啟動(dòng)Redis服務(wù)
            查找Redis安裝的目錄:  find /  -name 'redis*'  ------ 在根目錄下查找名稱中含有redis的文件
            經(jīng)過查找,發(fā)現(xiàn)Redis被安裝到了/usr/local/bin/目錄下。
            接下來,啟動(dòng)Redis服務(wù):
            /usr/local/bin/redis-server
            說明: 從以上的截圖中,可以發(fā)現(xiàn)啟動(dòng)的端口為缺省的6379. 用戶可以在啟動(dòng)的時(shí)候,指定具體的配置文件,并在其中指定啟動(dòng)的端口。
            g.  查看Redis進(jìn)程
            ps -ef | grep redis
            說明: 如果可以看到進(jìn)程,說明啟動(dòng)正常。  3.   簡單的Redis測試程序
            讀者可以自行創(chuàng)建Eclipse項(xiàng)目,引入jedis的客戶端包,測試程序如下:
          public class RedisTest {
          private Jedis jedis = null;
          private String key1 = "key1";
          private String key2 = "key2";
          public RedisTest() {
          jedis = new Jedis("localhost");
          }
          public static void main(String[] args) {
          RedisTest redisTest = new RedisTest();
          redisTest.isReachable();
          redisTest.testData();
          redisTest.delData();
          redisTest.testExpire();
          }
          public boolean isReachable() {
          boolean isReached = true;
          try {
          jedis.connect();
          jedis.ping();
          // jedis.quit();
          } catch (JedisConnectionException e) {
          e.printStackTrace();
          isReached = false;
          }
          System.out
          .println("The current Redis Server is Reachable:" + isReached);
          return isReached;
          }
          public void testData() {
          jedis.set("key1", "data1");
          System.out.println("Check status of data existing:"
          + jedis.exists(key1));
          System.out.println("Get Data key1:" + jedis.get("key1"));
          long s = jedis.sadd(key2, "data2");
          System.out.println("Add key2 Data:" + jedis.scard(key2)
          + " with status " + s);
          }
          public void delData() {
          long count = jedis.del(key1);
          System.out.println("Get Data Key1 after it is deleted:"
          + jedis.get(key1));
          }
          public void testExpire() {
          long count = jedis.expire(key2, 5);
          try {
          Thread.currentThread().sleep(6000);
          } catch (InterruptedException e) {
          e.printStackTrace();
          }
          if (jedis.exists(key2)) {
          System.out
          .println("Get Key2 in Expire Action:" + jedis.scard(key2));
          } else {
          System.out.println("Key2 is expired with value:"
          + jedis.scard(key2));
          }
          }
          }
          public class RedisTest {
          private Jedis jedis = null;
          private String key1 = "key1";
          private String key2 = "key2";
          public RedisTest() {
          jedis = new Jedis("localhost");
          }
          public static void main(String[] args) {
          RedisTest redisTest = new RedisTest();
          redisTest.isReachable();
          redisTest.testData();
          redisTest.delData();
          redisTest.testExpire();
          }
          public boolean isReachable() {
          boolean isReached = true;
          try {
          jedis.connect();
          jedis.ping();
          // jedis.quit();
          } catch (JedisConnectionException e) {
          e.printStackTrace();
          isReached = false;
          }
          System.out
          .println("The current Redis Server is Reachable:" + isReached);
          return isReached;
          }
          public void testData() {
          jedis.set("key1", "data1");
          System.out.println("Check status of data existing:"
          + jedis.exists(key1));
          System.out.println("Get Data key1:" + jedis.get("key1"));
          long s = jedis.sadd(key2, "data2");
          System.out.println("Add key2 Data:" + jedis.scard(key2)
          + " with status " + s);
          }
          public void delData() {
          long count = jedis.del(key1);
          System.out.println("Get Data Key1 after it is deleted:"
          + jedis.get(key1));
          }
          public void testExpire() {
          long count = jedis.expire(key2, 5);
          try {
          Thread.currentThread().sleep(6000);
          } catch (InterruptedException e) {
          e.printStackTrace();
          }
          if (jedis.exists(key2)) {
          System.out
          .println("Get Key2 in Expire Action:" + jedis.scard(key2));
          } else {
          System.out.println("Key2 is expired with value:"
          + jedis.scard(key2));
          }
          }
          }
            4. 總結(jié)
            本文簡要直觀介紹了Redis的安裝和部署,并基于jedis的簡單測試程序,說明了Redis的基本使用情況,更多的內(nèi)容,可以查閱相關(guān)資料。

          posted on 2014-06-03 09:59 順其自然EVO 閱讀(597) 評(píng)論(0)  編輯  收藏 所屬分類: 測試學(xué)習(xí)專欄

          <2014年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 苍溪县| 南丰县| 辉县市| 中西区| 顺平县| 瓮安县| 保山市| 化州市| 津南区| 旬邑县| 雷州市| 无棣县| 渝北区| 太谷县| 长岛县| 旬邑县| 正安县| 岢岚县| 卓资县| 区。| 衡阳市| 通道| 马公市| 翁牛特旗| 陵水| 威海市| 茂名市| 霍城县| 南昌县| 义马市| 牡丹江市| 吉水县| 洞口县| 拉萨市| 汝阳县| 红河县| 台湾省| 丹凤县| 平山县| 青龙| 合水县|