1. 程式人生 > >springboot簡易使用redis快取

springboot簡易使用redis快取

寫在前面:本文將使用的是redis單節點docker環境,如需redis叢集環境請移步至https://blog.csdn.net/belonghuang157405/article/details/80934743

redis中文教程:http://www.redis.cn

專案地址:https://github.com/Blankwhiter/redis

一、搭建redis環境

拉取映象並啟動redis容器

docker pull redis
docker run -d -p 6379:6379 --name redis-single redis

二、springboot整合redis

1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId> <artifactId>redis</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>redis</name> <description>Demo project for Spring Boot</description> <parent
>
<groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!--引入springboot redis--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!--引入序列化 由於本次測試沒有引入spring-boot-starter-web模組,故引入jackson-databind <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.6</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

2.application.yml

spring:
  redis:
    host: 192.168.9.219
    port: 6379
    jedis:
      pool:
        max-active: 100
        max-idle: 10
        max-wait: -1ms
        min-idle: 5
    password:

3.User.java


public class User {

    private Integer id;

    private String name;

    private int age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

4.RedisConfig.java

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;
import java.net.UnknownHostException;

@Configuration
public class RedisConfig {

    /**
     * 重新設定redis序列化 採用json格式 預設採用的是jdk序列化
     * @param redisConnectionFactory
     * @return
     * @throws UnknownHostException
     */
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
        RedisTemplate<Object, Object> template = new RedisTemplate();
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(
                Object.class);
        template.setDefaultSerializer(jackson2JsonRedisSerializer);
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }

}

5.測試類


import com.example.redis.po.User;
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 RedisApplicationTests {


    /**
     * 操作key-value都是字串
     */
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    /**
     * 操作key-value都是物件
     */
    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 自定義序列後的RedisTemplate
     */
    @Autowired
    RedisTemplate<Object, Object> newRedisTemplate;

    /**
     * 測試儲存
     * redis常見資料型別:string-字串  list-列表 set-集合 hash-雜湊 zset-有序集合
     * stringRedisTemplate.opsForValue() 操作字串
     * stringRedisTemplate.opsForList() 操作列表
     * stringRedisTemplate.opsForSet() 操作集合
     * stringRedisTemplate.opsForHash() 操作雜湊
     * stringRedisTemplate.opsForZet() 操作有序集合
     */
    @Test
    public void testSave() {
         stringRedisTemplate.opsForValue().append("token","123");
    }

    /**
     * 測試讀取
     */
    @Test
    public void testRead() {
        String token = stringRedisTemplate.opsForValue().get("token");
        System.out.println(token);
    }

   /**
     * 自定序列化redisTemplate 寫入
     */
    @Test
    public void testNewSave(){
        User user = new User();
        user.setId(1);
        user.setName("jack");
        user.setAge(20);
        newRedisTemplate.opsForValue().set("user-one",user);

    }

    /**
     * 自定序列化redisTemplate 讀取
     */
    @Test
    public void testNewRead() {
        String token = newRedisTemplate.opsForValue().get("user-one").toString();
        System.out.println(token);
    }
}

其他資料型別請讀者自行檢視api進行測試。