1. Custom Key Generator Spring Cached
  2. Custom Key Generator Spring Cache Tool
  3. Custom Key Generator Spring Cache 2017
  4. Custom Key Generator Spring Cachet

Just like its sibling, @CacheEvict requires specifying one (or multiple) caches that are affected by the action, allows a custom cache and key resolution or a condition to be specified but in addition, features an extra parameter allEntries which indicates whether a cache-wide eviction needs to be performed rather then just an entry one (based. Dec 18, 2014  Caching Data in Spring Using Redis. The Spring Framework provides a simple way to cache the results of method calls with little to no configuration. Implementing a Custom Key Generator. For basic purposes, the default key generation system for cached data works. The parameters to your method become the key in your cache store. 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. このクイックチュートリアルでは、Spring Cacheを使用してカスタムキージェネレータを作成する方法を説明します。 上記のモジュールの紹介については、 この記事へ を参照してください。. 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 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.

Example

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

A service bean

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

The main class

Example Project

Dependencies and Technologies Used:

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

Spring Cache is nothing but a store of key-value pairs, where values are the ones returned from @Cacheable methods, whereas, for keys there has to be some strategy to generate them. By default Spring uses a simple key generation based on the following algorithm:

  • If @Cacheable method has no arguments then SimpleKey.EMPTY is used as key.

  • If only one argument is used, then the argument instance is used as key.

  • If more than one argument is used, then an instance of SimpleKey composed of all arguments is used as key.

Note that to avoid key collisions, SimpleKey class overrides hashCode() and equals() methods and compares all parameters of the target method as shown in the following snippets:


Example

In this example, we will understand the default key generation. We will also understand why we may need custom keys in some cases and how to generate them.

Java Config

ConcurrentMapCacheManager is another Spring provided CacheManager implementation that lazily builds ConcurrentMapCache instances for each request.

A service bean

To understand default cache generation (as stated above), we are going to create a no arg, one arg and multiple args @Cacheable methods:

The main class

As seen in above native cache output, the key generation is according the rules we stated in the beginning of this tutorial.

Generating custom keys

What will happen if we create multiple @Cacheable methods having same argument types? Let's see that by creating two no-args methods:

As seen in above output, Spring attempts to retrieved the same cache value of department list (String[]) for the second method. That's because both methods map to the same key value of SimpleKey.EMPTY. To fix this issue we can define a custom key for the second method. @Cacheable method has an element key for that purpose:

@Cacheable#key allows to use Spring Expression Language (SpEL) for computing the key dynamically. Check out this list to understand what expression metadata can be used.

Custom Key Generator Spring Cached

Custom

In above example #root.methodName refers to the method which is being invoked.

Let's run ExampleMain2 again:


Also check out this to understand more scenarios where defining custom key generation is desirable.

Custom Key Generator Spring Cache Tool

Example Project

Dependencies and Technologies Used:

Custom Key Generator Spring Cache 2017

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

Custom Key Generator Spring Cachet


Coments are closed
Scroll to top