likes
comments
collection
share

SpringBoot2.x系列教程33--SpringBoot中整合Redis实现持久化缓存效果

作者站长头像
站长
· 阅读数 19

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第7天,点击查看活动详情

前言

在上一章节中,壹哥 带大家利用默认的ConcurrentHashMap,实现了一种默认的内存级别的缓存效果。但是这种缓存方案,并没有把数据实现持久化缓存,也就是说一旦内存被释放,缓存的数据也就不存在了。所以在本章节中,我会带大家结合之前学过的Redis,带各位把数据持久化缓存到Redis中。

本案例我会直接在上一节的案例基础上进行改造。

一. Spring Boot整合Redis实现缓存

1. 创建Web项目

我们按照之前的经验,创建一个SpringBoot的Web程序,具体过程略。 SpringBoot2.x系列教程33--SpringBoot中整合Redis实现持久化缓存效果

2. 添加依赖包

我们在上一章节的基础上,添加2个新的依赖包,redis和json的。

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

        <!-- 改造:添加sql相关的依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>

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

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.39</version>
			</dependency>

3. 修改application.yml配置文件

主要是添加关于redis的配置信息,以及设置缓存类型。

cache:
  default-exp: 1000 #单位秒,缓存的过期时间
server:
  port: 8080
spring:
  application:
    name: cache-demo
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: syc
    url: jdbc:mysql://localhost:3306/spring-security?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&useSSL=false&serverTimezone=UTC
  redis:
    host: localhost
    port: 6379
    database: 0
    #password:
  cache:
    type: redis #由redis进行缓存,一共有10种缓存方案
  jpa:
    database: mysql
    show-sql: true #开发阶段,打印要执行的sql语句.
    hibernate:
      ddl-auto: update

4. 修改缓存管理器等配置类

这里我创建一个CacheConfig缓存配置类,对Redis进行基本的配置。

package com.yyg.boot.config;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
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.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.lang.reflect.Method;
import java.time.Duration;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/14
 * @Description Description
 * EnableCaching启用缓存
 */
@Configuration
@EnableCaching
public class CacheConfig {

    @Value("${cache.default-exp}")
    private long exps;

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private int port;

    //@Value("${spring.redis.timeout}")
    //private int timeout;

    //@Value("${spring.redis.password}")
    //private String password;

    @Bean
    public KeyGenerator keyGenerator() {
        return new KeyGenerator() {
            @Override
            public Object generate(Object target, Method method, Object... params) {
                StringBuffer sb = new StringBuffer();
                sb.append(target.getClass().getName());
                sb.append(method.getName());
                for (Object obj : params) {
                    sb.append(obj.toString());
                }
                return sb.toString();
            }
        };
    }

    /**
     * RedisTemplate配置
     */
    @Bean
    public RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        // 使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
        Jackson2JsonRedisSerializer<JSON> serializer = new Jackson2JsonRedisSerializer<JSON>(JSON.class);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        serializer.setObjectMapper(mapper);
        template.setValueSerializer(serializer);
        template.setHashValueSerializer(serializer);
        // 使用StringRedisSerializer来序列化和反序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }

    @Bean
    public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
        // 生成一个默认配置,通过config对象即可对缓存进行自定义配置
        RedisSerializer<String> redisSerializer = new StringRedisSerializer();
        // 使用Jackson2JsnRedisSerializer来序列化和反序列化redis的value值
        Jackson2JsonRedisSerializer<JSON> serializer = new Jackson2JsonRedisSerializer<>(JSON.class);
        // 配置序列化
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
        config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisSerializer));
        config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(serializer));
        // 设置缓存的默认过期时间
        config.entryTtl(Duration.ofSeconds(exps));
        // 不缓存空值
        config.disableCachingNullValues();
        return  RedisCacheManager.builder(redisConnectionFactory).cacheDefaults(config).build();
    }

}

5. 创建实体类

这里我们创建一个实体类,封装用户信息。

package com.yyg.boot.domain;

import lombok.Data;
import lombok.ToString;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name="user")
@Data
@ToString
public class User implements Serializable {

    //IllegalArgumentException: DefaultSerializer requires a Serializable payload
    // but received an object of type [com.syc.redis.domain.User]

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

    @Column
    private String username;

    @Column
    private String password;

}

6. 创建User仓库类

再定义一个JpaRepository,用于对User类进行CRUD操作。

package com.yyg.boot.repository;

