Spring Framework
  1. Spring Framework
  2. SPR-8015

Caching abstraction treats single method argument as array

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Complete
    • Affects Version/s: 3.1 M1
    • Fix Version/s: 3.1 M2
    • Component/s: None
    • Labels:
      None
    • Last commented by a User:
      true

      Description

      Given the following scenario

      class SampleClass {
      
        @Cacheable("mycache")
        public Something getSomething(String id) { ... }
      
        @CacheEvict(value = "mycache, key = "#id")
        public void updateSomething(String id, Object payload) { ... }
      }
      

      cache invalidation will not work correctly. Actually it's not working because the DefaultKeyGenerator simply hashes the given parameters as is. In case no key attribute is configured (just like in the above example) the parameters will be handed in as single-element-Object[] causing a different hash to be created than if the single object would be handed to the key generator directly. So you either have to define the key expression to create an array or alter the annotation on the caching side to @Cacheable(value = "mycache", key = "#id") which is rather repetitive.

        Activity

        Hide
        Costin Leau added a comment -

        Changed DefaultKeyGenerator implementation to return the object if only one argument is given. This might be somewhat surprising if more params are given but is more intuitive for the majority of cases (when only one param is given) in my opinion.

        Show
        Costin Leau added a comment - Changed DefaultKeyGenerator implementation to return the object if only one argument is given. This might be somewhat surprising if more params are given but is more intuitive for the majority of cases (when only one param is given) in my opinion.

          People

          • Assignee:
            Costin Leau
            Reporter:
            Oliver Gierke
            Last updater:
            Trevor Marshall
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:
              Days since last comment:
              3 years, 6 weeks, 4 days ago