说在前面:本配置教程不是教你如何在SpringBoot中配置RedisTemplate来访问Redis,而是为了降低查询数据库的负载,而把Redis当做缓存来用,明白?
如果用的持续集成工具是Maven的话,最好是明确依赖项的版本,例如如下的正确的配置
<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.0modelVersion>
<groupId>com.examplegroupId>
<artifactId>rediscacheartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>rediscachename>
<description>Demo project for Spring Boot Redis Cachedescription>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.4.RELEASEversion>
<relativePath/>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
<springBoot.version>2.0.4.RELEASEspringBoot.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>${springBoot.version}version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
<version>${springBoot.version}version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-jpaartifactId>
<version>${springBoot.version}version>
dependency>
<dependency>
<groupId>com.h2databasegroupId>
<artifactId>h2artifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-cacheartifactId>
<version>${springBoot.version}version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
<version>${springBoot.version}version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<version>${springBoot.version}version>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
而如下是错误的配置:
<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.0modelVersion>
<name>chapter-6-spring-boot-cache-redisname>
<description>《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 6 章《数据缓存》Demodescription>
<groupId>demo.springbootgroupId>
<artifactId>chapter-6-spring-boot-cache-redisartifactId>
<version>1.0version>
<packaging>jarpackaging>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.0.0.BUILD-SNAPSHOTversion>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-thymeleafartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-jpaartifactId>
dependency>
<dependency>
<groupId>com.h2databasegroupId>
<artifactId>h2artifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-cacheartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-redisartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
plugins>
build>
<repositories>
<repository>
<id>spring-snapshotsid>
<name>Spring Snapshotsname>
<url>https://repo.spring.io/libs-snapshoturl>
<snapshots>
<enabled>trueenabled>
snapshots>
repository>
repositories>
project>
两者的区别就在于在错误的配置中没有指明依赖项的具体版本!在这种坏的习惯下,在IDE中虽然不会报告什么错误,但是如果你打开IDE中的Maven Projects视图的话,其实有些依赖解析是错误的,如下图
第一,在配置文件中加入Redis连接信息
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.min-idle=0
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-wait=-1
第二,开始SpringBoot的缓存支持@EnableCaching
@SpringBootApplication
@EnableCaching
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}
这时候SpringBoot会自动配置Redis缓存的CacheManager
第三,在Service中开始缓存数据,例如
@Cacheable(key = "#p0")
@Override
public Book findById(Long id) {
System.out.println(" call findById method ");
return bookRepository.findById(id).get();
}
当第一次请求Service中的findById方法时,回查询数据库,例如日志如下
call findById method
Hibernate: select book0_.id as id1_0_0_, book0_.introduction as introduc2_0_0_, book0_.name as name3_0_0_, book0_.writer as writer4_0_0_ from book book0_ where book0_.id=?
此时查看Redis中缓存的数据,如下
127.0.0.1:6379> keys *
1) "books::1"
当第二次再调用findById方法时,如果debug代码的话,你可以看到此时根本不会再进入到这个方法中,而是直接从缓存中取得数据
技术更新这么快,作为开发者,多写一些Demo,多了解一些技术,至少在遇到问题时,会让你想到,哎,是不是那个技术可以解决我的这个问题,而不是,一点思绪也没有。