Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-10688

Spring cache defaults to hashcode based cache key for multiple arguments

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Duplicate
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: Caching
    • Labels:
      None
    • Last commented by a User:
      false

      Description

      If I add a @Cacheable("cachename") annotation to a method with multiple arguments and I don't specify a key, Spring will create a cache key for me. This cache key is a hash of the arguments. Since a hash is not guaranteed to be unique, I think that this is a dangerous default which will cause errors (although the errors will be rare).

      For example:
      Long.valueOf(0) and Long.valueOf(-1) both have 0 as their hashcode.

      I have attached a failing test case. The test case fails the final assertion

      org.junit.ComparisonFailure: expected:<0:[-1]> but was:<0:[0]>
      	at org.junit.Assert.assertEquals(Assert.java:123)
      	at org.junit.Assert.assertEquals(Assert.java:145)
      	at foo.bar.SpringCacheBrokenTest.testCache(SpringCacheBrokenTest.java:52)
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                pwebb Phil Webb
                Reporter:
                lanceuk Lance S
                Last updater:
                Phil Webb
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Days since last comment:
                  5 years, 22 weeks ago