这篇文章给大家分享的是有关SpringBoot与SpringCache概念是什么的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

1.SpringCache的概念

首先我们知道jpa,jdbc这些东西都是一些规范,比如jdbc,要要连接到数据库,都是需要用到数据库连接,预处理,结果集这三个对象,无论是连接到mysql还是oracle都是需要用到这个三个对象的,这是一种规范,而SpringCache是一种作为缓存的规范,具体实现有redis,EhCahe等

2.SpringCache用法(redis版)2.1 .SpringCache基本用法

1.pom.xml

<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.3</version><relativePath/><!--lookupparentfromrepository--></parent><groupId>com.yl</groupId><artifactId>cache_redis</artifactId><version>0.0.1-SNAPSHOT</version><name>cache_redis</name><description>DemoprojectforSpringBoot</description><properties><java.version>11</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency><artifactId>spring-boot-starter-data-redis</artifactId><artifactId>spring-boot-starter-web</artifactId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

2.application.properties

#redis的配置spring.redis.host=192.168.244.135spring.redis.port=6379spring.redis.password=root123

3.实体类

packagecom.yl.cache_redis.domain;importjava.io.Serializable;publicclassUserimplementsSerializable{privateIntegerid;privateStringusername;privateStringpassword;publicIntegergetId(){returnid;}publicvoidsetId(Integerid){this.id=id;}publicStringgetUsername(){returnusername;}publicvoidsetUsername(Stringusername){this.username=username;}publicStringgetPassword(){returnpassword;}publicvoidsetPassword(Stringpassword){this.password=password;}@OverridepublicStringtoString(){return"User{"+"id="+id+",username='"+username+'\''+",password='"+password+'\''+'}';}}

4.service

packagecom.yl.cache_redis;importcom.yl.cache_redis.domain.User;importorg.springframework.cache.annotation.Cacheable;importorg.springframework.stereotype.Service;@ServicepublicclassUserService{@Cacheable(cacheNames="u1")//这个注解作用就是将方法的返回值存到缓存中publicUsergetUserById(Integerid){System.out.println("getUserById:"+id);Useruser=newUser();user.setId(id);user.setUsername("root");user.setPassword("root");returnuser;}}

5.主程序,加上开启缓存的注解

packagecom.yl.cache_redis;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cache.annotation.EnableCaching;@SpringBootApplication@EnableCaching//开启缓存功能publicclassCacheRedisApplication{publicstaticvoidmain(String[]args){SpringApplication.run(CacheRedisApplication.class,args);}}

6.测试

6.1)userservice没加@Cacheable注解时

6.2)userservice加@Cacheable注解后,发现sevice中的方法只调用了一次

6.3)在redis中也可以看到缓存中有数据,key为定义好的cacheNames+::+方法的参数

2.2 .SpringCache自定义缓存key

1.SpringCache默认使用cacheNames和方法中的参数结合组成key的,那么如果有多个参数呢?它又是如何组成key的呢?我们可以指定key吗?

2.如何自定义key呢?

1)自定义key

packagecom.yl.cache_redis;importorg.springframework.cache.interceptor.KeyGenerator;importorg.springframework.stereotype.Component;importjava.lang.reflect.Method;importjava.util.Arrays;@ComponentpublicclassMyKeyGeneratorimplementsKeyGenerator{@OverridepublicObjectgenerate(Objecttarget,Methodmethod,Object...params){returntarget.toString()+":"+method.getName()+":"+Arrays.toString(params);}}

2)测试

2.3 .SpringCache更新缓存

1.使用@CachePut注解来更新,注意:@CachePut中的key要和@Cacheable中的key一样,否则更新不了!

2.4 .SpringCache清空缓存

1.使用@CacheEvict注解,主要key和要@Cacheable中的key一致

2.测试

2.5 .SpringCache其他用法

1.@Caching注解,可以组合多个注解

2.@CacheConfig注解

3.SpringCache用法(EhCache版)

1.pom.xml

<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.3</version><relativePath/><!--lookupparentfromrepository--></parent><groupId>com.yl</groupId><artifactId>ehcache</artifactId><version>0.0.1-SNAPSHOT</version><name>ehcache</name><description>DemoprojectforSpringBoot</description><properties><java.version>11</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency><artifactId>spring-boot-starter-web</artifactId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><groupId>net.sf.ehcache</groupId><artifactId>ehcache</artifactId><version>2.10.6</version></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

2.实体类

packagecom.yl.ehcache.model;importjava.io.Serializable;publicclassUserimplementsSerializable{privateIntegerid;privateStringusername;privateStringpassword;publicIntegergetId(){returnid;}publicvoidsetId(Integerid){this.id=id;}publicStringgetUsername(){returnusername;}publicvoidsetUsername(Stringusername){this.username=username;}publicStringgetPassword(){returnpassword;}publicvoidsetPassword(Stringpassword){this.password=password;}@OverridepublicStringtoString(){return"User{"+"id="+id+",username='"+username+'\''+",password='"+password+'\''+'}';}}

3.service

packagecom.yl.ehcache.service;importcom.yl.ehcache.model.User;importorg.springframework.cache.annotation.CacheEvict;importorg.springframework.cache.annotation.Cacheable;importorg.springframework.stereotype.Service;@ServicepublicclassUserService{@Cacheable(cacheNames="user")publicUsergetUserById(Integerid){System.out.println("getUserById()...");Useruser=newUser();user.setId(id);user.setUsername("root");user.setPassword("root");returnuser;}@CacheEvict(cacheNames="user")publicvoiddelete(Integerid){System.out.println("delete");}

4.主程序

packagecom.yl.ehcache;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cache.annotation.EnableCaching;@SpringBootApplication@EnableCachingpublicclassEhcacheApplication{publicstaticvoidmain(String[]args){SpringApplication.run(EhcacheApplication.class,args);}}

5.ehcache.xml

<ehcache><diskStorepath="java.io.tmpdir/shiro-spring-sample"/><defaultCachemaxElementsInMemory="1000"eternal="false"timeToIdleSeconds="120"timeToLiveSeconds="120"overflowToDisk="false"diskPersistent="false"diskExpiryThreadIntervalSeconds="120"/><cachename="user"maxElementsInMemory="1000"eternal="false"overflowToDisk="true"diskPersistent="true"diskExpiryThreadIntervalSeconds="600"/></ehcache>

6.测试

感谢各位的阅读!关于“SpringBoot与SpringCache概念是什么”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!