Hystrix Request Caching by Example
Per the documentation you linked to here,
Request caching is enabled by implementing the
getCacheKey()
method on aHystrixCommand
object...
You haven't implemented getCacheKey()
,
@Override
protected String getCacheKey() {
return String.valueOf(id); // <-- changed from `value` in example
}
Then you also need a HystrixRequestContext
HystrixRequestContext context = HystrixRequestContext.initializeContext();
Which is (again, per the documentation)
Typically this context will be initialized and shutdown via a
ServletFilter
that wraps a user request or some other lifecycle hook.
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 annotate execute
with an @Override
so you get a compiler error if you forget and then
HystrixRequestContext context = HystrixRequestContext.initializeContext();
GetFizzCommand commandA = new GetFizzCommand(2L);
GetFizzCommand commandB = new GetFizzCommand(2L);
Fizz a = commandA.execute(); // <-- should not be cached
Fizz b = commandB.execute(); // <-- should be cached.