SpringBoot集成Redis缓存

说在前面:本配置教程不是教你如何在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视图的话,其实有些依赖解析是错误的,如下图

SpringBoot集成Redis缓存_第1张图片

SpringBoot 集成Redis缓存 配置方式

第一,在配置文件中加入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代码的话,你可以看到此时根本不会再进入到这个方法中,而是直接从缓存中取得数据

后记

  1. 为了调试这个程序花费了将近一整天的时间,第一个原因是因为Maven的依赖没有指明具体的版本;第二个原因是,这个实例项目的代码是从github中其他人(https://github.com/kinggggg/springboot-learning-example/tree/master/chapter-6-spring-boot-cache-redis)写的直接clone下来的,在测试的过程当中如果不检查Redis中的数据是否正确缓存的话,其实发现不了这个问题。因为虽然没有正确的配置依赖项的版本,但是SpringBoot会默认生成不用用于Redis缓存的RedisCacheManager,而是其他的CacheManager(比如SimpleCacheManager),如果是这样的话,虽然在表明上看到的效果与利用Redis进行缓存数据的效果一样,但是其实用的不是Redis进行的缓存
  2. 由于这个示例代码是位于多模块项目当中,而对于多模块的Maven项目,第三方的依赖均位于最顶层的项目的依赖下,无法
  3. 有时候我们遇到问题了,直接google,百度,这时候的思绪其实只想找到一个值复制粘贴就能解决自己问题的代码,搜索的时间越长,这种感觉越强烈!出现这种情况后自己一定要冷静下来理性地分析问题,而不是一味的进行搜索
  4. 在这个过程当中,我也尝试直接看SpringBoot的官方文档,因为之前直接看开源项目的官方文档的时间比较少,通过这次的经历发现,其实一些开源项目的官方文档写的是很不错的,例如Spring。看官方文档是最直接解决问题的一种方式。

最后

技术更新这么快,作为开发者,多写一些Demo,多了解一些技术,至少在遇到问题时,会让你想到,哎,是不是那个技术可以解决我的这个问题,而不是,一点思绪也没有。

你可能感兴趣的:(Spring,Spring,Boot,SpringBoot,Redis,缓存,集成)