springboot缓存之使用redis作为缓存管理

时间:2022-07-23
本文章向大家介绍springboot缓存之使用redis作为缓存管理,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

接上一节。

1、环境准备

(1)使用docker安装redis,可参照之前的docker安装使用,然后输入以下命令下载安装redis镜像。

sudo docker pull redis

sudo docker run --name redis01 -p 6379:6379 -d redis

(2)安装redis管理工具,Redis Desktop Manager,安装完成后

自己设置个名字,输入虚拟机系统的Ip地址,默认不设置密码,点击OK即可。然后右键点击名字,选择console可进行语句测试。

(3) redis相关操作可参考之前学go语言时的。

2、整合redis

(1)引入redis启动器

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

(2)springboot中redis的基本命令

    @Autowired
    StringRedisTemplate stringRedisTemplate; //操作k,v字符串

    @Autowired
    RedisTemplate redisTemplate; //k,v都是对象

    @Test
    public void testRedis(){
        //字符串
        stringRedisTemplate.opsForValue().append("msg","hello");
        String msg = stringRedisTemplate.opsForValue().get("msg");
        System.out.println(msg);
        //列表
        stringRedisTemplate.opsForList().leftPush("name","张三");
        stringRedisTemplate.opsForList().leftPush("name","李四");
        //集合stringRedisTemplate.opsForSet()
        //哈希stringRedisTemplate.opsForHash()
        //有序集合stringRedisTemplate.opsForZSet()
    }

(3)测试保存我们的java对象

package com.gong.springbootcache;

import com.gong.springbootcache.bean.Employee;
import com.gong.springbootcache.mapper.EmployeeMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootCacheApplicationTests {

    @Autowired
    EmployeeMapper employeeMapper;

    @Autowired
    StringRedisTemplate stringRedisTemplate; //操作k,v字符串

    @Autowired
    RedisTemplate redisTemplate; //k,v都是对象

    @Autowired
    RedisTemplate<Object,Employee>  empRedisTemplate;


    @Test
    public void testRedis(){
        //字符串
        stringRedisTemplate.opsForValue().append("msg","hello");
        String msg = stringRedisTemplate.opsForValue().get("msg");
        System.out.println(msg);
        //列表
        stringRedisTemplate.opsForList().leftPush("name","张三");
        stringRedisTemplate.opsForList().leftPush("name","李四");
        //集合stringRedisTemplate.opsForSet()
        //哈希stringRedisTemplate.opsForHash()
        //有序集合stringRedisTemplate.opsForZSet()
    }

    @Test
    public void testSave(){
        Employee employee = employeeMapper.getEmpById(1);
        //默认如果保存对象,使用jdk序列化机制序列化之后的数据保存到redis中
        redisTemplate.opsForValue().set("emp-01",employee);
        //使用json格式的数据进行保存
        //(1)自己将数据以json格式保存
        //(2)redisTemplate默认的序列化规则
        empRedisTemplate.opsForValue().set("emp-02",employee);
    }
}

我们自己定义了个redisTemplate,因为使用默认的redisTemplate,存入到redis中的数据不是正常的中文,我们新建一个MyRedisConfig.java

package com.gong.springbootcache.config;

import com.gong.springbootcache.bean.Employee;
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.serializer.Jackson2JsonRedisSerializer;

@Configuration
public class MyRedisConfig {

    @Bean
    public RedisTemplate<Object,Employee> employeeRedisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<Object,Employee> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        Jackson2JsonRedisSerializer<Employee> ser = new Jackson2JsonRedisSerializer<Employee>(Employee.class);
        template.setDefaultSerializer(ser);
        return template;
    }

}

使用我们自定义的redisTempate就可以实现存储中文了。

看下效果: