1 类继承的方法来实现请求缓存
1.1 编写CacheCommand类
package com.study.service.hystrix;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixRequestCache;
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategyDefault;
import org.springframework.web.client.RestTemplate;
public class CacheCommand extends HystrixCommand<String> {
private Long cacheKey;
private RestTemplate restTemplate;
private Integer uid;
public CacheCommand(Long cacheKey, RestTemplate restTemplate, Integer uid) {
super(Setter.withGroupKey(
HystrixCommandGroupKey.Factory.asKey("cache-group")).andCommandKey(HystrixCommandKey.Factory.asKey("cache-test")));
this.restTemplate = restTemplate;
this.uid = uid;
this.cacheKey = cacheKey;
}
@Override
protected String run() throws Exception {
System.out.println("没有缓存,查询数据~!");
String url = "http://study-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));
}
}
1.2 在OrderService中添加getUser2方法
// 继承类的方式
public String getUser2(Integer id) {
Long cacheKey = 9999L;
CacheCommand cacheCommand = new CacheCommand(9999L, restTemplate, id);
String val = cacheCommand.execute();
return val;
}
1.3 在OrderContoller中编写测试接口
@RequestMapping("/cache")
public String cache() {
HystrixRequestContext context = HystrixRequestContext.initializeContext();
// 调用用户,查询用户信息,
String result1 = orderService.getUser2(1);
String result2 = orderService.getUser2(2);
context.close();
return "result1:" + result1 + ",result2:" + result2;
}
访问接口测试,可以发现,在cachekey相同的情况下,返回的值是相同的
如果需要不同时,直接修改cachekey可以获取,如:
修改getUser2为
// 继承类的方式
public String getUser2(Integer id) {
Long cacheKey = 9999L+id;
CacheCommand cacheCommand = new CacheCommand(9999L, restTemplate, id);
String val = cacheCommand.execute();
return val;
}
也可以直接调用清理缓存的方法,如下:
public String getUser2(Integer id) {
Long cacheKey = 9999L;
CacheCommand cacheCommand = new CacheCommand(9999L, restTemplate, id);
String val = cacheCommand.execute();
// 清空request缓存
cacheCommand.clearRequestCache();
return val;
}
2 注解方式实现请求缓存
2.1 在OrderService中添加getUser3方法以及clearRequestCache方法
//请求缓存注解
@CacheResult
@HystrixCommand(commandKey = "cache-user")
public String getUser3(Integer id, @CacheKey Long cacheKey) {
String url = "http://study-user/user/{id}";
String info = restTemplate.getForObject(url, String.class, id);
return info;
}
//缓存清除注解
@CacheRemove(commandKey = "cache-user")
@HystrixCommand
public void clearRequestCache(@CacheKey Long cacheKey) {
}
2.2 在OrderContoller中编写测试接口
@RequestMapping("/cache2")
public String cache2() {
HystrixRequestContext context = HystrixRequestContext.initializeContext();
// 调用用户,查询用户信息,
String result1 = orderService.getUser3(1, 12345L);
String result2 = orderService.getUser3(2, 12345L);
context.close();
return "cache2 result1:" + result1 + ", cache2 result2:" + result2;
}
如果要清除缓存,直接调用clearRequestCache方法。