看到Jedis相关代码如下:
public static Map<String, String> testPipeline(String psKey) {
Map<String, String> resultMap = new HashMap<>();
try (Jedis jedis = RedisManager.getAliJedisObject(RedisManager.DB_0)) {
Pipeline pipeline = jedis.pipelined();
//1.查询第一次
Response<Map<String, String>> responseAll = pipeline.hgetAll("0");
//2.查询第二次
Response<Map<String, String>> responseOne = pipeline.hgetAll("100024");
pipeline.sync();
resultMap.putAll(responseAll.get());
resultMap.putAll(responseOne.get());
}
return resultMap;
}
此处使用了JDK1.7特性,自动资源管理。
Java中某些资源是需要手动关闭的,如InputStream,Writes,Sockets,Sql classes等。这个新的语言特性允许try语句本身申请更多的资源, 这些资源作用于try代码块,并自动关闭。 在这个例子中,数据流会在 try 执行完毕后自动被关闭,前提是,这些可关闭的资源必须实现 java.lang.AutoCloseable 接口。
你可以定义关闭多个资源:
try (
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest))
{
// code
}
在Jedis中,也实现了AutoCloseable接口。