Can I use the same cache name for methods in another class for caching using ehcache

I created two methods in different classes and annotated them with annotation @Cacheable

and gave the same cache name as below:

class MyClass{
    @Cacheable(value="inScopeCache")
    public Products getProducts() { 
        //reading data from repository
    } 
}

class MyNewClass{
    @Cacheable(value="inScopeCache")
    public NewProducts getProducts() { 
        //reading data from repository..
    } 
}

      

Will this work? if so, how? I have correctly defined the above cache in spring xml. What happens if both methods are called at the same time?

How do I generate a key for this case, since there are no parameters for this method?

thanks Harish

+3


source to share


1 answer


It won't work, not because of the identity of the cache names, but because of the no arg methods.

Spring Key Generation Caching will end up with the same key for both methods by default , causing a collision inside the cache.



You have two options:

  • Specify the key to be used in each method
  • Using different caches
+1


source







All Articles