Announcement Announcement Module
Collapse
No announcement yet.
Cacheable using field from returned objet as key Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Cacheable using field from returned objet as key

    Hoping this is not a crazy question and that this is the right place to ask it...

    I have two @service methods that return the same object out of MongoDB. They have different arguments - and I want to cache the returned object using the object's Id. E.g.,

    Code:
    public User getUserById(String id) { ...
    
    public User getUserByEmailAddress(String emailAddress) { ...
    Using @Cacheable I'm abale to easily cache the first one:

    Code:
    @Cacheable(value = "user", key = "#id")
    public User getUserById(String id) { ...
    I was hoping to use the @CachePut annotation to add caching to the second method - but I'm not sure how to extract the Id from the returned object:

    Code:
    @CachePut(value = "user", key = ???)
    public User getUserByEmailAddress(String emailAddress) { ...
    Is there a way to do this through SpEL? Or do I need to roll my own Cache/Manager or Aspect as I have done in the past?

    Thanks in advance.
    M
    Last edited by MAndrews; Mar 14th, 2012, 09:22 AM.
Working...
X