I am trying to figure out how Hystrix request caching works but am not following the wiki or end-to-end examples they provide in their docs.
Essentially I have the following HystrixCommand
subclass:
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);
}
}
So I feel like I'm going against the grain here. I believe Hystrix offers built-in caching, as is evidenced by a 'cacheKey
', but I can't find any working examples. I don't want to reinvent the wheel here and build caching into my commands if something is already provided out of the box.
So I ask: what does request caching look like with Hystrix (exactly)? How are entries added to the cache? How/when is the cache flushed? Is it configurable (expiries, max sizes, etc.)?
Per the documentation you linked to here,
You haven't implemented
getCacheKey()
,Then you also need a
HystrixRequestContext
Which is (again, per the documentation)
Then I believe you cannot change the method signature of
execute()
like that (doExecute()
isn't part of the interface) instead you pass the parameter to your command constructor and please annotateexecute
with an@Override
so you get a compiler error if you forget and then