-
Notifications
You must be signed in to change notification settings - Fork 680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow customization of @CacheFor cache key #1166
Conversation
if ("".equals(cacheKey)) { | ||
cacheKey = "urlcache:" + request.url + request.querystring; | ||
// Generate a cache key for this request | ||
cacheKey = cacheFor.generator().newInstance().generate(request); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't it cause a bigger memory footprint because of creating a new instance of generator every time?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There two options here:
- We can maintain a cache of CacheKeyGenerator instances in the ActionInvoker.
- Push the creation of generator to be application developer responsibility by added a get() method to the CacheKeyGenerator interface.
public interface CacheKeyGenerator {
public String generate(Request request);
public CacheKeyGenerator get();
}
Line 154 will can rewitten as:
cacheKey = cacheFor.generator().get().generate(request);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DefaultCacheKeyGenerator can implement a singleton
public class DefaultCacheKeyGenerator implements CacheKeyGenerator {
private final static CacheKeyGenerator instance;
@Override
public String generate(Request request) {
return "urlcache:" + request.url + request.querystring;
}
public CacheKeyGenerator get() {
if(instance == null) {
instance = new DefaultCacheKeyGenerator();
}
return instance;
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can make quick these changes if this is what is required to get this pull request out of the door. I am also all ears for any other solutions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tahseenarticle I am not really sure if we need to change anything. I just asked :)
Probably it would be a premature optimization.
What I actually am waiting is @xael-fry review.
@xael-fry Please review the PR. From a code quality, it seems ok. Though, probably a little bit overkill... Or not? |
Please see issue #1133
At the moment when you annotate an Controller method with CacheFor. Play framework generate the cache key as follow in ActionInvoker.java line 152:
CacheFor annotation does not allow customisation of this cache key. This is useful in case you your controller shows different page based on the session. For example if you viewing different language version of the website the URL will be the same but the locale information is in the session. Having a way to customise this cache key helps in lot different cases of caching.
My proposal is to extend the @CacheFor as follow:
Where CacheKeyGenerator is a simple interface that generate the cache key.
Then we can modify the InvokerAction.invoke to generate the cache key based on the CacheKeyGenerator.
You can use a custom CacheKeyGenerator as follow:
If you do not specify the
generator
the default key generator will be used as usual.