zoukankan      html  css  js  c++  java
  • 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就可以实现存储中文了。

    看下效果:

  • 相关阅读:
    数据结构-循环队列程序演示
    C++进阶:新人易入的那些坑 --1.常量、常指针和指针常量
    this.$confirm的用法
    属性或方法“degreeList”不是在实例上定义的,而是在渲染期间引用的。通过初始化该属性,确保该属性是反应性的,无论是在data选项中,还是在基于类的组件中
    CSS清除浮动
    react里的高阶组件
    map和forEach的区别
    hash和history两种模式的区别
    js原型链的理解
    for..in,for..of 和forEach的区别
  • 原文地址:https://www.cnblogs.com/xiximayou/p/12291542.html
Copyright © 2011-2022 走看看