Spring Boot demo系列 :Redis缓存

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 目录Spring Boot demo系列 :Redis缓存12.2.1 手动添加`@class`12.2.2 将实体类设置为`open`
+关注继续查看

Spring Boot demo系列 :Redis缓存


本文演示了如何在Spring Boot中将Redis作为缓存使用,具体的内容包括:

  • 环境搭建
  • 项目搭建
  • 测试
  • Redis
  • MySQL
  • MyBatis Plus


Redis安装非常简单,以笔者的Manjaro为例,直接paru安装:

paru -S redis


UbuntuCentOS之类的都提供了软件包安装:


sudo apt install redis
sudo yum install redis


如果想从源码编译安装:


wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make


Windows以及其他系统的安装可以参考此处

新建项目,加入如下依赖:

Maven


<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.2</version>
</dependency>


Gradle


implementation("com.baomidou:mybatis-plus-boot-starter:3.4.2")
implementation("mysql:mysql-connector-java:8.0.23")


项目结构:


9.png


MyBatis Plus+Redis配置类:


@Configuration
@MapperScan("com.example.demo.dao")
public class MyBatisPlusConfig {
}
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
@EnableCaching
public class RedisConfig {
    @Bean
    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setConnectionFactory(factory);
        return template;
    }

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory factory) {
        RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig()
        .serializeKeysWith(
            RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())
        ).serializeValuesWith(
            RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())
        );
        return RedisCacheManager.builder(factory).cacheDefaults(configuration).build();
    }
}


重点说一下Redis配置类,这个类主要生成两个Bean:


RedisTemplate:简化Redis操作的数据访问类

CacheManager:Spring的中央缓存管理器

其中RedisTemplate是一个模板类,第一个参数的类型是该template使用的键的类型,通常是String,第二个参数的类型是该template使用的值的类型,通常为Object或Seriazable。


setKeySerializer和setValueSerializer分别设置键值的序列化器。键一般为String类型,可以使用自带的StringRedisSerializer。对于值,可以使用自带的GenericJackson2RedisSerializer。


CacheManager的配置类似,就不重新说了。


@Getter
@Setter
@ToString
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private Integer id;
    private String name;
}


public interface UserMapper extends BaseMapper<User> {
}


@org.springframework.stereotype.Service
@Transactional
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class Service {
    private final UserMapper mapper;

    @CachePut(value = "user",key = "#user.id")
    public User save(User user){
        User oldUser = mapper.selectById(user.getId());
        if(oldUser == null){
            mapper.insert(user);
            return user;
        }
        if(mapper.updateById(user) == 1)
            return user;
        return oldUser;
    }

    @CacheEvict(value = "user",key = "#id")
    public boolean delete(Integer id){
        return mapper.deleteById(id) == 1;
    }

    @Cacheable(value = "user",key = "#id")
    public User select(Integer id){
        return mapper.selectById(id);
    }

    @Cacheable(value="allUser",key = "#root.target+#root.methodName")
    //root.target是目标类,这里是com.example.demo.Service,root.methodName是方法名,这里是selectAll
    public List<User> selectAll(){
        return mapper.selectList(null);
    }
}


注解说明如下:

  • @CachePut:执行方法体再将返回值缓存,一般用于更新数据
  • @CacheEvict:删除缓存,一般用于删除数据
  • @Cacheable:查询缓存,如果有缓存就直接返回,没有缓存的话执行方法体并将返回值存入缓存,一般用于查询数据


三个注解都涉及到了key以及value属性,实际上,真正的存入Rediskey是两者的组合,比如:


@Cacheable(value="user",key="#id")


则存入的Redis中的key为:


10.png


而存入对应的值为方法返回值序列化后的结果,比如如果返回值为User,则会被序列化为:


11.png


spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: 123456
  redis:
    database: 0
    host: 127.0.0.1
    port: 6379
logging:
  level:
    com.example.demo: debug


spring.redis.database指定数据库的索引,默认为0,hostport分别指定主机(默认本地)以及端口(默认6379)。

也就是说,简单配置的话可以完全省略Redis相关配置,仅指定数据库连接url、用户名以及密码:


spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: 123456
logging:
  level:
    com.example.demo: debug


Redis服务器启动需要一个配置文件,默认位置为/etc/redis.conf(源码编译安装的话在源文件夹内),建议先复制一份:


cp /etc/redis.conf ~/Desktop/


默认的配置文件为单机Redis配置,端口6379redis-server可以直接运行:


sudo redis-server redis.conf


