SpringBoot integrates Redis to implement data caching

1. Know Spring Cache

Spring Cache is a set of caching solutions provided by Spring. It does not provide cache implementation itself, but provides a unified interface and code specifications, configuration, annotations, etc., in order to integrate various Cache solutions, so that users do not need to care about the details of Cache.

Spring supports "transparently" adding a cache to the application, applying the cache to the method, and checking whether there is available data in the cache before the method is executed. This can reduce the number of method executions and increase the speed of response. The application of the cache is "transparent" and will not cause any interference to the caller. As long as the caching support is enabled through the annotation @EnableCaching, Spring Boot will automatically handle the basic configuration of the cache.

Spring Cache acts on methods. When a cache method is called, the method parameters and the return result will be stored in the cache as a "key/value" (key / value), and the method will no longer be executed when the method is called with the same parameters next time. Instead, get the result directly from the cache and return it. Therefore, when using Spring Cache, you must ensure that the same results are returned when the cached method and method parameters are the same.

The declarative cache annotations provided by Spring Boot are as follows:

annotation Description
@EnableCaching Turn on caching.
@Cacheable It can be used on classes and methods to cache the return values ​​of classes or methods in the form of key-value pairs.
@CachePut The method is called and the result is cached.
@CacheEvict Empty the cache.
@Caching Used to combine multiple annotation tags.

Detailed use of declarative cache annotations: "Spring uses Cache and integrates Ehcache"

 

2. Know Redis

Redis is currently one of the most widely used memory data storage systems. It supports a richer data structure, data persistence, transaction, HA (High Available), dual-computer cluster system, master-slave library.

Redis is a key-value storage system. The value types it supports include String, List, Set, Zset (ordered set) and Hash. These data types all support push/pop, add/remove, and take intersection, union, difference or richer operations, and these operations are all atomic. On this basis, Redis supports various sorts and algorithms in different ways.

 

3. Create a SpringBoot project and data table

[Example] Integrate SpringBoot and MyBaits framework, and use Redis as cache to implement user data related operation functions, such as adding users, querying user information based on user ID, updating user information, deleting users based on user ID, etc. As shown below:

3.1 Create a project

(1) Create a SpringBoot project, the project structure is as follows:

(2) Use Maven to add dependent files

In the pom.xml configuration information file, add Redis, MyBatis, MySQL database, Thymeleaf template engine and other related dependencies:

<!-- Redis启动器 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>2.3.4.RELEASE</version>
</dependency>

<!-- MyBatis与SpringBoot整合依赖 -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.3</version>
</dependency>

<!-- MySQL的JDBC数据库驱动 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.20</version>
</dependency>

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

3.2 Create a data table

Use MySQL database, create tb_user user information table, and add data.

-- 判断数据表是否存在,存在则删除
DROP TABLE IF EXISTS tb_user;
 
-- 创建“用户信息”数据表
CREATE TABLE IF NOT EXISTS tb_user
( 
	user_id INT AUTO_INCREMENT PRIMARY KEY COMMENT '用户编号',
	user_name VARCHAR(50) NOT NULL COMMENT '用户姓名',
	age int default(0) NOT NULL COMMENT '年龄',
	blog_url VARCHAR(50) NOT NULL COMMENT '博客地址',
	blog_remark VARCHAR(50) COMMENT '博客信息'
) COMMENT = '用户信息表';
 
-- 添加数据
INSERT INTO tb_user(user_name,age,blog_url,blog_remark) VALUES('pan_junbiao的博客',32,'https://blog.csdn.net/pan_junbiao','您好,欢迎访问 pan_junbiao的博客');

 

4. Redis implements data caching

4.1 Redis configuration

First, configure the Spring target cache manager in the application.yml configuration file to support Ehcache, Generic, Redis, Jcache, etc. This configuration uses Redis.

#Spring配置
spring:
  #缓存管理器
  cache:
    type: redis

Configure Redis and MyBatis in the application.yml configuration file. The complete configuration information is as follows:

