有两种方式: 第一种是基于类的继承
第二种是基于注解方式
下面讲解第一种方式:
基于类的继承 继承HystrixCommand
public class CacheCommand extends HystrixCommand<String> {
private Long cacheKey; //key key值一样就不会重新查 而是调用缓存
private RestTemplate restTemplate;
private Integer uid; //需要查询的userid
public CacheCommand(Long id, RestTemplate restTemplate,Integer uid) {
super(Setter.withGroupKey(
HystrixCommandGroupKey.Factory.asKey("cache-group")
).andCommandKey(HystrixCommandKey.Factory.asKey("cache-test"))
);
this.cacheKey = cacheKey;
this.restTemplate = restTemplate;
this.uid = uid;
}
@Override
protected String run() throws Exception {
String url = "http://yidiankt-user/user/{id}";
String info = restTemplate.getForObject(url,String.class,uid);
return info;
}
@Override
protected String getCacheKey() {
return String.valueOf(cacheKey);
}
/**
* 清除缓存的方法
*/
public void clearRequestCache(){
HystrixRequestCache.getInstance(
HystrixCommandKey.Factory.asKey("cache-test"), HystrixConcurrencyStrategyDefault.getInstance()).clear(String.valueOf(cacheKey));
}
}
Service里面的调用方法
//通过继承类的方式实现的缓存处理
public String getUser2(Integer id) { //根据id进行查找
CacheCommand cacheCommand = new CacheCommand((long) 9999, restTemplate, id); //这个地方为了测试方便 写死了key
return cacheCommand.execute(); //执行
}
controller层
只要是在init HystrixRequestContext
和关闭HystrixRequestContext 之间的都是经过缓存的 有缓存的话就不会直接去查询数据库
@RequestMapping("/cache")
public String cache() throws ExecutionException, InterruptedException {
HystrixRequestContext context = HystrixRequestContext.initializeContext(); //在同一个范围内
/**
*虽然传进去的id是不同的但是 他们的private Long cacheKey;是相同的(为方便测试 规定死了 key是相同的)
* 查询之后就会去缓存 然后key是相同的话就会直接去调用缓存 而不是 再去查询
*/
String result1 = orderService.getUser2(1);
String result2 = orderService.getUser2(2);
context.close();//在同一个范围内
return "result1 = " + result1 + "result2 =" + result2;
}
下面是基于注解的方式
Service 里面的代码
//通过注解的方式实现的缓存处理
@CacheResult
@HystrixCommand(commandKey = "cache-key")
/**
* @CacheResult 该注解用来标记请求命令返回的结果应该被缓存,它必须与@HystrixCommand注解结合使用,e
*
*
*
* @CacheKey 说明这是一个key 用来判断查询的是否是同一个数据
* 写了这个之后 cachekey相同了他就不会走这个方法而是查询缓存了
*/
public String getUser3(Integer id, @CacheKey Long cachekey) {
String url = "http://yidiankt-user/user/{id}";
String info = restTemplate.getForObject(url, String.class, id);
return info;
}
注解方式需要自己写一个清空缓存的方法传进去缓存的值
但是方法体不需要自己写
/**
* 清空缓存的方法
* @CacheRemove 该注解用来让请求命令的缓存失效,失效的缓存根据定义Key决定
* @CacheKey Long cachekey 告诉他要清空的是哪一个key
*
*/
@CacheRemove(commandKey = "cache-key")
@HystrixCommand
public void clearRequestCache(@CacheKey Long cachekey) {
}
Controller里面的代码
controller层和基于继承类的方式一样的
@RequestMapping("/cache2")
public String cache2() throws ExecutionException, InterruptedException {
HystrixRequestContext context = HystrixRequestContext.initializeContext(); //在同一个范围内
/**
*虽然传进去的id是不同的但是 他们的private Long cacheKey;是相同的(为方便测试 规定死了 key是相同的)
* 查询之后就会去缓存 然后key是相同的话就会直接去调用缓存 而不是 再去查询
*/
String result1 = orderService.getUser3(1,12345L);
String result2 = orderService.getUser3(2,123465L);
String result3 = orderService.getUser3(2,123465L);
context.close();//在同一个范围内
return "result1 = " + result1 + "result2 =" + result2+"result3 =" + result3;
}