SpringBoot Redis 缓存(一)

172 阅读2分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第10天,点击查看活动详情

一、前言

Redis是内存数据库,什么是内存数据库,内存数据库是相对于硬盘数据库的。您的电脑配置是内存16G,硬盘500G,Redis的数据就是放在16G内存中,Mysql数据就放在500G硬盘中。

内存读写速度快,所以Redis数据库快,那它可以做缓存和一些访问率高的数据存储。

因为是内存数据库,所以Redis存储容量小,而且断电了数据会丢失。
所以Redis不要存储数据量大的数据,对于断电数据丢失,redis有两种持久化策略(记录命令和定时刷盘),这些后续再说。

废话不多话,SpringBoot集成Redis,直接上代码。

二、目录结构和配置

2.1 目录结构

image.png

2.2 pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.xxx</groupId>
    <artifactId>spring-boot-redis</artifactId>
    <version>1.0.0</version>
    <name>spring-boot-redis</name>
    <description>spring-boot-redis project description</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
    </dependencies>

</project>
  • 就是加了这个 spring-boot-starter-data-redis依赖

2.3 application.properties

# 应用名称
spring.application.name=spring-boot-redis
server.port=8888
# redis 地址
spring.redis.host=localhost
spring.redis.port=6379
  • 主要就是redis的地址 端口配置

三、代码

RedisConfiguration


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfiguration {
    private final RedisConnectionFactory redisConnectionFactory;

    @Autowired
    public RedisConfiguration(RedisConnectionFactory redisConnectionFactory) {
        this.redisConnectionFactory = redisConnectionFactory;
    }

    @Bean()
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        return redisTemplate;
    }

    @Bean
    public StringRedisTemplate stringRedisTemplate() {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(redisConnectionFactory);
        return stringRedisTemplate;
    }

}
  • 这里主要就是注入redisTemplatestringRedisTemplate对象
  • redisTemplate 是可以操作对象的,里面配置了对象的序列化方式
  • stringRedisTemplate 只能操作字符串

UserInfo


import java.io.Serializable;

public class UserInfo implements Serializable {
    private static final long serialVersionUID = -4514567603228180464L;

    private Long id;
    private String username;
    private String password;

    public UserInfo(Long id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "UserInfo{" +
                "id=" + id +
                ", username='" + username + ''' +
                ", password='" + password + ''' +
                '}';
    }
}