SpringBoot_07_Cache_04_Redis

前言

SpringBoot_07_Cache_01_快速入门 这一节中,我们了解了 Spring 声明式缓存抽象,知道了:

  • 我们使用Spring声明式缓存,使用的是其抽象,无关底层实现。也就是说,不管你底层使用的是什么缓存技术,对Spring声明式缓存的使用是一致的
  • 若想要采用不同的缓存技术,只需要替换掉底层注册的 CacheManager 即可

那么,这一节我们将采用 redis作为缓存技术,来实现一个Spring声明式缓存的实例,这显然就很简单了,一共两步:

  • 引入 redis依赖
  • 开启缓存支持
  • 配置 redis
  • 使用声明式缓存

其他步骤同上一步: SpringBoot_07_Cache_01_快速入门

一、SpringBoot 缓存整合 redis

1.创建子模块

这里我们创建一个子模块,创建步骤同 SpringBoot_07_Cache_01_快速入门

group = 'com.ray.study'
artifact ='spring-boot-07-cache-redis'

2.引入依赖

2.1 继承父工程依赖

在父工程spring-boot-seedssettings.gradle加入子工程

rootProject.name = 'spring-boot-seeds'
include 'spring-boot-01-helloworld'
include 'spring-boot-02-restful-test'
include 'spring-boot-03-thymeleaf'
include 'spring-boot-04-swagger2'
include 'spring-boot-05-jpa'
include 'spring-boot-05-mybatis'
include 'spring-boot-05-tk-mybatis'
include 'spring-boot-06-nosql-redis'
include 'spring-boot-06-nosql-mongodb'
include 'spring-boot-07-cache-concurrentmap'
include 'spring-boot-07-cache-ehcache'
include 'spring-boot-07-cache-caffeine'
include 'spring-boot-07-cache-redis'

这样,子工程spring-boot-07-cache-redis就会自动继承父工程中subprojects `函数里声明的依赖,主要包含如下依赖:

		implementation 'org.springframework.boot:spring-boot-starter-web'
        testImplementation 'org.springframework.boot:spring-boot-starter-test'

        compileOnly 'org.projectlombok:lombok'
        annotationProcessor 'org.projectlombok:lombok'

2.2 引入redis依赖

将子模块spring-boot-07-cache-redisbuild.gradle修改为如下内容:

dependencies {
    // spring-boot-starter-cache
    implementation 'org.springframework.boot:spring-boot-starter-cache'

    // redis 依赖 和 common pool2 ,要使用redis连接池就需要引入此common pool2
    implementation 'org.springframework.boot:spring-boot-starter-data-redis'
    implementation 'org.apache.commons:commons-pool2'


    // mysql 驱动和 jpa
    implementation 'mysql:mysql-connector-java'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

}

3.修改配置

3.1 修改application.yml

关于 redis cache 的可以使用默认配置:

server:
  port: 8088
  servlet:
    context-path: /

spring:
  datasource:       # 配置数据源
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/integrate-jpa?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
    username: root
    password: root
  jpa:                 # 配置jpa
    database: mysql       # 数据库类型
    show-sql: true        # 打印sql语句
    hibernate:
      ddl-auto: update    # 加载 Hibernate时, 自动更新数据库结构

3.2 CacheConfiguration

开启缓存支持

通常SpringBoot默认的keyGenerator 是SimpleKeyGenerator,这个策略是以参数作为key值,如果参数为空的,就会返回SimpleKey[]字符串,这对于很多无参的方法的就有问题了.
向Spring容器中重新注册一个实现了 org.springframework.cache.interceptor.keyGenerator 这个接口的Bean即可,将key值设置为类名+方法名+参数名,这样就不会冲突了

package com.ray.study.springboot07cache.caffeine.config;

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * Spring cache 配置类
 *
 * @author shira 2019/05/13 18:45
 */
@Configuration
@EnableCaching
public class CacheConfiguration {

	@Bean
	public KeyGenerator caffeineKeyGenerator() {
		return (target, method, params) -> {  // KeyGenerator#generate
			StringBuilder sb = new StringBuilder();
			sb.append(target.getClass().getName());
			sb.append(method.getName());
			for (Object obj : params) {
				sb.append(obj.toString());
			}
			return sb.toString();
		};
	}



}

3.其他部分

其他部分(数据库准备、业务实现、单元测试)参加: SpringBoot_07_Cache_01_快速入门

不过请注意要缓存的JavaBean必须实现Serializable接口,因为Spring会将对象先序列化再存入 Redis,因此

@Getter
@Setter
@NoArgsConstructor
@MappedSuperclass 
public class BaseDO implements Serializable {   // BaseDO需要实现Serializable接口,否则会抛无法序列化的异常

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	@JsonIgnore
	private Date creationDate;

	@JsonIgnore
	private Date lastUpdateDate;
}


参考资料

  1. springboot 2 集成 redis 缓存
  2. Spring Boot缓存实战 Caffeine
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值