1. 前言

上一文我们整合了Mybatis Plus,今天我们会把缓存也集成进来。缓存是一个系统应用必备的一种功能,除了在减轻数据库的压力之外。还在存储一些短时效的数据场景中发挥着重大作用,比如存储用户Token、短信验证码等等,目前缓存的选型还是比较多的,EHCACHEHAZELCASTCAFFEINECOUCHBASE以及本文要整合的REDIS。接下来我们将会在kono脚手架项目中集成Spring Cache以及Redis

Gitee: https://gitee.com/felord/kono day05 分支

GitHub: https://github.com/NotFound403/kono day05 分支

2. 整合目标

使项目具有缓存功能,同时将默认的JDK序列化修改为Jackson序列化以存储一些对象,同时实现一些特定的个性化的缓存空间以满足不同场景下的不同缓存TTL时间需求。

3. 依赖集成

目前只需要引入下面的依赖即可:

 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>

默认情况下spring-data-redis使用高性能的lettuce客户端实现,当然你可以替换为老旧的jedis

4. 缓存及Redis配置

缓存以及Redis相关的配置项分别为spring.cachespring.redis开头的配置,这里比较简单的配置为:

spring:
redis:
host: localhost
port: 6379
cache:
# type: REDIS
redis:
# 全局过期时间
time-to-live: 120

5. RedisTemplate个性化

默认情况下会有两个模板类被注入Spring IoC供我们使用,需要个性化配置来满足实际的开发。

一个是RedisTemplate<Object, Object>,主要用于对象缓存,其默认使用JDK序列化,我们需要更改其序列化方式解决一些问题,比如Java 8日期问题、JSON序列化问题。需要我们重写一下。

/**
* Redis的一些自定义配置.
*
* @author felord.cn
* @since 2020 /8/17 20:39
*/
@ConditionalOnClass(ObjectMapper.class)
@Configuration(proxyBeanMethods = false)
public class RedisConfiguration {
/**
* Redis template redis template.
*
* @param redisConnectionFactory the redis connection factory
* @return the redis template
*/
@Bean("redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
// 使用Jackson2JsonRedisSerialize 替换默认序列化
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = initJacksonSerializer();
// 设置value的序列化规则和 key的序列化规则
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setKeySerializer(new StringRedisSerializer());
template.afterPropertiesSet();
return template;
} /**
* 处理redis序列化问题
* @return Jackson2JsonRedisSerializer
*/
private Jackson2JsonRedisSerializer<Object> initJacksonSerializer() {
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//以下替代旧版本 om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
om.activateDefaultTyping(om.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL);
//bugFix Jackson2反序列化数据处理LocalDateTime类型时出错
om.disable(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS);
// java8 时间支持
om.registerModule(new JavaTimeModule());
jackson2JsonRedisSerializer.setObjectMapper(om);
return jackson2JsonRedisSerializer;
} }

另一个是StringRedisTemplate,主要处理键值都是字符串的缓存,采用默认就好。

6. 缓存个性化

使用Spring Cache做缓存的时候,有针对不同的key设置不同过期时间的场景。比如Jwt Token我想设置为一周过期,而验证码我想设置为五分钟过期。这个怎么实现呢?需要我们个性化配置RedisCacheManager。首先我通过枚举来定义这些缓存及其TTL时间。例如:

/**
* 缓存定义枚举
*
* @author felord.cn
* @see cn.felord.kono.configuration.CacheConfiguration
* @since 2020/8/17 21:40
*/ public enum CacheEnum { /**
* 用户jwt token 缓存空间 ttl 7天
*/
JWT_TOKEN_CACHE("usrTkn", 7 * 24 * 60 * 60),
/**
* 验证码缓存 5分钟ttl
*/
SMS_CAPTCHA_CACHE("smsCode", 5 * 60); /**
* 缓存名称
*/
private final String cacheName;
/**
* 缓存过期秒数
*/
private final int ttlSecond; CacheEnum(String cacheName, int ttlSecond) {
this.cacheName = cacheName;
this.ttlSecond = ttlSecond;
} public String cacheName() {
return this.cacheName;
} public int ttlSecond() {
return this.ttlSecond;
}
}

这样就能很清楚地描述个性化的缓存了。

