【问题标题】:Redis on Java Connection PoolJava 连接池上的 Redis
【发布时间】:2019-09-16 16:24:31
【问题描述】:

我是 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 都在增加并且内存在增加。

客户 连接的客户端:149 client_longest_output_list:0 client_biggest_input_buf:0 阻塞的客户端:0

问题: 1 如何关闭/保持连接? 2 如何在 key 上设置 age/ttl?

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

【问题讨论】:

    标签: redis jedis


    【解决方案1】:

    由于我使用了try-resources,所以不需要关闭jedis pool资源。在应用程序结束时,调用 destroy 方法销毁池。

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2014-03-25
        • 1970-01-01
        • 1970-01-01
        • 2020-06-20
        • 2021-01-01
        • 2021-12-13
        • 1970-01-01
        • 2013-04-08
        • 2012-05-22
        相关资源
        最近更新 更多