Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9052379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:09:18+00:00 2026-06-16T13:09:18+00:00

From the spring documentation : @Cacheable(value=bookCache, key=isbn) public Book findBook(ISBN isbn, boolean checkWarehouse, boolean

  • 0

From the spring documentation :

@Cacheable(value="bookCache", key="isbn")
public Book findBook(ISBN isbn, boolean checkWarehouse, boolean includeUsed)

How can I specify @Cachable to use isbn and checkWarehouse as key?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:09:19+00:00Added an answer on June 16, 2026 at 1:09 pm

    Update: Current Spring cache implementation uses all method parameters as the cache key if not specified otherwise. If you want to use selected keys, refer to Arjan’s answer which uses SpEL list {#isbn, #includeUsed} which is the simplest way to create unique keys.

    From Spring Documentation

    The default key generation strategy changed with the release of Spring
    4.0. Earlier versions of Spring used a key generation strategy that, for multiple key parameters, only considered the hashCode() of
    parameters and not equals(); this could cause unexpected key
    collisions (see SPR-10237 for background). The new
    ‘SimpleKeyGenerator’ uses a compound key for such scenarios.

    Before Spring 4.0

    I suggest you to concat the values of the parameters in Spel expression with something like key="#checkWarehouse.toString() + #isbn.toString()"), I believe this should work as org.springframework.cache.interceptor.ExpressionEvaluator returns Object, which is later used as the key so you don’t have to provide an int in your SPEL expression.

    As for the hash code with a high collision probability – you can’t use it as the key.

    Someone in this thread has suggested to use T(java.util.Objects).hash(#p0,#p1, #p2) but it WILL NOT WORK and this approach is easy to break, for example I’ve used the data from SPR-9377 :

        System.out.println( Objects.hash("someisbn", new Integer(109), new Integer(434)));
        System.out.println( Objects.hash("someisbn", new Integer(110), new Integer(403)));
    

    Both lines print -636517714 on my environment.

    P.S. Actually in the reference documentation we have

    @Cacheable(value="books", key="T(someType).hash(#isbn)") 
    public Book findBook(ISBN isbn, boolean checkWarehouse, boolean includeUsed)
    

    I think that this example is WRONG and misleading and should be removed from the documentation, as the keys should be unique.

    P.P.S. also see https://jira.springsource.org/browse/SPR-9036 for some interesting ideas regarding the default key generation.

    I’d like to add for the sake of correctness and as an entertaining mathematical/computer science fact that unlike built-in hash, using a secure cryptographic hash function like MD5 or SHA256, due to the properties of such function IS absolutely possible for this task, but to compute it every time may be too expensive, checkout for example Dan Boneh cryptography course to learn more.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is from Spring documentation , section 9.5.4 <bean id=myAdvisor class=com.mycompany.MyAdvisor> <property name=someProperty value=Custom
From Spring Projects Issue Tracker Beans: public class JavaBean { private NestedBean nested; }
I'm using Spring @Cacheable annotation with Hazelcast 2.1 and Spring 3.1. @Cacheable(testCache) public MyObject
I need help with understanding following example with @ModelAttribute from Spring documentation: (method populatePetTypes()
The samples and documentation in question can be found here: http://static.springsource.org/spring-flex/docs/1.5.0.M1/ The reference doc
I am reading the doc Spring 3.1 security documentation extensively, but I can't find
From the Spring JDBC documentation, I know how to insert a blob using JdbcTemplate
I am considering to utilize Expression-Based Access Control from Spring Security 3.0. The documentation
I have looked up Google and Spring documentation but can not find any thing
Following various example configurations from Spring documentation as well as some forums on the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.