1. Customs Cache Locations
  2. Spring Cache Keygenerator Example
  3. Customkey Generator
  4. Custom Key Generator Spring Cache 2017

In the last example we saw how to use a custom KeyGenerator by using CacheConfig#keyGenerator. In this example, we will see how to register our custom KeyGenerator globally by implementing CachingConfigurer interface or by extending its adapter class CachingConfigurerSupport.

Spring boot cache key

2017 October 1. The default key generators for Spring Cache SimpleKeyGenerator and SimpleKey only consider the argument types of a method and not the method itself. This means that if you have two different methods with the same argument types (eg. In Spring Cache, you can define the cache key at global level by using the key prefix configuration in conjunction with using the default or implementing a custom key generator. Apart from that, cache key definition at the class and method level is also supported. By default a cache key is generated by a key generator that uses Arrays.deepHashCode(Object) and Arrays.deepEquals(Object, Object) on the method parameters. The cache lookup based on this key is similar to a HashMap lookup. Spring Cache - Using CachingConfigurer to register Custom KeyGenerator globally. In the last example we saw how to use a custom KeyGenerator by using CacheConfig#keyGenerator. Use the key property or a custom key generator to have more fine-grained control over your cache keys. Cacheable Annotations and Synchronization For very expensive methods, you want to optimize.


Java Config

CachingConfigurer interface is to be implemented by @Configuration classes annotated with @EnableCaching. It provides various methods to configure or customize caching abstraction.

We are also overriding cacheManager() method to register our cache provider.

The Custom KeyGenerator

Customs Cache Locations

A service bean

Spring Cache Keygenerator Example

As compare to the last example, we don't need to use @CacheConfig#keyGenerator or @Cacheable#keyGenerator anymore.

The main class

Example Project

Customkey Generator

Custom Key Generator Spring Cache 2017

Dependencies and Technologies Used:

  • spring-context 5.0.4.RELEASE: Spring Context.
  • JDK 1.8
  • Maven 3.3.9

Coments are closed
Scroll to top