import com.yyg.boot.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User,Long> {
}

7. 创建Service服务类

然后我们在service层定义几个方法,实现对User信息的增删改查操作。

7.1 定义UserService接口

package com.yyg.boot.service;

import com.yyg.boot.domain.User;

public interface UserService {

    User findById(Long id);

    User save(User user);

    void deleteById(Long id);

}

7.2 实现UserServiceImpl类

package com.yyg.boot.service.impl;

import com.yyg.boot.domain.User;
import com.yyg.boot.repository.UserRepository;
import com.yyg.boot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    //普通的缓存+数据库查询代码实现逻辑:
    //User user=RedisUtil.get(key);
    //   if(user==null){
    //   	  user=userDao.findById(id);
    //   	  //redis的key="product_item_"+id
    //   	  RedisUtil.set(key,user);
    //   }
    //   return user;

    /**
     *  注解@Cacheable:查询的时候才使用该注解!
     *  注意:在Cacheable注解中支持EL表达式
     *  redis缓存的key=user_1/2/3....
     *  redis的缓存雪崩,缓存穿透,缓存预热,缓存更新...
     *  condition = "#result ne null",条件表达式,当满足某个条件的时候才进行缓存
     *  unless = "#result eq null":当user对象为空的时候,不进行缓存
     */
    @Cacheable(value = "user", key = "#id", unless = "#result eq null")
    @Override
    public User findById(Long id) {

        return userRepository.findById(id).orElse(null);
    }

    /**
     * 注解@CachePut:一般用在添加和修改方法中
     * 既往数据库中添加一个新的对象,于此同时也往redis缓存中添加一个对应的缓存.
     * 这样可以达到缓存预热的目的.
     */
    @CachePut(value = "user", key = "#result.id", unless = "#result eq null")
    @Override
    public User save(User user) {
        return userRepository.save(user);
    }

    /**
     * CacheEvict:一般用在删除方法中
     */
    @CacheEvict(value = "user", key = "#id")
    @Override
    public void deleteById(Long id) {
        userRepository.deleteById(id);
    }

}

8. 创建Controller接口方法

创建一个Controller,定义几个URL接口进行测试。

package com.yyg.boot.web;

import com.yyg.boot.domain.User;
import com.yyg.boot.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping
    public User saveUser(@RequestBody User user) {
        return userService.save(user);
    }

    @GetMapping("/{id}")
    public ResponseEntity<User> getUserById(@PathVariable("id") Long id) {
        User user = userService.findById(id);
        log.warn("user="+user.hashCode());
        HttpStatus status = user == null ? HttpStatus.NOT_FOUND : HttpStatus.OK;
        return new ResponseEntity<>(user, status);
    }

    @DeleteMapping("/{id}")
    public String removeUser(@PathVariable("id") Long id) {
        userService.deleteById(id);
        return "ok";
    }

}

9. 创建入口类

最后我们创建一个入口类,启动项目。

package com.yyg.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CacheApplication {

    public static void main(String[] args) {
        SpringApplication.run(CacheApplication.class, args);
    }

}

10. 项目代码结构

如下是完整的项目代码结构,各位可以参考创建。

SpringBoot2.x系列教程33--SpringBoot中整合Redis实现持久化缓存效果

11. 重新项目进行测试

首先可以看到,我的redis中此时没有任何缓存数据。 SpringBoot2.x系列教程33--SpringBoot中整合Redis实现持久化缓存效果

接下来我在Postman中进行查询测试。 SpringBoot2.x系列教程33--SpringBoot中整合Redis实现持久化缓存效果

此时在Redis Desktop Manager中,我们重新加载一下数据,可以看到已经有了缓存的redis数据了。 SpringBoot2.x系列教程33--SpringBoot中整合Redis实现持久化缓存效果

控制台中也可以看到User的hashCode依然相同,说明我们已经成功的利用Redis实现了缓存,并且实现了持久化,因为Redis会自动把数据保存到内存和硬盘上,自带持久化策略。 SpringBoot2.x系列教程33--SpringBoot中整合Redis实现持久化缓存效果

结语

这样,壹哥 就在SpringBoot中,把数据持久化缓存到了Redis中,这样我们就不用担心数据会丢失了,而且因为有了缓存,所以效率与数据安全性都得到了保证。

今日小作业:

再次改造学生信息管理系统,实现学生缓存信息的持久化。

转载自:https://juejin.cn/post/7170469344051200030
评论
请登录