Guava 11:缓存无法在5分钟内刷新

时间:2019-06-10 16:51:53

标签: java caching guava

我们在每30秒运行一次的HeartBeat线程中有下面的方法,我们引入了Guava缓存,对于ClientsDAO.getClients()如下刷新了5分钟,这样我们就不会每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

1 个答案:

答案 0 :(得分:5)

您永远不会重复使用C:\Users\jainil>pip install geoviews==1.6.2 Collecting geoviews==1.6.2 Using cached https://files.pythonhosted.org/packages/5d/76/7aa62a5dfdec25ab9f921223bd26507bb603bc5f08a4a7057d3f5e24f42b/geoviews-1.6.2-py2.py3-none-any.whl Requirement already satisfied: numpy>=1.0 in c:\users\jainil\anaconda3\lib\site-packages (from geoviews==1.6.2) (1.16.4) Requirement already satisfied: holoviews>=1.11.1 in c:\users\jainil\anaconda3\lib\site-packages (from geoviews==1.6.2) (1.12.3) Requirement already satisfied: bokeh>=1.0.0 in c:\users\jainil\anaconda3\lib\site-packages (from geoviews==1.6.2) (1.2.0) Collecting cartopy>=0.16.0 (from geoviews==1.6.2) Using cached https://files.pythonhosted.org/packages/e5/92/fe8838fa8158931906dfc4f16c5c1436b3dd2daf83592645b179581403ad/Cartopy-0.17.0.tar.gz Installing build dependencies ... done Getting requirements to build wheel ... error ERROR: Complete output from command 'C:\Users\jainil\Anaconda3\python.exe' 'C:\Users\jainil\Anaconda3\lib\site-packages\pip\_vendor\pep517\_in_process.py' get_requires_for_build_wheel 'C:\Users\jainil\AppData\Local\Temp\tmp25d9ovmd': ERROR: setup.py:171: UserWarning: Unable to determine GEOS version. Ensure you have 3.3.3 or later installed, or installation may fail. '.'.join(str(v) for v in GEOS_MIN_VERSION), )) Proj 4.9.0 must be installed. ---------------------------------------- ERROR: Command "'C:\Users\jainil\Anaconda3\python.exe' 'C:\Users\jainil\Anaconda3\lib\site-packages\pip\_vendor\pep517\_in_process.py' get_requires_for_build_wheel 'C:\Users\jainil\AppData\Local\Temp\tmp25d9ovmd'" failed with error code 1 in C:\Users\jainil\AppData\Local\Temp\pip-install-sjcurctz\cartopy ,因此它总是一个新呼叫

您将在每个呼叫中​​创建一个新的备忘供应商,因此基本上每次都会拨打一个新的呼叫,因为新的备忘供应商为空,因此会传播该呼叫以填充自身。您应该只创建一次便笺供应商,然后像这样反复调用它:

Suppliers.memoizeWithExpiration