How do you configure multiple caches in Spring Boot?

In Spring Boot, you can use the @CacheConfig annotation to configure multiple caches. By using the @CacheConfig annotation, you can define cache configurations for multiple methods or classes uniformly. The specific steps are as follows:

  1. Add the @EnableCaching annotation to the main configuration class of Spring Boot or the configuration class that needs to enable caching in order to activate the caching feature.
  2. Add cache annotations such as @Cacheable, @CachePut, @CacheEvict to the methods where caching is needed, specify some cache configurations like cache name, cache key, etc.
  3. Add the @CacheConfig annotation to the class where caching needs to be enabled, specifying the default cache configuration, including the cache name and cache manager.

Below is an example code showing how to configure multiple caches in Spring Boot.

@Configuration
@EnableCaching
public class CacheConfig {
    
    @Bean
    public CacheManager cacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();
        
        // 创建缓存对象
        Cache cache1 = new ConcurrentMapCache("cache1");
        Cache cache2 = new ConcurrentMapCache("cache2");
        
        // 将缓存对象加入到缓存管理器中
        cacheManager.setCaches(Arrays.asList(cache1, cache2));
        
        return cacheManager;
    }
    
    @CacheConfig(cacheNames = "cache1")
    @Service
    public class MyService1 {
        
        @Cacheable(key = "#id")
        public String getDataFromCache(int id) {
            // 从数据库或其他地方获取数据
            return "Data from cache1";
        }
        
        @CachePut(key = "#id")
        public String updateDataInCache(int id) {
            // 更新缓存中的数据
            return "Updated data in cache1";
        }
        
        @CacheEvict(key = "#id")
        public void deleteDataFromCache(int id) {
            // 从缓存中删除数据
        }
    }
    
    @CacheConfig(cacheNames = "cache2")
    @Service
    public class MyService2 {
        
        @Cacheable(key = "#id")
        public String getDataFromCache(int id) {
            // 从数据库或其他地方获取数据
            return "Data from cache2";
        }
        
        @CachePut(key = "#id")
        public String updateDataInCache(int id) {
            // 更新缓存中的数据
            return "Updated data in cache2";
        }
        
        @CacheEvict(key = "#id")
        public void deleteDataFromCache(int id) {
            // 从缓存中删除数据
        }
    }
}

In the example above, we used the @CacheConfig annotation to specify the cache configuration for both MyService1 and MyService2 classes. MyService1 uses a cache called “cache1,” while MyService2 uses a cache called “cache2.” We also need to create Cache objects in the CacheConfig class and add them to the cache manager.

Please note that the cacheNames attribute in the @CacheConfig annotation specifies the default cache name. If no cache name is specified in a specific method, the default cache name will be used. However, if a cache name is specified in a specific method, then that specified cache name will be used.

bannerAds