连接可以通过自带的redis-cli命令:


redis-cli -h localhost -p 6379


默认情况下可以直接使用


redis-cli


连接。

基本操作:

  • keys *:查询所有键
  • get key:查询key所对应的值
  • flushall:清空所有键


12.png


@SpringBootTest
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
class DemoApplicationTests {

    private final Service service;

    @Test
    void select() {
        service.select(1);
        service.select(1);
    }

    @Test
    void selectAll(){
        service.selectAll();
        service.selectAll();
    }

    @Test
    void delete(){
        service.delete(1);
    }

    @Test
    void save(){
        User user = new User(1,"name1");
        service.save(user);
        service.select(user.getId());
        user.setName("name2");
        service.save(user);
        service.select(user.getId());
    }
}


执行其中的select,会发现MyBatis Plus只有一次select的输出,证明缓存生效了:


13.png


而把缓存注解去掉后,会有两次select输出:


14.png


其它测试方法就不截图了,原理类似。

其实@Cacheable/@CacheEvict/@CachePut中的value都是String [],在Java中可以直接写上value,在Kotlin中需要[value]

序列化到Redis时,实体类会被加上一个@class字段:


15.png


这个标识供Jackson反序列化时使用,笔者一开始的实体类实现是:


data class User(var id:Int?=null, var name:String="")


但是序列化后不携带@class字段:


16.png


在反序列化时直接报错:

Could not read JSON: Missing type id when trying to resolve subtype of [simple type, class java.lang.Object]: missing type id property '@class'
 at [Source: (byte[])"{"id":1,"name":"name2"}"; line: 1, column: 23]; nested exception is com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Missing type id when trying to resolve subtype of [simple type, class java.lang.Object]: missing type id property '@class'
 at [Source: (byte[])"{"id":1,"name":"name2"}"; line: 1, column: 23]


解决方法有两个:

  • 手动添加@class字段
  • 将实体类设为open


12.2.1 手动添加@class


准确来说并不是手动添加,而是让注解添加,需要添加一个类注解@JsonTypeInfo


@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
data class User(var id:Int?=null, var name:String="")


该注解的use用于指定类型标识码,该值只能为JsonTypeInfo.Id.CLASS


12.2.2 将实体类设置为open


在Java中,实体类没有任何额外配置,Redis序列化/反序列化一样没有问题,是因为值序列化器GenericJackson2JsonRedisSerializer,该类会自动添加一个@class字段,因此不会出现上面的问题。


但是在Kotlin中,类默认不是open的,也就是无法添加@class字段,因此便会反序列化失败,解决方案是将实体类设置为open:


open class User(var id:Int?=null, var name:String="")

但是缺点是不能使用data class了。

Java版:

Kotlin版:


相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
8天前
|
缓存 NoSQL Java
SSM之Spring注解式缓存Redis以及redies中的击穿,雪崩,穿的三种现象
SSM之Spring注解式缓存Redis以及redies中的击穿,雪崩,穿的三种现象
|
8天前
|
缓存 NoSQL Java
SSM之spring注解式缓存redis->redis整合,redis的注解式开发及应用场景,redis的击穿穿透雪崩
SSM之spring注解式缓存redis->redis整合,redis的注解式开发及应用场景,redis的击穿穿透雪崩
33 0
|
8天前
|
缓存 NoSQL Java
SSM之spring注解式缓存redis
SSM之spring注解式缓存redis
22 0
|
24天前
|
缓存 NoSQL Java
Redis之与SSM集成Spring注解式缓存
Redis之与SSM集成Spring注解式缓存
54 0
|
25天前
|
缓存 NoSQL Java
SSM之spring注解式缓存redis
SSM之spring注解式缓存redis
245 0
|
1月前
|
缓存 NoSQL Java
SSM之spring注解式缓存redis
SSM之spring注解式缓存redis
183 0
|
1月前
|
缓存 NoSQL Java
SSM之spring注解式缓存redis
SSM之spring注解式缓存redis
46993 13
|
2月前
|
缓存 Java Spring
缓存 - Spring Boot 整合 Caffeine 不完全指北
缓存 - Spring Boot 整合 Caffeine 不完全指北
81 0
|
2月前
|
缓存 Java Go
解决Spring Data JPA查询存在缓存问题及解决方案
解决Spring Data JPA查询存在缓存问题及解决方案
145 0
|
3月前
|
缓存 NoSQL Java
Spring Cache简化缓存开发
Spring Cache简化缓存开发
99 0
相关产品
云迁移中心
推荐文章
更多