然后我们通过向Spring IoC分别注入RedisCacheConfigurationRedisCacheManagerBuilderCustomizer 来个性化配置,你可以留意CacheEnum是如何工作的。如果你有其它的个性化需要也可以对这两个配置类进行定制化。

import cn.felord.kono.enumeration.CacheEnum;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.autoconfigure.cache.RedisCacheManagerBuilderCustomizer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializationContext; import java.time.Duration;
import java.util.EnumSet;
import java.util.stream.Collectors; /**
* redis 缓存配置.
*
* @author felord.cn
* @since 2020 /8/17 20:14
*/
@EnableCaching
@Configuration
public class CacheConfiguration { /**
* Redis cache configuration.
*
* @param redisTemplate the redis template
* @return the redis cache configuration
*/
@Bean
public RedisCacheConfiguration redisCacheConfiguration(RedisTemplate<Object, Object> redisTemplate, CacheProperties cacheProperties) {
// 参见 spring.cache.redis
CacheProperties.Redis redisProperties = cacheProperties.getRedis(); RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
// 缓存的序列化问题
.serializeValuesWith(RedisSerializationContext.SerializationPair
.fromSerializer(redisTemplate.getValueSerializer())); if (redisProperties.getTimeToLive() != null) {
// 全局 TTL 时间
redisCacheConfiguration = redisCacheConfiguration.entryTtl(redisProperties.getTimeToLive());
}
if (redisProperties.getKeyPrefix() != null) {
// key 前缀值
redisCacheConfiguration = redisCacheConfiguration.prefixCacheNameWith(redisProperties.getKeyPrefix());
}
if (!redisProperties.isCacheNullValues()) {
// 默认缓存null值 可以防止缓存穿透
redisCacheConfiguration = redisCacheConfiguration.disableCachingNullValues();
}
if (!redisProperties.isUseKeyPrefix()) {
// 不使用key前缀
redisCacheConfiguration = redisCacheConfiguration.disableKeyPrefix();
}
return redisCacheConfiguration;
} /**
* Redis cache manager 个性化配置缓存过期时间.
* @see RedisCacheManager,CacheEnum
* @return the redis cache manager builder customizer
*/
@Bean
public RedisCacheManagerBuilderCustomizer redisCacheManagerBuilderCustomizer(RedisCacheConfiguration redisCacheConfiguration) { return builder -> builder.cacheDefaults(redisCacheConfiguration)
// 自定义的一些缓存配置初始化 主要是特定缓存及其ttl时间
.withInitialCacheConfigurations(EnumSet.allOf(CacheEnum.class).stream()
.collect(Collectors.toMap(CacheEnum::cacheName,
cacheEnum -> redisCacheConfiguration.entryTtl(Duration.ofSeconds(cacheEnum.ttlSecond())))));
} }

个性化的同时我们可以通过注解@EnableCaching开启Spring Cache缓存支持。关于Spring Cache的细节可以通过文章Spring Cache详解来了解。

请注意,只有通过Spring Cache操作缓存才会达到上图的效果。命令行操作需要显式的声明指令。

7. 总结

最近事情比较多,所以难得抽出时间来搞一搞。如果你在实际开发中遇到需要整合的功能也可以告诉我,同时如果你发现整合中的一些缺陷或者Bug请提交ISSUE。多多关注:码农小胖哥,跟我一起整合开发脚手架。

关注公众号:Felordcn 获取更多资讯

个人博客:https://felord.cn

