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就可以實現存儲中文了。

看下效果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM