java對redis的基本操作-1

來源:互聯網
上載者:User

標籤:

Jedis 用戶端實現

Maven pom檔案加入依賴

<dependencies>    <dependency>      <groupId>redis.clients</groupId>      <artifactId>jedis</artifactId>      <version>2.1.0</version>    </dependency>        <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>4.8.2</version>      <scope>test</scope>    </dependency>  </dependencies>  

Jedis 簡單使用

/*  * JedisTest.java  */  package com.x.java2000_wl;    import org.junit.Before;  import org.junit.Test;    import redis.clients.jedis.Jedis;    /**  * jedis 簡單使用  * @author http://blog.csdn.net/java2000_wl  * @version <b>1.0</b>  */  public class JedisSimpleTest {        private Jedis jedis;            /**      * 初始化串連      * <br>------------------------------<br>      */      @Before      public void beforeClass() {          jedis = new Jedis("127.0.0.1");          jedis.auth("java2000_wl");      }            /**      * set 新增      * <br>------------------------------<br>      */      @Test      public void testSet() {          jedis.set("blog", "java2000_wl");      }            /**      *  擷取      * <br>------------------------------<br>      */      @Test      public void testGet() {          System.out.println(jedis.get("blog"));      }            /**      * 修改key      * <br>------------------------------<br>      */      @Test      public void testRenameKey() {          jedis.rename("blog", "blog_new");      }            /**      * 按key刪除      * <br>------------------------------<br>      */      @Test      public void testDel() {          jedis.del("blog_new");      }            /**      * 擷取所有的key       * <br>------------------------------<br>      */      @Test      public void testKeys() {          System.out.println(jedis.keys("*"));      }  }  

使用commons-pool串連池

/*  * JedisPoolTest.java  */  package com.x.java2000_wl;    import java.util.ResourceBundle;    import org.junit.Assert;  import org.junit.BeforeClass;  import org.junit.Test;    import redis.clients.jedis.Jedis;  import redis.clients.jedis.JedisPool;  import redis.clients.jedis.JedisPoolConfig;    /**  * jedis Pool 操作  * @author http://blog.csdn.net/java2000_wl  * @version <b>1.0</b>  */  public class JedisPoolTest {        private static JedisPool jedisPool;            /**      * initPoolConfig      * <br>------------------------------<br>      * @return      */      private static JedisPoolConfig initPoolConfig() {          JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();          // 控制一個pool最多有多少個狀態為idle的jedis執行個體          jedisPoolConfig.setMaxActive(1000);           // 最大能夠保持空閑狀態的對象數          jedisPoolConfig.setMaxIdle(300);          // 逾時時間          jedisPoolConfig.setMaxWait(1000);          // 在borrow一個jedis執行個體時,是否提前進行alidate操作;如果為true,則得到的jedis執行個體均是可用的;          jedisPoolConfig.setTestOnBorrow(true);           // 在還會給pool時,是否提前進行validate操作          jedisPoolConfig.setTestOnReturn(true);          return jedisPoolConfig;      }            /**      * 初始化jedis串連池      * <br>------------------------------<br>      */      @BeforeClass      public static void before() {          JedisPoolConfig jedisPoolConfig = initPoolConfig();            // 屬性檔案讀取參數資訊          ResourceBundle bundle = ResourceBundle.getBundle("redis_config");          String host = bundle.getString("redis.host");          int port = Integer.valueOf(bundle.getString("redis.port"));          int timeout = Integer.valueOf(bundle.getString("redis.timeout"));          String password = bundle.getString("redis.password");          // 構造串連池          jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);      }        @Test      public void testSet() {          Jedis jedis = null;           // 從池中擷取一個jedis執行個體          try {              jedis = jedisPool.getResource();              jedis.set("blog_pool", "java2000_wl");          } catch (Exception e) {              // 銷毀對象              jedisPool.returnBrokenResource(jedis);              Assert.fail(e.getMessage());          } finally {              // 還會到串連池              jedisPool.returnResource(jedis);          }      }                   @Test      public void testGet() {          Jedis jedis = null;           try {              // 從池中擷取一個jedis執行個體              jedis = jedisPool.getResource();              System.out.println(jedis.get("blog_pool"));          } catch (Exception e) {              // 銷毀對象              jedisPool.returnBrokenResource(jedis);              Assert.fail(e.getMessage());          } finally {              // 還會到串連池              jedisPool.returnResource(jedis);          }      }  }  

切記: 當出現異常時 要銷毀對象 returnBrokenResource,  使用完之後要 還會串連returnResource

java對redis的基本操作-1

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.