SpringBoot-缓存Ehcache的使用

开发
Spring缓存(EhCache)是在Spring3.1开始引入的,但是其本身只提供了缓存接口,不提供具体缓存的实现,其实现需要第三方缓存实现(Generic、EhCache、Redis等)。

spring缓存(EhCache)是在Spring3.1开始引入的,但是其本身只提供了缓存接口,不提供具体缓存的实现,其实现需要第三方缓存实现(Generic、EhCache、Redis等)。EhCache、Redis比较常用,使用Redis的时候需要先安装Redis服务器。

为什么引入缓存

  • 提升服务性能:例如在项目开发完成以后,随着时间推移,各种数据急剧增加,在数据不断增加的情况下,一个简单的Select * from Student,都可能非常耗时,变成我们用户体验的痛点。并且在分布式远程调用的过程中,网络开销本来就比较大,如果再加上上面情况导致整体响应时间变大,得不偿失,因此缓存是十分必要的
  • 减少数据库压力:当数据增大,请求变多以后,数据库的压力将大大增加,缓存的出现可以减轻数据库压力。

SpringBoot抽象缓存

刚才说了Spring3.1引入了缓存接口,可以对接不同的缓存技术主要接口有:

  • org.springframework.cache.Cache (定义缓存的接口)。
  • org.springframework.cache.CacheManager:缓存管理器针对不同的缓存技术,有不同的缓存管理器,SpringBoot会按照以下顺序自动配置这些框架提供的缓存管理器。
  • Generic。
  • JCache (JSR-107) (EhCache 3, Hazelcast, Infinispan, and others)。
  • EhCache 2.x。
  • Hazelcast。
  • Infinispan。
  • Couchbase。
  • Redis。
  • Caffeine。
  • Simple。

代码实现

添加缓存依赖

在pom.xml中添加spring-boot-starter-cache。

<!--数据缓存-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>

开启缓存

使用注解**@EnableCaching**注解开启缓存功能。

@Configuration
@EnableCaching
public class MyCacheConfig {
}

数据缓存

在缓存操作中常用的注解有以下:

@Cacheable

@Cacheable可以标记在方法和类上面,在执行的时候会先看缓存数据是否存在,如果存在直接返回缓存数据,如果不存在就会支付方法并将缓存返回到缓存中,常用的三个属性。

value:用于说明缓存的名称,可以指定一个或者多个。

key:缓存的键值可以为空,如果不为空需要安装SpEl表达方式编写。

condition:缓存的条件,可以为空,如果使用按照SpEl方式编写,返回true则缓存,false不缓存。

 @Cacheable(value = "student",key = "#id",condition = "#id>11")
@Override
public Student findById(Long id) {
return studentMapper.findById(id);
}

@CachePut

@CachePut可以标注在方法和类上面,常用属性和**@Cacheable相同,不同之处在于执行方法前不会查看缓存中是否存在,而是方法执行完成以后将结果放入缓存中,多用于数据的添加和修改。

   @CachePut(value = "student",key = "#student.id")
@Override
public Student updateStudent(Student student){
studentMapper.updateStudent(student);
return student;
}

@CacheEvict

@CacheEvict可以标注在方法和类方面,用于清除缓存,常用注解除了和@Cacheable相同以外还有。

  • allEntries:是否清空所有缓存,默认false,当为true时,调用方法后就会清空所有缓存。
  • beforeInvocation:是否在方法执行前情况,默认false,为true的时候,在方法调用前就会清空缓存,false的时候如果方法抛出异常则不会清除缓存。
 @CacheEvict(value = "student",key = "#id",allEntries = true,beforeInvocation = true)
public void deleteStudent(@Param("id") Long id){
System.out.println("deleteStudent数据库..." + id);
studentMapper.deleteStudent(id);
}

集成EhCache

因为springboot只是缓存的抽象,要具体实现缓存还有依赖第三方缓存框架,我们这里介绍EhCache框架实现缓存。

添加EhCache依赖

在pom.xml中添加EhCache依赖。

<!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.9.2</version>
</dependency>

添加Ehcache相关配置

1、在src\main\resources路径下添加ehcache.xml文件。

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd">
<cache name="student"
maxElementsInMemory="10000"
eternal="true"
overflowToDisk="true"
diskPersistent="true"
diskExpiryThreadIntervalSeconds="600"/>
</ehcache>

注解含义:

  • name:缓存名称和缓存注解中value属性相同即可。
  • maxElementsInMemory:缓存的最大数量。
  • overflowToDisk:缓存达到最大数量,会写入到磁盘。
  • eternal:缓存是否永久有效,如果设置为true,则timeout无效。
  • diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认120s。

2、在application.yml添加ehcache.xml的路径。

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

ehcache.config的默认路径为src\main\resourcesehcache.xm,所以也可以不配置。

测试

1、测试@Cacheable(value = "student",key = "#id",cndition = "#id>11")使用postman测试接口http://localhost:8899/student/select/11。

点击两次我们在console发现,两次都进入了方法,这是因为我们有判断添加id大于11才会放入缓存中。

如果id>11例如http://localhost:8899/student/select/13,那么点击两次的情况下,我们只进入了方法一次。

其他测试可以自行测试,这里就不过多测试了。

责任编辑:姜华 来源: 今日头条
相关推荐

2023-02-14 07:47:20

SpringBootEhcache

2023-01-11 15:11:36

SpringEhcache

2023-10-12 08:00:48

2014-12-31 09:56:29

Ehcache

2021-09-26 05:02:00

缓存Ehcache用法

2014-12-31 09:45:48

EhCache

2019-04-23 08:42:42

EhcacheMemcacheRedis

2012-02-01 15:53:03

JavaEhcache

2009-09-21 17:23:49

Hibernate使用

2021-11-04 08:04:49

缓存CaffeineSpringBoot

2021-02-17 21:04:03

Ehcache缓存Java

2020-08-19 17:56:46

缓存Redis集中式

2024-02-28 09:54:07

线程池配置

2011-11-16 10:46:33

Ehcache

2009-09-21 14:59:31

Hibernate二级

2018-05-15 10:54:33

NginxRedisEhcache

2019-07-10 15:41:50

RedisJava缓存

2020-01-10 15:42:13

SpringBootRedis数据库

2024-02-29 09:20:10

2020-06-29 07:43:12

缓存RedisSpringBoot
点赞
收藏

51CTO技术栈公众号