码迷,mamicode.com
首页 > 编程语言 > 详细

java对redis的基本操作-1

时间:2015-05-12 01:21:36      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/Crow00/p/4496118.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!