SpringCache使用Ehcache做缓存

SpringCache简介

一个项目随着时间的积累, 数据规模会越来越大, 对数据的访问需求也越来越高, 数据库的查询等操作的使用量也会越来越大.但是数据库的性能是有上限的.因此如何提高数据查询的性能, 分担数据库的访问压力也成为了一个重要的功能.

而缓存就是实际工作中经常会被使用的一种分担数据库访问压力,提高数据查询性能的方法.

从3.1开始Spring引入了对Cache的支持。其使用方法和原理都类似于Spring对事务管理的支持。Spring Cache是作用在方法上的,其核心思想是:当我们在调用一个缓存方法时会把该方法参数和返回结果作为一个键值存放在缓存中,等到下次利用同样的参数调用该方法时将不再执行该方法,而是直接从缓存中获取结果进行返回。

Spring Cache, 本质上不是一个具体的缓存实现方案, 而是一个对缓存使用的抽象, 只要通过一定的配置和既有的代码,注解,就可以利用各种缓存方案 如EHCache,Redis等, 来暂时存储数据.

上面这部分简介摘抄自 SpringCache简单梳理 这篇文章

要想使用SpringCache,原则上也是需要实现SpringCache提供的相关接口的。

//spring cache的缓存接口
org.springframework.cache.Cache
org.springframework.cache.CacheManager

但spring框架已经提供了这2个接口的很多实现了,本次我们想使用Ehcache做缓存,spring提供的EhCacheCache和EhCacheCacheManager分别实现了上面2个接口。

引入依赖

    <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-cacheartifactId>
        dependency>
        <dependency>
            <groupId>net.sf.ehcachegroupId>
            <artifactId>ehcacheartifactId>
        dependency>

配置ehcache.xml文件


<ehcache updateCheck="false" dynamicConfig="false">
    <diskStore path="D:/test_back_up/cach"/>

    
    <cache name="DocCache"
           maxElementsInMemory="10000"
           eternal="false"
           timeToIdleSeconds="600"
           timeToLiveSeconds="1800"
           overflowToDisk="false"
           maxElementsOnDisk="1"
           diskPersistent="false"
           diskExpiryThreadIntervalSeconds="120"
           memoryStoreEvictionPolicy="LRU"/>


    
    
    
    
    <defaultCache name="defaultCache"
                  maxElementsInMemory="10000"
                  eternal="false"
                  timeToIdleSeconds="120"
                  timeToLiveSeconds="120"
                  overflowToDisk="true"
                  maxElementsOnDisk="100000"
                  diskPersistent="false"
                  diskExpiryThreadIntervalSeconds="120"
                  memoryStoreEvictionPolicy="LRU"/>

ehcache>

配置 net.sf.ehcache.CacheManager


@Configuration
@EnableConfigurationProperties(CacheProperties.class)
public class EhcacheConfig {

    /**
     * 设置为共享模式
     */
    /*@Bean
    public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){
        EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean();
        ehCacheManagerFactoryBean.setShared(true);
        return ehCacheManagerFactoryBean;
    }*/

    @Bean
    @ConditionalOnMissingBean
    CacheManager myEhCacheCacheManager(CacheProperties cacheProperties) {
        Resource location = cacheProperties.resolveConfigLocation(cacheProperties.getEhcache().getConfig());
        if (location != null) {
            return EhCacheManagerUtils.buildCacheManager(location);
        }
        return EhCacheManagerUtils.buildCacheManager();
    }

}

配置org.springframework.cache.ehcache.EhCacheCacheManager

@Configuration
public class SpringCacheConfig {

    /**
     * EhCacheCacheManager实现了spring的CacheManager接口
     */
    @Bean
    EhCacheCacheManager ehCacheCacheManager(net.sf.ehcache.CacheManager myEhCacheCacheManager){
        return new EhCacheCacheManager(myEhCacheCacheManager);
    }

}

在方法上使用@Cacheable注解即可,方法上必须配置了动态代理的,SpringCache很类似于Spring的事务管理的。

你可能感兴趣的:(springboot,SpringCache,Ehcache,缓存)