SpringBoot集成Redis缓存

说在前面:本配置教程不是教你如何在SpringBoot中配置RedisTemplate来访问Redis,而是为了降低查询数据库的负载,而把Redis当做缓存来用,明白?

首先纠正一个问题:

如果用的持续集成工具是Maven的话,最好是明确依赖项的版本,例如如下的正确的配置

<?xml version="1.0" encoding="UTF-8"?>
<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.example</groupId>
    <artifactId>rediscache</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>rediscache</name>
    <description>Demo project for Spring Boot Redis Cache</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <springBoot.version>2.0.4.RELEASE</springBoot.version>
    </properties>

    <dependencies>

        <!-- Web 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${springBoot.version}</version>
        </dependency>

        <!-- 模板引擎 Thymeleaf 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>${springBoot.version}</version>
        </dependency>

        <!-- Spring Data JPA 依赖 :: 数据持久层框架 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>${springBoot.version}</version>
        </dependency>

        <!-- h2 数据源依赖 -->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- Cache 缓存依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
            <version>${springBoot.version}</version>
        </dependency>

        <!-- Redis 数据源依赖 -->
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <version>${springBoot.version}</version>
        </dependency>

        <!-- 测试依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <version>${springBoot.version}</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

而如下是错误的配置:

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <name>chapter-6-spring-boot-cache-redis</name>
    <description>《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 6 章《数据缓存》Demo</description>

    <groupId>demo.springboot</groupId>
    <artifactId>chapter-6-spring-boot-cache-redis</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.BUILD-SNAPSHOT</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <!-- Web 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 模板引擎 Thymeleaf 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <!-- Spring Data JPA 依赖 :: 数据持久层框架 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!-- h2 数据源依赖 -->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- Cache 缓存依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>

        <!-- Redis 数据源依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-redis</artifactId>
        </dependency>

        <!-- 测试依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <!-- Spring Boot Maven 插件
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            -->
        </plugins>
    </build>


    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/libs-snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>

两者的区别就在于在错误的配置中没有指明依赖项的具体版本!在这种坏的习惯下,在IDE中虽然不会报告什么错误,但是如果你打开IDE中的Maven Projects视图的话,其实有些依赖解析是错误的,如下图

依赖解析错误

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,多了解一些技术,至少在遇到问题时,会让你想到,哎,是不是那个技术可以解决我的这个问题,而不是,一点思绪也没有。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值