Results 1 to 1 of 1

Thread: Cacheable using field from returned objet as key

  1. #1
    Join Date
    Feb 2012
    Posts
    20

    Default 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 at 09:22 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •