首页 > 编程技术 > java

SpringBoot2整合Ehcache组件实现轻量级缓存管理

发布时间:2021-6-19 15:00

一、Ehcache缓存简介

Hibernate缓存

Hibernate三级缓存机制简介:

一级缓存:基于Session级别分配一块缓存空间,缓存访问的对象信息。Session关闭后会自动清除缓存。

二级缓存:是SessionFactory对象缓存,可以被创建出的多个 Session 对象共享,二级缓存默认是关闭的,如果要使用需要手动开启,并且依赖EhCache组件。

三级缓存:查询缓存,配置开启该缓存的情况下,重复使用一个sql查询某个范围内的数据,会进行缓存。

EhCache缓存特点

对比Redis缓存

Ehcache:直接在Jvm虚拟机中缓存,速度快,效率高,不适合处理大规模缓存数据,在分布式环境下,缓存数据共享操作复杂;

Redis:作为独立的缓存中间件,在分布式缓存系统中非常好用,缓存数据共享,有效支撑大量数据缓存,支持哨兵模式,或者集群模式的高可用成熟方案;

二、集成SpringBoot框架

1、核心依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
</dependency>

2、加载配置

基础配置

spring:
  cache:
    ehcache:
      config: classpath:ehcache.xml

启动类注解

@EnableCaching
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args) ;
    }
}

3、配置详解

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">

    <!-- 操作系统缓存的临时目录,内存满后写入该目录 -->
    <diskStore path="java.io.tmpdir"/>

    <defaultCache
            maxElementsInMemory="1000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            maxElementsOnDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </defaultCache>

    <cache name="userEntity"
           maxElementsInMemory="1000"
           eternal="false"
           timeToIdleSeconds="120"
           timeToLiveSeconds="120"
           maxElementsOnDisk="10000000"
           diskExpiryThreadIntervalSeconds="120"
           memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </cache>
</ehcache>

配置参数说明

三、注解用法

@Service
public class CacheService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CacheService.class);

    @Resource
    private UserMapper userMapper ;

    @Cacheable(value="userEntity")  // 在缓存有效期内,首次查询才访问数据库
    public UserEntity getById (Integer id){
        // 通过日志,标识方法是否执行
        LOGGER.info("getById..."+id);
        return userMapper.selectById(id) ;
    }

    @CacheEvict(value="userEntity",key = "#id") //该ID数据更新,清空该ID缓存
    public void updateUser(Integer id) {
        UserEntity user = new UserEntity() ;
        user.setId(id);
        user.setUserName("myCache");
        userMapper.updateById(user);
    }
}

@Cacheable:注解标记在一个方法上,也可以标记在一个类上,标记在一个方法上表示该方法支持缓存,该方法被调用后将其返回值缓存起来,下次同样的请求参数执行该方法时可以直接从缓存中获取结果,而不需要再次执行该方法。

@CacheEvict:注解标记在需要清除缓存元素的方法或类上的,当标记在一个类上时表示其中所有的方法的执行都会触发缓存的清除操作,并且可以按照指定属性清除。

四、源代码地址

GitHub·地址
https://github.com/cicadasmile/middle-ware-parent
GitEE·地址
https://gitee.com/cicadasmile/middle-ware-parent

以上就是SpringBoot2整合Ehcache组件实现轻量级缓存管理的详细内容,更多关于SpringBoot2 整合Ehcache组件的资料请关注猪先飞其它相关文章!

标签:[!--infotagslink--]

您可能感兴趣的文章: