【发布时间】:2015-01-22 21:17:19
【问题描述】:
我正在尝试弄清楚 Hystrix request caching 的工作原理,但没有关注他们在文档中提供的 wiki 或端到端示例。
基本上我有以下HystrixCommand 子类:
public class GetFizzCommand extends HystrixCommand<Fizz> {
private Long id;
private Map<Long,Fizz> fizzCache = new HashMap<Long,Fizz>();
void doExecute(Long id) {
this.id = id;
execute();
}
@Override
public Fizz run() {
return getFizzSomehow();
}
@Override
public Fizz getFallback() {
// Consult a cache somehow.
// Perhaps something like a Map<Long,Fizz> where the 'id' is the key (?)
// If the 'id' exists in the cache, return it. Otherwise, give up and return
// NULL.
fizzCache.get(id);
}
}
所以我觉得我在这里违背了常规。我相信 Hystrix 提供内置缓存,'cacheKey' 证明了这一点,但我找不到任何工作示例。如果已经提供了开箱即用的东西,我不想在这里重新发明轮子并将缓存构建到我的命令中。
所以我问:Hystrix 的请求缓存是什么样的(确切地说)?如何将条目添加到缓存中?如何/何时刷新缓存?是否可配置(到期时间、最大大小等)?
【问题讨论】:
-
你可以阅读 HystrixCommand 的实现。一个起点是方法HystrixCommand.isResponseFromCache())(链接到grepcode.com)。
标签: java caching fault-tolerance hystrix