openFeign接口加@Cacheable
时间: 2023-10-26 16:23:12 浏览: 163
详解Spring缓存注解@Cacheable,@CachePut , @CacheEvict使用
在使用OpenFeign接口添加@Cacheable注解时,可以通过@EnableCaching注解来创建并注册CachingCapability bean,以便Feign客户端能够识别接口上的@Cacheable注解。具体地,在OpenFeign接口的方法上添加@Cacheable注解,可以指定缓存的名称和键值。例如,在DemoClient接口的demoEndpoint方法上添加@Cacheable注解:
```java
public interface DemoClient {
@GetMapping("/demo/{filterParam}")
@Cacheable(cacheNames = "demo-cache", key = "#keyParam")
String demoEndpoint(String keyParam, @PathVariable String filterParam);
}
```
这样,在调用demoEndpoint方法时,如果参数的缓存已存在,则直接返回缓存的结果,如果缓存不存在,则会执行方法体内的逻辑,并将结果存入缓存中。通过使用@Cacheable注解,可以提高OpenFeign接口的性能和效率。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [Springcloud OpenFeign 详解](https://blog.csdn.net/leesinbad/article/details/129131951)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]
阅读全文