gpt4 book ai didi

java - Guava 11 : Cache not working with 5 minute refresh

转载 作者:行者123 更新时间:2023-11-29 07:24:34 25 4
gpt4 key购买 nike

我们在每 30 秒运行一次的 HeartBeat 线程中使用以下方法,我们为 ClientsDAO.getClients() 引入了 5 分钟刷新一次的 Guava 缓存,以便我们不会每 30 秒访问一次数据库。

private List<String> getClients() {
final Supplier<List<String>> supplier = () -> ClientsDAO.getClients();
if(Server.CACHE_REFRESH_DURATION <=0)
return supplier.get();
else{
LOG.debug("Fetching Clients from cache, duration = "+Server.CACHE_REFRESH_DURATION+". timeunit = "+Server.CACHE_REFRESH_TIMEUNIT);
return Suppliers.memoizeWithExpiration(supplier, Server.CACHE_REFRESH_DURATION, Server.CACHE_REFRESH_TIMEUNIT).get();
}
}

正如您在下面的日志中看到的那样,每次线程 HeartBeat 运行时它都会访问数据库而不是从缓存中获取它。有人可以帮我解决吗?

[Jun 10 10:16:05] pool-16-thread-1 | DEBUG | com.server.Heartbeat | Fetching Clients from cache, duration = 5. timeunit = MINUTES
[Jun 10 10:16:05] pool-16-thread-1 | DEBUG | com.server.ClientsDAO | Getting DB connection
[Jun 10 10:16:05] pool-16-thread-1 | DEBUG | com.server.ClientsDAO | Queried for Clients

[Jun 10 10:16:35] pool-16-thread-1 | DEBUG | com.server.Heartbeat | Fetching Clients from cache, duration = 5. timeunit = MINUTES
[Jun 10 10:16:35] pool-16-thread-1 | DEBUG | com.server.ClientsDAO | Getting DB connection
[Jun 10 10:16:35] pool-16-thread-1 | DEBUG | com.server.ClientsDAO | Queried for Clients

[Jun 10 10:17:05] pool-16-thread-1 | DEBUG | com.server.Heartbeat | Fetching Clients from cache, duration = 5. timeunit = MINUTES
[Jun 10 10:17:05] pool-16-thread-1 | DEBUG | com.server.ClientsDAO | Getting DB connection
[Jun 10 10:17:05] pool-16-thread-1 | DEBUG | com.server.ClientsDAO | Queried for Clients

最佳答案

你永远不会重复使用 Suppliers.memoizeWithExpiration 所以它总是一个新调用

您在每次调用时都会创建一个新的内存供应商,因此基本上每次都会进行一次新调用,因为新的内存供应商是空的,因此会传播调用以填充自身。你应该只创建一次 memoizing 供应商,然后像这样重复调用它:

private final Supplier<List<Client>> getClientsSupplier = Suppliers.memoizeWithExpiration(ClientsDao::getClients, Server.CACHE_REFRESH_DURATION, Server.CACHE_REFRESH_TIMEUNIT);

public List<Client> getClients() {
return getClientsSupplier.get();
}

关于java - Guava 11 : Cache not working with 5 minute refresh,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56530445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com