springboot中注解实现集成redisjava
https://blog.csdn.net/a67474506/article/details/52608855web
Spring定义了org.springframework.cache.CacheManager和org.springframework.cache.Cache接口来统一不一样的缓存技术,而SpringBoot为咱们提供了自动配置多个CacheManager的实现。redis
其实是SpringBoot提早作好了这些工做,将经常使用的缓存中间件先集成了SpringBoot项目,开发者只须要【在POM中引入具体的实现】和【配置缓存中间件的好参数】便可。spring
application.yml中的配置信息以下所示:apache
其中的App类和DataCache类是公用的。也就是用SimpleCache、Guava、Redis三种不一样的缓存应用,这两个类的代码彻底同样。缓存
package com.ding.data; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.ding.data.cache.DataCache; /** * 是Spring Boot项目的核心注解,主要是开启自动配置 */ @SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan @RestController // 开启缓存 @EnableCaching public class App { @Autowired private DataCache dataCache; public static void main(String[] args) { SpringApplication.run(App.class, args); } @RequestMapping("/put") public String put(Long id, String value) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(new Date()) + " : value is " + dataCache.put(id, value) ; } @RequestMapping("/get") public String query(Long id){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return sdf.format(new Date()) + " : value is " +dataCache.query(id) ; } @RequestMapping("/remove") public String remove(Long id) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); dataCache.remove(id) ; return sdf.format(new Date()) + " : success " ; } }
package com.ding.data.cache; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import javax.annotation.PostConstruct; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Component; @Component public class DataCache { private Map<Long, String> dataMap = new HashMap<Long, String>(); /** * 初始化 */ @PostConstruct public void init() { dataMap.put(1L, "张三"); dataMap.put(2L, "李四"); dataMap.put(3L, "王五"); } /** * 查询 * 若是数据没有缓存,那么从dataMap里面获取,若是缓存了, * 那么从guavaDemo里面获取 * 而且将缓存的数据存入到 guavaDemo里面 * 其中key 为 #id+dataMap */ @Cacheable(value="guavaDemo" ,key="#id + 'dataMap'") public String query(Long id) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf.format(new Date()) + " : query id is " + id); return dataMap.get(id); } /** * 插入 或者更新 * 插入或更新数据到dataMap中 * 而且缓存到 guavaDemo中 * 若是存在了那么更新缓存中的值 * 其中key 为 #id+dataMap */ @CachePut(value="guavaDemo" ,key="#id + 'dataMap'") public String put(Long id, String value) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf.format(new Date()) + " : add data ,id is "+ id); dataMap.put(id, value); // data persistence return value; } /** * 删除 * 删除dataMap里面的数据 * 而且删除缓存guavaDemo中的数据 * 其中key 为 #id+dataMap */ @CacheEvict(value="guavaDemo" , key="#id + 'dataMap'") public void remove(Long id) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf.format(new Date()) + " : remove id is "+ id + " data"); dataMap.remove(id); // data remove } }
不须要在POM中引入任何缓存中间件(如Redis、Guava、Ehcache等),也不须要在application.yml配置文件中写任何配置信息。springboot
只须要在主启动类上加上 @EnableCaching 注解并在想使用缓存的地方使用 @Cacheable 、 @CachePut 、@CacheEvict 等注解就可使用了。app
<project xmlns="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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ding.data</groupId> <artifactId>cacheCase</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <boot.version>1.3.5.RELEASE</boot.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>${boot.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>${boot.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> <version>${boot.version}</version> </dependency> <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>${boot.version}</version> </dependency> --> <!-- <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>19.0</version> </dependency> --> </dependencies> </project>
首次访问eclipse
http://localhost:8080/get?id=1maven
eclipse控制台打印以下
2018-07-03 19:49:22 : query id is 1
再次访问,显示内容同上,可是eclipse控制台没有打印。说明此时已经缓存成功了,没有进入Service的方法。
访问
http://localhost:8080/put?id=1&value=666
再次访问
http://localhost:8080/get?id=1
Eclipse控制台中并未打印,说明 @CachePut 注解后立刻数据立刻就进入了缓存。
访问
http://localhost:8080/remove?id=1
Eclipse控制台中打印以下:
2018-07-03 19:59:19 : remove id is 1 data
再次访问
http://localhost:8080/get?id=1
缓存已删除了。
<project xmlns="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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ding.data</groupId> <artifactId>cacheCase</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <boot.version>1.3.5.RELEASE</boot.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>${boot.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>${boot.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> <version>${boot.version}</version> </dependency> <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>${boot.version}</version> </dependency> --> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>19.0</version> </dependency> </dependencies> </project>
spring: cache: #缓存名称 cache-names: guavaDemo #缓存最大数量500条, 缓存失效时间 6个小时 guava.spec: maximumSize=500,expireAfterWrite=360m
跟SimpleCache的测试结果同样。
<project xmlns="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.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ding.data</groupId> <artifactId>cacheCase</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <boot.version>1.3.5.RELEASE</boot.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>${boot.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>${boot.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> <version>${boot.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> <version>${boot.version}</version> </dependency> <!-- <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>19.0</version> </dependency> --> </dependencies> </project>
spring: cache: #缓存名称 cache-names: guavaDemo #缓存最大数量500条, 缓存失效时间 6个小时 #guava.spec: maximumSize=500,expireAfterWrite=360m # REDIS (RedisProperties) redis : host : localhost # server host port : 6379 # connection port password : 123 pool.max-idle : 8 # pool settings ... pool.min-idle : 1 pool.max-active : 8 pool.max-wait : -1
查询前,可见Redis中尚未任何缓存信息
查询
http://localhost:8080/get?id=1
Eclipse控制台打印以下:
查看Redis中,可见多了一条数据
查看redis中的内容,已经变了
查看Redis的值,值已经消失了
调用结束,中止项目,查看Redis。可见调用后Redis的缓存仍然存在,由于同SimpleCache、Guava不同,Redis的缓存能够实现持久化;而SimpleCache、Guava则随着项目的中止缓存清空了。
若是没有缓存,进入 @Cacheable 标注的方法内获取数据,返回时将数据灌入value和key定位的缓存;若是有缓存,就不进入方法了,直接返回缓存。
确定进入方法进行操做,而后将返回值灌入value和key定位的缓存中。
删除value和key定位的缓存。
在Spring Boot中经过@EnableCaching注解自动化配置合适的缓存管理器(CacheManager),默认状况下Spring Boot根据下面的顺序自动检测缓存提供者:
Generic
JCache (JSR-107)
EhCache 2.x
Hazelcast
Infinispan
Redis
Guava
Simple
SpringBoot会根据你的类路径里面的依赖jar,来肯定使用什么类型进行缓存,因此基本是咱们是不用配置spring.cache.type这个属性的。
因此配置了Guava或者Redis缓存时,SpringBoot会自动检测这两个缓存的jar包和配置,发现那个就会自动适配上对应的缓存管理器并在后面使用。
那么能否同时引入多个缓存组件并同时使用呢?如何使用呢?——后面会有说明