#Spring配置
spring:
  #缓存管理器
  cache:
    type: redis
  #Redis配置
  redis:
    database: 0 #Redis数据库索引(默认为0)
    host: 127.0.0.1 #Redis服务器地址
    port: 6379 #Redis服务器连接端口
    password:  #Redis服务器连接密码(默认为空)
    jedis:
      pool:
        max-active: 8 #连接池最大连接数(使用负值表示没有限制)
        max-wait: -1s #连接池最大阻塞等待时间(使用负值表示没有限制)
        max-idle: 8  #连接池中的最大空闲连接
        min-idle: 0 #连接池中的最小空闲连接
    lettuce:
      shutdown-timeout: 100ms #关闭超时时间,默认值100ms
  #使用Thymeleaf模板引擎
  thymeleaf:
    mode: HTML5
    encoding: UTF-8
    cache: false  #使用Thymeleaf模板引擎,关闭缓存
    servlet:
      content-type: text/html
  #DataSource数据源
  datasource:
    url: jdbc:mysql://localhost:3306/db_admin?useSSL=false&amp
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver

#MyBatis配置
mybatis:
  type-aliases-package: com.pjb.entity #别名定义
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #指定 MyBatis 所用日志的具体实现,未指定时将自动查找
    map-underscore-to-camel-case: true #开启自动驼峰命名规则(camel case)映射
    lazy-loading-enabled: true #开启延时加载开关
    aggressive-lazy-loading: false #将积极加载改为消极加载(即按需加载),默认值就是false
    #lazy-load-trigger-methods: "" #阻挡不相干的操作触发,实现懒加载
    cache-enabled: true #打开全局缓存开关(二级环境),默认值就是true

4.2 Redis configuration class (config layer)

In the com.pjb.config package, create a RedisConfig class (Redis configuration class) and inherit the CachingConfigurerSupport class.

package com.pjb.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
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.Jackson2JsonRedisSerializer;

import java.lang.reflect.Method;

/**
 * Redis配置类
 * @author pan_junbiao
 **/
@Configuration
public class RedisConfig extends CachingConfigurerSupport
{
    /**
     * 缓存对象集合中,缓存是以key-value形式保存的,
     * 当不指定缓存的key时,SpringBoot会使用keyGenerator生成Key。
     */
    @Bean
    public KeyGenerator keyGenerator()
    {
        return new KeyGenerator()
        {
            @Override
            public Object generate(Object target, Method method, Object... params) {
                StringBuilder sb = new StringBuilder();
                //类名+方法名
                sb.append(target.getClass().getName());
                sb.append(method.getName());
                for (Object obj : params) {
                    sb.append(obj.toString());
                }
                return sb.toString();
            }
        };
    }

    /**
     * 缓存管理器
     */
    @SuppressWarnings("rawtypes")
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory connectionFactory)
    {
        RedisCacheManager cacheManager = RedisCacheManager.create(connectionFactory);
        //设置缓存过期时间

        return cacheManager;
    }

    /**
     * 实例化RedisTemplate对象
     */
    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory)
    {
        StringRedisTemplate template = new StringRedisTemplate(factory);
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }
}

4.3 Create an entity class (Entity layer)

In the com.pjb.entity package, create a UserInfo class (user information entity class).

package com.pjb.entity;

import java.io.Serializable;

/**
 * 用户信息实体类
 * @author pan_junbiao
 **/
public class UserInfo implements Serializable
{
    private int userId; //用户编号
    private String userName; //用户姓名
    private int age; //年龄
    private String blogUrl; //博客地址
    private String blogRemark; //博客信息

    //省略getter与setter方法...
}

Note: The entity class must implement the Serializable interface, otherwise the cache function cannot be implemented.

4.4 Database mapping layer (Mapper layer)

In the com.pjb.mapper package, create the UserMapper interface (user information Mapper dynamic proxy interface).

package com.pjb.mapper;

