SpringBootsad整合EhCache做缓存处理

轻量级的缓存框架Ehcache实现其功能。从以下几点切入:

  • 什么是EhCache?
  • 它和redis、membercache比较有什么优势?
  • 和SpringBoot怎么整合?
  • 实现机制?
  • 有哪些坑?
  1. EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认CacheProvider。Ehcache是一种广泛使用的开源Java分布式缓存。主要面向通用缓存,Java EE和轻量级容器。它具有内存和磁盘存储,缓存加载器,缓存扩展,缓存异常处理程序,一个gzip缓存servlet过滤器,支持REST和SOAP api等特点。
        Spring 提供了对缓存功能的抽象:即允许绑定不同的缓存解决方案(如Ehcache),但本身不直接提供缓存功能的实现。它支持注解方式使用缓存,非常方便。
  2.     快速
        简单
        多种缓存策略
        缓存数据有两级:内存和磁盘,因此无需担心容量问题
        缓存数据会在虚拟机重启的过程中写入磁盘
        可以通过RMI、可插入API等方式进行分布式缓存
        具有缓存和缓存管理器的侦听接口
        支持多缓存管理器实例,以及一个实例的多个缓存区域
        提供Hibernate的缓存实现
  3. (1)
  4. 导入jar包
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-cache</artifactId>
            </dependency>
                <!-- Ehcache 坐标 -->
            <dependency>
                <groupId>net.sf.ehcache</groupId>
                <artifactId>ehcache</artifactId>
            </dependency>

    (2)

    新建ehcache.xml配置文件
        <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
        <diskStore path="java.io.tmpdir/ehcache"/>
        <!--defaultCache:echcache的默认缓存策略  -->
        <defaultCache
                maxElementsInMemory="10000"
                eternal="false"
                timeToIdleSeconds="120"
                timeToLiveSeconds="120"
                maxElementsOnDisk="10000000"
                diskExpiryThreadIntervalSeconds="120"
                memoryStoreEvictionPolicy="LRU">
            <persistence strategy="localTempSwap"/>
        </defaultCache>
        <cache name="users"
               maxElementsInMemory="10000"
               eternal="false"
               timeToIdleSeconds="120"
               timeToLiveSeconds="120"
               maxElementsOnDisk="10000000"
               diskExpiryThreadIntervalSeconds="120"
               memoryStoreEvictionPolicy="LRU">
            <persistence strategy="localTempSwap"/>
        </cache>
        </ehcache>
        然后再通过application.properties配置下
        spring.cache.ehcache.config=classpath:ehcache.xml

   (3)

启动类上添加@EnableCaching
    在写一个可以扩展的配置类
    /**
     * 开启缓存配置
     *
     * @author zhangyi
     * @date 2018/12/13 15:47
     */
    @Configuration
    public class EhCacheConfig {
    }

(4)

在类上添加缓存配置,方法上添加缓存操作

    @Service
    @CacheConfig(cacheNames = "users")
    public class UserServiceImpl implements UserService{
    @Cacheable(value = "users")
    @Override
    public List<User> getAllUser(){
        List<User> list = new ArrayList<>();
        for(int i = 0; i < 5; i++) {
            User user = new User();
            user.setUserName(String.valueOf(i+Math.random()*10));
            user.setPassWord(String.valueOf(i));
            list.add(user);
        }
        System.out.println("模拟数据库查询... 过程");
        return list;
    }
    }

result:

第一次查询
    模拟数据库查询... 过程
    8.339899184231392--0
    4.358651013143946--1
    4.244988713811452--2
    9.693692145368964--3
    8.744268864524635--4
    第二次查询
    8.339899184231392--0
    4.358651013143946--1
    4.244988713811452--2
    9.693692145368964--3
    8.744268864524635--4
    第三次查询
    8.339899184231392--0
    4.358651013143946--1
    4.244988713811452--2
    9.693692145368964--3
    8.744268864524635—4

扫描二维码关注公众号,回复: 5163615 查看本文章

简单的三步走,后续的缓存一致性通过 CachePut CacheEvent来控制数据库和缓存数据之间的同步性
第一次查询是通过执行serverImpl中方法查看的,后续的缓存中有数据的时候,通过缓存读取
坑:
    在使用SoringBoot整合shiro时候,使用的是Ehcache做缓存在shiro配置类中,配置了EhcacheManager,导致报错,看了许多教程都是错误的,目前直接在
    application文件中加载其配置类就好了,直接缓存信息

猜你喜欢

转载自www.cnblogs.com/guyanzy/p/10367465.html