6Hystrix 清除缓存 · SpringCloud微服务实战 · 看云
6. Hystrix 清除缓存
导航
有缓存设置就必然会有缓存清除,当代数据发生变动的时候,需要清除缓存,不让会造成脏数据。
本节代码地址
6.1 新建FwHystrixCommondFlushCache类
方法里面设置了HystrixCommandKey 用来指明删除缓存的Key。通过手动执行
FwHystrixCommondFlushCache.flushCache("test");可以实现将缓存删除。
@Slf4j
public class FwHystrixCommondFlushCache extends HystrixCommand<String> {
public static final HystrixCommandKey TEST_KEY = HystrixCommandKey.Factory.asKey("TestKey");
private final String name;
protected FwHystrixCommondFlushCache(String name) {
super(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("myGroup"))
.andCommandKey(TEST_KEY));
this.name = name;
}
@Override
protected String run() {
log.info("get data,{}", this.name);
return this.name + ":" + Thread.currentThread().getName();
}
private static void flushCache(String name) {
HystrixRequestCache.getInstance(TEST_KEY,
HystrixConcurrencyStrategyDefault.getInstance()).clear(name);
}
@Override
protected String getCacheKey() {
return this.name;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
HystrixRequestContext context = HystrixRequestContext.initializeContext();
for (int i = 0; i <5 ; i++) {
FwHystrixCommondFlushCache test = new FwHystrixCommondFlushCache("test");
log.info(test.execute());
FwHystrixCommondFlushCache.flushCache("test");
}
context.shutdown();
}
}
6.2 执行main方法
14:24:12.662 [hystrix-myGroup-1] INFO com.yisu.hystrix.without.FwHystrixCommondFlushCache - get data,test
14:24:12.670 [main] INFO com.yisu.hystrix.without.FwHystrixCommondFlushCache - test:hystrix-myGroup-1
14:24:12.673 [hystrix-myGroup-2] INFO com.yisu.hystrix.without.FwHystrixCommondFlushCache - get data,test
14:24:12.674 [main] INFO com.yisu.hystrix.without.FwHystrixCommondFlushCache - test:hystrix-myGroup-2
14:24:12.675 [hystrix-myGroup-3] INFO com.yisu.hystrix.without.FwHystrixCommondFlushCache - get data,test
14:24:12.676 [main] INFO com.yisu.hystrix.without.FwHystrixCommondFlushCache - test:hystrix-myGroup-3
14:24:12.678 [hystrix-myGroup-4] INFO com.yisu.hystrix.without.FwHystrixCommondFlushCache - get data,test
14:24:12.679 [main] INFO com.yisu.hystrix.without.FwHystrixCommondFlushCache - test:hystrix-myGroup-4
14:24:12.680 [hystrix-myGroup-5] INFO com.yisu.hystrix.without.FwHystrixCommondFlushCache - get data,test
14:24:12.681 [main] INFO com.yisu.hystrix.without.FwHystrixCommondFlushCache - test:hystrix-myGroup-5
如果我们在将方法FwHystrixCommondFlushCache.flushCache("test");注释掉,可以看到run()方法仍然被执行一次,因此清缓存OK
14:30:24.144 [hystrix-myGroup-1] INFO com.yisu.hystrix.without.FwHystrixCommondFlushCache - get data,test
14:30:24.151 [main] INFO com.yisu.hystrix.without.FwHystrixCommondFlushCache - test:hystrix-myGroup-1
14:30:24.154 [main] INFO com.yisu.hystrix.without.FwHystrixCommondFlushCache - test:hystrix-myGroup-1
14:30:24.154 [main] INFO com.yisu.hystrix.without.FwHystrixCommondFlushCache - test:hystrix-myGroup-1
14:30:24.154 [main] INFO com.yisu.hystrix.without.FwHystrixCommondFlushCache - test:hystrix-myGroup-1
14:30:24.154 [main] INFO com.yisu.hystrix.without.FwHystrixCommondFlushCache - test:hystrix-myGroup-1