从零搭建Spring Boot脚手架(6):整合Redis作为缓存的更多相关文章

  1. 从零搭建Spring Boot脚手架(7):整合OSS作为文件服务器

    1. 前言 文件服务器是一个应用必要的组件之一.最早我搞过FTP,然后又用过FastDFS,接私活的时候我用MongoDB也凑合凑合.现如今时代不同了,开始流行起了OSS. Gitee: https: ...

  2. 从零搭建Spring Boot脚手架(1):开篇以及技术选型

    1. 前言 目前Spring Boot已经成为主流的Java Web开发框架,熟练掌握Spring Boot并能够根据业务来定制Spring Boot成为一个Java开发者的必备技巧,但是总是零零碎碎 ...

  3. 从零搭建Spring Boot脚手架(2):增加通用的功能

    1. 前言 今天开始搭建我们的kono Spring Boot脚手架,首先会集成Spring MVC并进行定制化以满足日常开发的需要,我们先做一些刚性的需求定制,后续再补充细节.如果你看了本文有什么问 ...

  4. 从零搭建Spring Boot脚手架(3):集成mybatis

    1. 前言 今天继续搭建我们的kono Spring Boot脚手架,上一文集成了一些基础的功能,比如统一返回体.统一异常处理.快速类型转换.参数校验等常用必备功能,并编写了一些单元测试进行验证,今天 ...

  5. 从零搭建Spring Boot脚手架(4):手写Mybatis通用Mapper

    1. 前言 今天继续搭建我们的kono Spring Boot脚手架,上一文把国内最流行的ORM框架Mybatis也集成了进去.但是很多时候我们希望有一些开箱即用的通用Mapper来简化我们的开发.我 ...

  6. 从零搭建Spring Boot脚手架(7):Elasticsearch应该独立服务

    1. Spring Data Elasticsearch Spring Data Elasticsearch是Spring Data项目的子项目,提供了Elasticsearch与Spring的集成. ...

  7. 从零搭建Spring Boot脚手架(5):整合 Mybatis Plus

    1. 前言 在上一文中我根据Mybatis中Mapper的生命周期手动实现了一个简单的通用Mapper功能,但是遗憾的是它缺乏实际生产的检验.因此我选择更加成熟的一个Mybatis开发增强包.它就是已 ...

  8. Spring Boot 2.x 整合 Redis最佳实践

    一.前言 在前面的几篇文章中简单的总结了一下Redis相关的知识.本章主要讲解一下 Spring Boot 2.0 整合 Redis.Jedis 和 Lettuce 是 Java 操作 Redis 的 ...

  9. Spring Boot 2.x整合Redis

    最近在学习Spring Boot 2.x整合Redis,在这里和大家分享一下,希望对大家有帮助. Redis是什么 Redis 是开源免费高性能的key-value数据库.有以下的优势(源于Redis ...

随机推荐

  1. java并发编程[持续更新]

    目录 java并发编程 1.常用类介绍 Semaphore 2.名词解释 2.1 线程安全 2.2 可重入锁和不可重入锁 java并发编程 1.常用类介绍 Semaphore Semaphore 类是 ...

  2. python下载及安装方法

    打开 http://www.python.org   找到Downlodas 点击windows   下载安装

  3. django-模板之标签

    目录 模板 模版是纯文本文件,可以生成任何基于文本的文件格式,比如HTML,XML,CSV等.Django模版语言致力于在性能和简单性上取得平衡.Django的模版系统并不是简单的将Python嵌入到 ...

  4. java JDBC自我总结

    preparedstatement和statement的区别 当不需要预编译时(不需要占位符)可以选用statement,存在不安全 当有占位符(?)时,需要选用preparedstatement s ...

  5. Django学习路28_ .html 文件继承及<block 标签>,include 'xxx.html'

    在 templates 文件夹下创建 基类 base.html <!DOCTYPE html> <html lang="en"> <head> ...

  6. 将形如 5D, 30s 的字符串转为秒

    import sys def convert_to_seconds(time_str): # write code here if 's' in time_str: return float(time ...

  7. 线程_Process基础语法

    """ Process([group[,target[,name[,args[,kwargs]]]]]) group:大多数情况下用不到 target:表示这个进程实例所 ...

  8. 【保姆级教学】新手第一次搭建vue项目和初始化

    前端项目初始化步骤 安装vue脚手架 通过vue脚手架创建项目 配置vue路由 配置Element-UI组件库 配置axios库 初始化git远程仓库 将本地项目托管到github或者码云上 通过vu ...

  9. 初学编程丨从零开始学习编程的基本路线,BAT程序员亲手总结!

    编程并不是说代码怎么写,框架怎么用,业务怎么转换为代码逻辑,这些都不是编程的要素(但却是工作的刚需......).我认为按照下面这个路线来学习编程,会使自己在学习的路途上少去很多问题(比如为啥会有多线 ...

  10. Mac上使用Docker安装SQLServer

    拉取 SQL Server 2017 Docker 镜像 docker pull microsoft/mssql-server-linux:2017-latest 运行Docker镜像 docker ...