Skip to content
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 #1133

Closed
tahseenarticle opened this issue May 2, 2017 · 3 comments
Closed

Allow customization of @CacheFor cache key #1133

tahseenarticle opened this issue May 2, 2017 · 3 comments
Assignees
Milestone

Comments

@tahseenarticle
Copy link
Contributor

tahseenarticle commented May 2, 2017

At the moment when you annotate an Controller method with CacheFor. Play framework generate the cache key as follow in ActionInvoker.java line 152:

cacheKey = "urlcache:" + request.url + request.querystring;

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:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CacheFor {
    String value() default "1h";
    String id() default "";
    Class<? extends CacheKeyGenerator> generator() default DefaultCacheKeyGenerator.class;
}

Where CacheKeyGenerator is a simple interface that generate the cache key.

public interface CacheKeyGenerator {
	public String generate(Request request);
}
public class DefaultCacheKeyGenerator implements CacheKeyGenerator {
	@Override
	public String generate(Request request) {
		return "urlcache:" + request.url + request.querystring;
	}
}

Then we can modify the InvokerAction.invoke to generate the cache key based on the CacheKeyGenerator.

You can use a custom CacheKeyGenerator as follow:

@CacheFor(value = "1h", generator=SessionBasedCacheKeyGenerator.class)

If you do not specify the generator the default key generator will be used as usual.

@CacheFor(value = "1h")

PS: I am working on a pull request to this issue

@asolntsev
Copy link
Contributor

@tahseenarticle Good luck! Let us know when the PR is ready.

@asolntsev
Copy link
Contributor

@tahseenarticle Do you have some updates? Could you make a pull request? Can I help with this?

@asolntsev
Copy link
Contributor

Implemented in PR #1166

@asolntsev asolntsev added this to the 1.5.1 milestone Oct 16, 2017
@asolntsev asolntsev self-assigned this Oct 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants