@CacheReceive在SpringBoot中不工作 @Cacheable(value=“api”,key=“#请求”) 公共对象QueryCenter API(QCRequest请求,HttpHeaders头)抛出JSONException,ParseException{ RestTemplate RestTemplate=新RestTemplate(); setErrorHandler(新工具ResponseHandler()); Response res=新响应(); HashMap=newHashMap(); logger.info(“无缓存”); Gson Gson=新的Gson(); 字符串requestJson=gson.toJson(请求); HttpEntity requestEntity=新的HttpEntity(requestJson,标头); System.out.println(“请求主体”+请求实体); 对象响应=null; 试一试{ response=restTemplate.postForObject(QCUtils.queryURL,requestEntity,Object.class); logger.info(“第一次响应>”+响应); response=response.toString().replaceAll(“\\\\”,”); 系统输出打印项次(“最终响应”+响应); }捕获(HttpClientErrorException httpEx){ logger.info(“错误:+httpEx”); } 返回响应; } @cacheexecute(value=“api”,key=“#请求”) 公共无效重置请求(QCRequest){ //故意空白 System.out.println(“程序中的逐出……”); }
缓存工作正常,但我无法使用@cacheexecute注释。我想cacheexecute方法是在Cacheable方法之后立即调用的。@CacheReceive在SpringBoot中不工作 @Cacheable(value=“api”,key=“#请求”) 公共对象QueryCenter API(QCRequest请求,HttpHeaders头)抛出JSONException,ParseException{ RestTemplate RestTemplate=新RestTemplate(); setErrorHandler(新工具ResponseHandler()); Response res=新响应(); HashMap=newHashMap(); logger.info(“无缓存”); Gson Gson=新的Gson(); 字符串requestJson=gson.toJson(请求); HttpEntity requestEntity=新的HttpEntity(requestJson,标头); System.out.println(“请求主体”+请求实体); 对象响应=null; 试一试{ response=restTemplate.postForObject(QCUtils.queryURL,requestEntity,Object.class); logger.info(“第一次响应>”+响应); response=response.toString().replaceAll(“\\\\”,”); 系统输出打印项次(“最终响应”+响应); }捕获(HttpClientErrorException httpEx){ logger.info(“错误:+httpEx”); } 返回响应; } @cacheexecute(value=“api”,key=“#请求”) 公共无效重置请求(QCRequest){ //故意空白 System.out.println(“程序中的逐出……”); },spring,Spring,缓存工作正常,但我无法使用@cacheexecute注释。我想cacheexecute方法是在Cacheable方法之后立即调用的。 resetOnRequest()方法不会在可缓存方法(QueryCenter API)之后调用。我认为您需要切换到“aspectj”模式才能使@cacheExit正常工作 从春天开始: 处理缓存注释的默认建议模式为“代理” 只允许通过代理截取呼叫;地方的 同一类中的调用不能以这种方式被拦截。暂时 更先进的拦截模式,考虑切换到“AspectJ”。 模式与编译时或加载
resetOnRequest()方法不会在可缓存方法(QueryCenter API)之后调用。我认为您需要切换到“aspectj”模式才能使
@cacheExit
正常工作
从春天开始:
处理缓存注释的默认建议模式为“代理”
只允许通过代理截取呼叫;地方的
同一类中的调用不能以这种方式被拦截。暂时
更先进的拦截模式,考虑切换到“AspectJ”。
模式与编译时或加载时编织相结合
第二个选项是尝试将
@cacheexecute
方法移动到另一个类中。使用ehcache对我来说很有效,添加下面的Xml文件和配置文件就足以进行缓存和缓存execute
@Cacheable(value = "apis", key = "#request")
public Object queryCenterAPI(QCRequest request,HttpHeaders headers) throws JSONException, ParseException {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(new ToolsResponseHandler());
Response res=new Response();
HashMap<String,String> map=new HashMap<String,String>();
logger.info("No Caching^^^^^^^^^^");
Gson gson = new Gson();
String requestJson = gson.toJson(request);
HttpEntity<String> requestEntity = new HttpEntity<String>(requestJson, headers);
System.out.println("Request Body "+requestEntity);
Object response = null;
try {
response = restTemplate.postForObject(QCUtils.queryURL, requestEntity, Object.class);
logger.info("1st response>"+response);
response = response.toString().replaceAll("\\\\", "");
System.out.println("Final response "+response);
}catch (HttpClientErrorException httpEx) {
logger.info("Error:"+httpEx);
}
return response;
}
@CacheEvict(value = "apis", key = "#request")
public void resetOnRequest(QCRequest request) {
// Intentionally blank
System.out.println("Evict in Progrsss......");
}
公共类AppConfig{
@豆子
公共缓存管理器缓存管理器(){
返回新的EhcacheManager(EhcacheManager().getObject());
}
@豆子
公共EhCacheManagerFactoryBean EHCacheManager(){
EhCacheManagerFactoryBean cmfb=新的EhCacheManagerFactoryBean();
setConfigLocation(新类路径资源(“ehcache.xml”);
cmfb.setShared(真);
返回cmfb;
}
}
我们只需要在xml文件中进行配置,并给出缓存逐出发生的时间。应该从其他类调用缓存逐出方法,否则它将无法工作,对于带有@Cacheable注释的方法也是如此。如果是,我必须创建一个单独的类,如MyObjectInvalidator,然后@CacheExit开始工作
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="true"
monitoring="autodetect"
dynamicConfig="true">
<diskStore path="java.io.tmpdir" />
<cache name="apis"
eternal="false"
maxEntriesLocalHeap="10000"
maxEntriesLocalDisk="1000"
diskSpoolBufferSizeMB="20"
timeToIdleSeconds="200" timeToLiveSeconds="900"
memoryStoreEvictionPolicy="LFU"
transactionalMode="off">
</cache>
public class AppConfig {
@Bean
public CacheManager cacheManager() {
return new EhCacheCacheManager(ehCacheCacheManager().getObject());
}
@Bean
public EhCacheManagerFactoryBean ehCacheCacheManager() {
EhCacheManagerFactoryBean cmfb = new EhCacheManagerFactoryBean();
cmfb.setConfigLocation(new ClassPathResource("ehcache.xml"));
cmfb.setShared(true);
return cmfb;
}
}