问题描述
我正在开发一个应用程序,我需要将来自相应 JSON API 调用的一堆产品信息存储到缓存 (eh-cache 3) 中。
它的工作方式是,http 请求会来并试图获取产品的信息。在内部,将调用一个方法以从上述 JSON API Web 服务获取信息。由于该方法被标记为可缓存,因此它将首先检查缓存。假设我们知道可缓存在这里是如何工作的。
现在我面临的问题是缓存内容不会刷新,直到有请求进入,将访问产品信息(因此访问缓存)。即使已达到缓存到期限制。
因此,为了解决这个问题,我使用了一个调度程序来调用清除缓存并通过调用之前用于获取产品信息的相同方法来重新填充缓存的方法。 像这样:
@Scheduled(fixedRate = 100000)
public void refreshProdInfoCache() throws Exception {
String cacheName = "prodInfoCache";
LOGGER.info("Cache Name -> " + cacheName);
cacheManager.getCache(cacheName).clear();
cacheManager.getCache(cacheName).put(cacheName,prodService.getProdInfo());
}
但是,我在运行应用程序时遇到以下异常:
java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request,or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message,your code is probably running outside of DispatcherServlet: In this case,use RequestContextListener or RequestContextFilter to expose the current request.
at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)
at org.springframework.web.context.support.WebApplicationContextUtils.currentRequestAttributes(WebApplicationContextUtils.java:312)
at org.springframework.web.context.support.WebApplicationContextUtils.access$400(WebApplicationContextUtils.java:65)
at org.springframework.web.context.support.WebApplicationContextUtils$RequestObjectFactory.getObject(WebApplicationContextUtils.java:328)
at org.springframework.web.context.support.WebApplicationContextUtils$RequestObjectFactory.getObject(WebApplicationContextUtils.java:323)
at org.springframework.beans.factory.support.AutowireUtils$ObjectFactoryDelegatingInvocationHandler.invoke(AutowireUtils.java:301)
at com.sun.proxy.$Proxy146.getHeader(Unknown Source)
以前有没有人有过这种情况的经验?从技术上讲,我需要在非高峰时段刷新缓存内容。因此需要在不等待用户请求的情况下刷新缓存。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)