import com.pjb.entity.UserInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

/**
 * 用户信息Mapper动态代理接口
 * @author pan_junbiao
 **/
@Mapper
@Repository
public interface UserMapper
{
    /**
     * 根据用户ID,获取用户信息
     */
    @Select("SELECT * FROM tb_user WHERE user_id = #{userId}")
    public UserInfo getUserById(int userId);

    /**
     * 新增用户,并获取自增主键
     */
    @Insert("INSERT INTO tb_user(user_name,age,blog_url,blog_remark) VALUES(#{userName},#{age},#{blogUrl},#{blogRemark});")
    @Options(useGeneratedKeys = true, keyColumn = "user_id", keyProperty = "userId")
    public int insertUser(UserInfo userInfo);

    /**
     * 修改用户
     */
    @Update("UPDATE tb_user SET user_name = #{userName} ,age = #{age} ,blog_url = #{blogUrl} ,blog_remark = #{blogRemark} WHERE user_id = #{userId}")
    public int updateUser(UserInfo userInfo);

    /**
     * 删除用户
     */
    @Delete("DELETE FROM tb_user WHERE user_id = #{userId}")
    public int deleteUser(int userId);
}

4.5 Business logic layer (Service layer)

Create the UserService interface (user information business logic interface) under the com.pjb.service package.

package com.pjb.service;

import com.pjb.entity.UserInfo;

/**
 * 用户信息业务逻辑接口
 * @author pan_junbiao
 **/
public interface UserService
{
    /**
     * 根据用户ID,获取用户信息
     */
    public UserInfo getUserById(int userId);

    /**
     * 新增用户,并获取自增主键
     */
    public UserInfo insertUser(UserInfo userInfo);

    /**
     * 修改用户
     */
    public UserInfo updateUser(UserInfo userInfo);

    /**
     * 删除用户
     */
    public int deleteUser(int userId);
}

Under the com.pjb.service.impl package, create the UserServiceImpl class (user information business logic class).

package com.pjb.service.impl;

import com.pjb.entity.UserInfo;
import com.pjb.mapper.UserMapper;
import com.pjb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户信息业务逻辑类
 * @author pan_junbiao
 * 缓存就在这层工作
 * @Cacheable,将查询结果缓存到redis中,(key="#p0")指定传入的第一个参数作为redis的key。
 * @CachePut,指定key,将更新的结果同步到redis中
 * @CacheEvict,指定key,删除缓存数据,allEntries=true,方法调用后将立即清除缓存
 **/
@CacheConfig(cacheNames = "userCache")
@Service
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = Exception.class)
public class UserServiceImpl implements UserService
{
    @Autowired
    private UserMapper userMapper;

    /**
     * 根据用户ID,获取用户信息
     */
    @Override
    @Cacheable(key = "#p0") // #p0 表示第一个参数
    public UserInfo getUserById(int userId)
    {
        return userMapper.getUserById(userId);
    }

    /**
     * 新增用户,并获取自增主键
     */
    @Override
    @CachePut(key = "#p0.userId")
    public UserInfo insertUser(UserInfo userInfo)
    {
        userMapper.insertUser(userInfo);
        return userInfo;
    }

    /**
     * 修改用户
     */
    @Override
    @CachePut(key = "#p0.userId")
    public UserInfo updateUser(UserInfo userInfo)
    {
        userMapper.updateUser(userInfo);
        return userInfo;
    }

    /**
     * 删除用户
     * 如果在@CacheEvict注解中添加allEntries=true属性,
     * 将会删除所有的缓存
     */
    @Override
    @CacheEvict(key = "#p0")
    public int deleteUser(int userId)
    {
        return userMapper.deleteUser(userId);
    }
}

As can be seen from the above code, the method of querying users uses the @Cacheable annotation to enable caching. Adding and modifying methods use the @CachePut annotation, which processes the method first, and then caches the result. To delete data, you need to use the @CacheEvict annotation to clear the cache.

