首页 > 编程语言 > 详细

spring cloud spring-hystrix 缓存

时间:2018-03-12 20:06:06      阅读:241      评论:0      收藏:0      [点我收藏+]
@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 "";
	}
}

  

spring cloud spring-hystrix 缓存

原文:https://www.cnblogs.com/zfzf1/p/8550680.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!