Redis安裝及簡單測試

來源:互聯網
上載者:User

摘要: Redis是目前業界非常受到歡迎的一個記憶體資料庫,一般用作系統的中間緩衝系統,用以提升整體商業系統的輸送量和響應速度。本文將簡要介紹安裝的主要過程以及給出一個簡要的測試代碼。


1.  系統內容和版本說明

   作業系統選用Ubuntu 14.04, Redis的版本選取目前的最新穩定版本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.  進入解壓之後的目錄,進行編譯

        cd redis-2.8.9

        sudo make

       

        說明: 如果沒有明顯的錯誤,則表示編譯成功

     d.  安裝

        sudo make install

         說明: 一般情況下,在Ubuntu系統中,都是需要使用sudo提升許可權的

      e.   在安裝成功之後,可以運行測試,確認Redis的功能是否正常

         sudo make test

        

    f.  啟動Redis服務

       尋找Redis安裝的目錄:  find /  -name 'redis*'  ------ 在根目錄下尋找名稱中含有redis的檔案

      經過尋找,發現Redis被安裝到了/usr/local/bin/目錄下。

      接下來,啟動Redis服務:

          /usr/local/bin/redis-server

       

     說明: 從以上的截圖中,可以發現啟動的連接埠為預設的6379. 使用者可以在啟動的時候,指定具體的設定檔,並在其中指定啟動的連接埠。

  g.  查看Redis進程

        ps -ef | grep redis

      

 說明: 如果可以看到進程,說明啟動正常。


3.   簡單的Redis測試程式

    讀者可以自行建立Eclipse項目,引入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));}}}

4. 總結

   本文簡要直觀介紹了Redis的安裝和部署,並基於jedis的簡單測試程式,說明了Redis的基本使用方式,更多的內容,可以查閱相關資料。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.