Spring Boot对缓存的支持

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chengqiuming/article/details/83217009

一 点睛

在Spring中使用缓存技术的关键是配置CacheManager,而Spring Boot为我们自动配置了多个CacheManager。

配置路径如下:

通过该图,可以看出,Spring Boot为我们自动配置了

  • GenericCacheConfiguration(使用Collection)
  • GuavaCacheConfiguration(使用Guava)
  • HazelcastCacheConfiguration(使用 Hazelcast)
  • InfinispanCacheConfiguration(使用Infinispan)
  • JCacheCacheConfiguration(使用JCache)
  • NoOpCacheConfiguration(不使用存储)
  • RedisCacheConfiguration(使用Redis)
  • SimpleCacheConfiguration(使用ConcurrentMap)

在不做任何额外配置的情况下,默认使用SimpleCacheConfiguration,即使用ConcurrentMapCacheManager。

二 Spring Boot的缓存配置属性缩减版源码

@ConfigurationProperties(prefix = "spring.cache")   //缓存配置的前缀使用spring.cache
public class CacheProperties {
    private CacheType type;            //可选generic、echcache、hazelcast、infinispan、jcache、redis、guava、simple、none
    private List<String> cacheNames = new ArrayList<String>();   //程序启动时创建缓存的名称
    private final EhCache ehcache = new EhCache();
    private final Hazelcast hazelcast = new Hazelcast();
    private final Infinispan infinispan = new Infinispan();
    private final JCache jcache = new JCache();
    private final Guava guava = new Guava();
    public static class EhCache {
        private Resource config;   //echcache配置文件的地址
    }

    public static class Hazelcast {
        private Resource config;  //hazelcast配置文件的地址
    }

    public static class Infinispan {
        private Resource config;   //配置文件的地址
    }

    public static class JCache {

        private Resource config;   //jcache配置文件的地址
    }

    public static class Guava {
        private String spec;   //guava specs
    }
}

三 Spring Boot缓存支持

在Spring Boot环境下,使用缓存技术只需在项目中导入相关缓存技术的依赖包,并在配置类使用@EnableCaching开启缓存支持即可。

猜你喜欢

转载自blog.csdn.net/chengqiuming/article/details/83217009