redis - Java操作
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