ehcache springboot,ehcache缓存失效

  ehcache springboot,ehcache缓存失效

  00-1010为什么引入cache springBoot抽象缓存代码实现添加缓存依赖,open cache data cache @ cache able @ cache put @ cache evict,集成Ehcache,添加EhCache依赖,添加EhCache相关配置注释含义:测试Spring cache (EhCache)在Spring3.1中引入,但只提供缓存接口,不提供具体的缓存实现,其实现需要第三方缓存实现(Generic,EhCache,Redis等。).EhCache和Redis是常用的。使用Redis时,需要先安装Redis服务器。

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

  00-1010刚才Spring3.1推出了缓存接口,可以和不同的缓存技术接口。主要界面有:

  org . spring framework . cache . cache(定义缓存的接口)。org . spring framework . cache . cache manager:不同的缓存技术有不同的缓存管理器。SpringBoot将按照以下顺序自动配置这些框架提供的缓存管理器。普通的.JCache (JSR-107) (EhCache 3,Hazelcast,Infinispan以及其他).EhCache 2.x .黑泽尔卡斯特.无限跨度.沙发底座.Redis .咖啡因.简单。

  

目录

 

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

  !-数据缓存-依赖关系groupIdorg.springframework.boot/groupId artifact id spring-boot-starter-cache/artifact id/dependency

  00-1010使用注释**@EnableCaching**打开缓存功能。

  @ Configuration @ enablecaching public class MyCacheConfig { }

  00-1010缓存操作中常用的注释如下:

  

为什么引入缓存

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

 

  Value:用于描述缓存的名称,可以指定一个或多个。

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

  条件:缓存的条件,可以为空。如果是用SpEl写的,为真就缓存,为假就不缓存。

  @Cacheable(value=student ,key=#id ,condition= # id11 )@ Override public Student find byid(Long id){ return Student mapper . find byid(id);}

  

SpringBoot抽象缓存

@CachePut可以在方法和类上标记,其常用属性和**@Cacheable相同,只是在方法执行前不检查缓存的存在,而是在方法执行后将结果放入缓存,多用于数据的添加和修改。

 

  @CachePut(value=student ,key= # Student . id )@ Override public Student update Student(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、在srcmainresources路径下添加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的默认路径为srcmainresourcesehcache.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,那么点击两次的情况下,我们只进入了方法一次。

  

 

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

  到此这篇关于SpringBoot缓存Ehcache的使用的文章就介绍到这了,更多相关SpringBoot缓存Ehcache内容请搜索盛行IT以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT!

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: