Skip to content

Be able to specify custom key generators on a per-cache basis [SPR-10629] #15257

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

Closed
spring-projects-issues opened this issue Jun 5, 2013 · 4 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Jun 5, 2013

Greg Nieman opened SPR-10629 and commented

Created on behalf of customer: They use a number of discrete caches in the application, and wished to use a different custom key generator for each. The current mechanism assumes one custom key generator. This can be worked around by creating a generator that delegates to the appropriate one and then using an additional annotation to specify which to use but they would like to have something a bit more unified.


Affects: 3.2.3

Issue Links:

Referenced from: commits 81c2080, 2b89c1a

0 votes, 5 watchers

@spring-projects-issues
Copy link
Collaborator Author

Phil Webb commented

Hi Greg,

Do you mean that they would like a custom key generator to be tied to each cache? So for example, given the annotations:

@Cachable("mycache")
// ...

@CachePut("mycache")
// ...

A key generator unique to "mycache" would be used?

I am guessing something similar to the CacheManager interface but for key generators?

@spring-projects-issues
Copy link
Collaborator Author

Greg Nieman commented

They wanted something like @Cachable(value="mycache", generator="custom1" ).

I believe the aim is to centralize certain key generation mechanisms, and they don't want to embed the rules in strings that exist in the context file.

The workaround I gave them used a custom KeyGenerator that simply delegated to one of a configured list of other custom generators. I used an additional annotation in conjunction with @Cacheable to feed in the custom key name. And that does work, but they would like something where they just specify that option at the same time they do the cache itself.

@spring-projects-issues
Copy link
Collaborator Author

Stéphane Nicoll commented

See PR #441

@spring-projects-issues
Copy link
Collaborator Author

Stéphane Nicoll commented

An extra keyGenerator parameter has been added to the base @Cache method annotations. This parameter holds the name of the
KeyGenerator bean to use to compute the key for that specific
caching endpoint.

This gives therefore a third way to customize the key. These are:

  1. Default KeyGenerator (global for all endpoints)
  2. The 'key' attribute of the annotation, giving the SpEL expression to use
  3. The 'keyGenerator' attribute of the annotation

The annotation attributes are therefore exclusive. Trying to specify
them both will result in an IllegalStateException.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants