redis - Java操作

来源:互联网 发布:diffie hellman算法 编辑:程序博客网 时间:2024/04/28 04:10

 Jedis 客户端实现

Maven pom文件 加入依赖

[html] view plain copy
 print?
  1. <dependencies>  
  2.   <dependency>  
  3.     <groupId>redis.clients</groupId>  
  4.     <artifactId>jedis</artifactId>  
  5.     <version>2.1.0</version>  
  6.   </dependency>  
  7.     
  8.   <dependency>  
  9.     <groupId>junit</groupId>  
  10.     <artifactId>junit</artifactId>  
  11.     <version>4.8.2</version>  
  12.     <scope>test</scope>  
  13.   </dependency>  
  14. </dependencies>  

 
Jedis 简单使用

[java] view plain copy
 print?
  1. /* 
  2.  * JedisTest.java 
  3.  */  
  4. package com.x.java2000_wl;  
  5.   
  6. import org.junit.Before;  
  7. import org.junit.Test;  
  8.   
  9. import redis.clients.jedis.Jedis;  
  10.   
  11. /** 
  12.  * jedis 简单使用 
  13.  * @author http://blog.csdn.net/java2000_wl 
  14.  * @version <b>1.0</b> 
  15.  */  
  16. public class JedisSimpleTest {  
  17.   
  18.     private Jedis jedis;  
  19.       
  20.     /** 
  21.      * 初始化连接 
  22.      * <br>------------------------------<br> 
  23.      */  
  24.     @Before  
  25.     public void beforeClass() {  
  26.         jedis = new Jedis("127.0.0.1");  
  27.         jedis.auth("java2000_wl");  
  28.     }  
  29.       
  30.     /** 
  31.      * set 新增 
  32.      * <br>------------------------------<br> 
  33.      */  
  34.     @Test  
  35.     public void testSet() {  
  36.         jedis.set("blog""java2000_wl");  
  37.     }  
  38.       
  39.     /** 
  40.      *  获取 
  41.      * <br>------------------------------<br> 
  42.      */  
  43.     @Test  
  44.     public void testGet() {  
  45.         System.out.println(jedis.get("blog"));  
  46.     }  
  47.       
  48.     /** 
  49.      * 修改key 
  50.      * <br>------------------------------<br> 
  51.      */  
  52.     @Test  
  53.     public void testRenameKey() {  
  54.         jedis.rename("blog""blog_new");  
  55.     }  
  56.       
  57.     /** 
  58.      * 按key删除 
  59.      * <br>------------------------------<br> 
  60.      */  
  61.     @Test  
  62.     public void testDel() {  
  63.         jedis.del("blog_new");  
  64.     }  
  65.       
  66.     /** 
  67.      * 获取所有的key  
  68.      * <br>------------------------------<br> 
  69.      */  
  70.     @Test  
  71.     public void testKeys() {  
  72.         System.out.println(jedis.keys("*"));  
  73.     }  
  74. }  

 

使用commons-pool连接池

[java] view plain copy
 print?
  1. /* 
  2.  * JedisPoolTest.java 
  3.  */  
  4. package com.x.java2000_wl;  
  5.   
  6. import java.util.ResourceBundle;  
  7.   
  8. import org.junit.Assert;  
  9. import org.junit.BeforeClass;  
  10. import org.junit.Test;  
  11.   
  12. import redis.clients.jedis.Jedis;  
  13. import redis.clients.jedis.JedisPool;  
  14. import redis.clients.jedis.JedisPoolConfig;  
  15.   
  16. /** 
  17.  * jedis Pool 操作 
  18.  * @author http://blog.csdn.net/java2000_wl 
  19.  * @version <b>1.0</b> 
  20.  */  
  21. public class JedisPoolTest {  
  22.   
  23.     private static JedisPool jedisPool;  
  24.       
  25.     /** 
  26.      * initPoolConfig 
  27.      * <br>------------------------------<br> 
  28.      * @return 
  29.      */  
  30.     private static JedisPoolConfig initPoolConfig() {  
  31.         JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();  
  32.         // 控制一个pool最多有多少个状态为idle的jedis实例  
  33.         jedisPoolConfig.setMaxActive(1000);   
  34.         // 最大能够保持空闲状态的对象数  
  35.         jedisPoolConfig.setMaxIdle(300);  
  36.         // 超时时间  
  37.         jedisPoolConfig.setMaxWait(1000);  
  38.         // 在borrow一个jedis实例时,是否提前进行alidate操作;如果为true,则得到的jedis实例均是可用的;  
  39.         jedisPoolConfig.setTestOnBorrow(true);   
  40.         // 在还会给pool时,是否提前进行validate操作  
  41.         jedisPoolConfig.setTestOnReturn(true);  
  42.         return jedisPoolConfig;  
  43.     }  
  44.       
  45.     /** 
  46.      * 初始化jedis连接池 
  47.      * <br>------------------------------<br> 
  48.      */  
  49.     @BeforeClass  
  50.     public static void before() {  
  51.         JedisPoolConfig jedisPoolConfig = initPoolConfig();    
  52.         // 属性文件读取参数信息  
  53.         ResourceBundle bundle = ResourceBundle.getBundle("redis_config");  
  54.         String host = bundle.getString("redis.host");  
  55.         int port = Integer.valueOf(bundle.getString("redis.port"));  
  56.         int timeout = Integer.valueOf(bundle.getString("redis.timeout"));  
  57.         String password = bundle.getString("redis.password");  
  58.         // 构造连接池  
  59.         jedisPool = new JedisPool(jedisPoolConfig, host, port, timeout, password);  
  60.     }  
  61.   
  62.     @Test  
  63.     public void testSet() {  
  64.         Jedis jedis = null;   
  65.         // 从池中获取一个jedis实例  
  66.         try {  
  67.             jedis = jedisPool.getResource();  
  68.             jedis.set("blog_pool""java2000_wl");  
  69.         } catch (Exception e) {  
  70.             // 销毁对象  
  71.             jedisPool.returnBrokenResource(jedis);  
  72.             Assert.fail(e.getMessage());  
  73.         } finally {  
  74.             // 还会到连接池  
  75.             jedisPool.returnResource(jedis);  
  76.         }  
  77.     }         
  78.       
  79.     @Test  
  80.     public void testGet() {  
  81.         Jedis jedis = null;   
  82.         try {  
  83.             // 从池中获取一个jedis实例  
  84.             jedis = jedisPool.getResource();  
  85.             System.out.println(jedis.get("blog_pool"));  
  86.         } catch (Exception e) {  
  87.             // 销毁对象  
  88.             jedisPool.returnBrokenResource(jedis);  
  89.             Assert.fail(e.getMessage());  
  90.         } finally {  
  91.             // 还会到连接池  
  92.             jedisPool.returnResource(jedis);  
  93.         }  
  94.     }  
  95. }  

 切记: 当出现异常时 要销毁对象 returnBrokenResource,  使用完之后要 还会连接returnResource

0 0
原创粉丝点击