@Service public class CacheService { @Autowired private RestTemplate restTpl; @CacheResult @HystrixCommand public Member cacheMember(Integer id) { System.out.println("调用 cacheMember 方法"); // Member member = restTpl.getForObject( // "http://spring-hy-member/member/{id}", Member.class, id); return null; } @CacheResult @HystrixCommand(commandKey = "cacheKey") public String getCache(Integer id) { System.out.println("执行查询方法"); return null; } @CacheRemove(commandKey = "cacheKey") @HystrixCommand public void removeCache(Integer id) { System.out.println("删除缓存方法"); } }
@RestController public class CacheController { @Autowired private CacheService cacheService; @RequestMapping(value = "/cache", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public String cache() { for(int i = 0; i < 3; i++) { cacheService.cacheMember(1); } return ""; } @RequestMapping(value = "/rc", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) public String testRemoveCache() { cacheService.getCache(1); cacheService.getCache(1); cacheService.removeCache(1); System.out.println("######### 分隔线 ###########"); cacheService.getCache(1); return ""; } }