@CacheConfig annotation: If all @Cacheable() has a value="xxx" attribute, it is obviously tiring to write if there are more methods, if you can declare it all at once, it will save trouble, so there is With @CacheConfig this configuration, @CacheConfig is a class-level annotation that allows to share the cache names. If you write another name in the method, then the method name will prevail.

4.6 Controller method (Controller layer)

In the com.pjb.controller package, create the UserController class (user controller) to realize the query, addition, modification, and deletion of user data, and realize the return of data.

package com.pjb.controller;

import com.pjb.entity.UserInfo;
import com.pjb.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 用户信息控制器
 * @author pan_junbiao
 **/
@Controller
@RequestMapping("/user")
public class UserController
{
    @Autowired
    private UserService userService;

    /**
     * 获取用户信息
     */
    @RequestMapping("getUserById")
    public ModelAndView getUserById(int userId)
    {
        //根据用户ID,获取用户信息
        UserInfo userInfo = userService.getUserById(userId);

        if(userInfo==null)
        {
            userInfo = new UserInfo();
        }

        //返回结果
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("userInfo",userInfo);
        modelAndView.setViewName("/user-info.html");
        return modelAndView;
    }

    /**
     * 新增用户
     */
    @ResponseBody
    @RequestMapping("insertUser")
    public boolean insertUser()
    {
        //创建新用户
        UserInfo userInfo = new UserInfo();
        userInfo.setUserName("pan_junbiao的博客");
        userInfo.setAge(32);
        userInfo.setBlogUrl("https://blog.csdn.net/pan_junbiao");
        userInfo.setBlogRemark("您好,欢迎访问 pan_junbiao的博客");

        //执行新增方法
        userService.insertUser(userInfo);

        //返回结果
        return userInfo.getUserId() > 0 ? true : false;
    }

    /**
     * 修改用户
     */
    @ResponseBody
    @RequestMapping("updateUser")
    public boolean updateUser(int userId)
    {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(userId);
        userInfo.setUserName("pan_junbiao的博客_02");
        userInfo.setAge(35);
        userInfo.setBlogUrl("https://blog.csdn.net/pan_junbiao");
        userInfo.setBlogRemark("您好,欢迎访问 pan_junbiao的博客");

        //执行修改方法
        userService.updateUser(userInfo);

        //返回结果
        return true;
    }

    /**
     * 删除用户
     */
    @ResponseBody
    @RequestMapping("deleteUser")
    public boolean deleteUser(int userId)
    {
        //执行新增方法
        int result = userService.deleteUser(userId);

        //返回结果
        return result > 0 ? true : false;
    }
}

4.7 Display page (View layer)

Create a user-info.html user information display page under the resources/templates directory.

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>用户信息</title>
    <meta name="author" content="pan_junbiao的博客">
    <style>
        table { border-collapse: collapse; margin-bottom: 10px}
        table,table tr th, table tr td { border:1px solid #000000; padding: 5px 10px;}
    </style>
</head>
<body>
<div align="center">
    <table>
        <caption>用户信息</caption>
        <tr>
            <th>用户ID:</th>
            <td th:text="${userInfo.userId}"></td>
        </tr>
        <tr>
            <th>用户名称:</th>
            <td th:text="${userInfo.userName}"></td>
        </tr>
        <tr>
            <th>年龄:</th>
            <td th:text="${userInfo.age}"></td>
        </tr>
        <tr>
            <th>博客地址:</th>
            <td th:text="${userInfo.blogUrl}"></td>
        </tr>
        <tr>
            <th>备注信息:</th>
            <td th:text="${userInfo.blogRemark}"></td>
        </tr>
    </table>
</div>
</body>
</html>

Results of the:

(1) Use Redis to obtain user information from cached data, as shown in the following figure:

(2) Use the Redis Desktop Manager (RDM) visualization tool to query the cached data, as shown in the figure below:

 

Guess you like

Origin blog.csdn.net/pan_junbiao/article/details/108813376