示例代码:
```java
public static boolean setKey(String key, String value) {
try (Jedis jedis = getRedisUtils().getMasterJedisPool().getResource())
{
jedis.set(key, value);
jedis.expire(key.getBytes(), _default_expire_secondes);
return true;
} catch (Exception ex) {
return false;
}
}
public static String getKey(String key) {
try (Jedis jedis = getRedisUtils().getSlaveJedisPool().getResource())
{
return jedis.get(key);
} catch (Exception ex) {
return "";
}
}
```
在`try`后面的括号中定义`Jedis`并从连接池获取资源,`try`方法块结束后会自动调用`Jedis`的`close`方法以释放资源回到连接池。
参考的文档中都是使用`pool.returnResourceObject(redis);`方法释放资源到连接池,3.0版之后该方法已废弃,改为使用`Jedis`的`close`方法。
---
> 参考:
> 1. [jedis连接池详解(Redis)](http://tianxingzhe.blog.51cto.com/3390077/1684306) 2.[Jedis连接池的使用](http://www.cnblogs.com/linjiqin/archive/2013/06/14/3135248.html)
---