I am trying to make a slightly better @Cached annotation by making it aware of the parameters of the function I call in my controllers.
so I have this Action :
public class ContextualCachedAction extends Action<ContextualCached> {
@Override
public Result call(Context ctx) throws Throwable {
try {
String key = makeKey(ctx);
Integer duration = configuration.duration();
Result result = (Result) Cache.get(key);
if (result == null) {
result = delegate.call(ctx);
//TODO find a way to cache only successful calls
Cache.set(key, result, duration);
}
return result;
} catch (RuntimeException e) {
throw e;
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
private String makeKey(Context ctx) {
//makes the key from some parameters in the ctx.request()
}
}
My question is this : I would like to cache the result of delegate.call() only if it is an Ok(). How can I check for that? Is there a property? a util? or do I need to Ok().getClass().isInstance(result)?
Thanks for any answers and hints.
PS : Why do I want to do that? Because I have some calls that generate few types of different results. Few enough results that caching them could be an option since I don't want to
Less sucky approach:
import org.junit.*;
import static org.fest.assertions.Assertions.assertThat;
import static play.test.Helpers.*;
/* do stuff */
Result result = doSomethingWithController();
assertThat(status(result)).isEqualTo(OK);
Works as of 2.2.2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With