Java连接池上的Redis

时间:2016-02-15 11:32:19

标签: redis jedis

我是Redis的新手,目前在Redis上使用Java 8,Java EE 7和AWS Elastic Cache。 使用我的java知识,一旦任务结束,所有资源都应该关闭/返回到池中。

pool= new JedisPool(new JedisPoolConfig(),"myendpoint.aws.com",6379,Protocol.DEFAULT_TIMEOUT);
try(Jedis jedis=pool.getResource();)
{
    ListTask gt=new ListTask();
    JsonArray listofTask=gt.getTutorials();
    Map<String,String> attributes = new HashMap<>();
    attributes.put("ListofTask",listofTask.toString());
    jedis.hmset(key, attributes);
}
catch (Exception ex) {
    Logger.getLogger(RedisOperation.class.getName()).log(Level.SEVERE,"setRedisListofTaskJSON", ex);
}
pool.close();

点击信息后,我将连接客户端设置为149.这只是一个测试应用程序多次的框。每次connected_clients增加并且内存增加。

客户 connected_clients:149 client_longest_output_list:0 client_biggest_input_buf:0 blocked_clients:0

问题: 1如何关闭/保持联系? 2如何在密钥上设置年龄/ ttl?

使用JDK8,Java EE7和Redis 2.8(使用Jedis连接)。

2 个答案:

答案 0 :(得分:1)

由于我使用了try-resources,因此无需关闭jedis池资源。在应用程序结束时,通过调用destroy方法来破坏池。

try(Jedis jedis=pool.getResource();)
{    
    jedis.hmset(key, attributes);
}catch (Exception ex) {

}
pool.destroy();

原始文件 https://github.com/xetorthio/jedis/wiki/Getting-started

答案 1 :(得分:0)

您的问题2的答案是使用来自Jedis类的expire或expireat和ttl方法

相关问题