前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot缓存之使用redis作为缓存管理

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

作者头像
西西嘛呦
发布2020-08-26 15:33:40
6140
发布2020-08-26 15:33:40
举报

接上一节。

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启动器

代码语言:javascript
复制
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

(2)springboot中redis的基本命令

代码语言:javascript
复制
    @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对象

代码语言:javascript
复制
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

代码语言:javascript
复制
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就可以实现存储中文了。

看下效果:

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-02-10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器镜像服务
容器镜像服务(Tencent Container Registry,TCR)为您提供安全独享、高性能的容器镜像托管分发服务。您可同时在全球多个地域创建独享实例,以实现容器镜像的就近拉取,降低拉取时间,节约带宽成本。TCR 提供细颗粒度的权限管理及访问控制,保障您的数据安全。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档