Spring Boot 2.1.0.RELEASE 整合 Redis

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qmqm011/article/details/88169994

一、添加Maven依赖

完整的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.wuychn</groupId>
    <artifactId>spring-boot-redis</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

</project>

二、编写配置文件

完整的application.yml文件如下:

server:
  port: 8080
spring:
  application:
    name: spring-data-redis
  redis:
    host: 192.168.11.59
    port: 6379

spring.redis.host和spring.redis.port如果不指定,默认会连接本机的6379端口。如果Redis有密码,还需要指定密码(spring.redis.host)。Spring Boot对Redis默认的配置在RedisProperties类中,包括连接池、哨兵等配置,可以参考RedisProperties源码。

三、使用

经过以上配置,就可以操作Redis中的数据了。可以直接注入RedisTemplate来操作数据:

package com.wuychn;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class SpringBootRedisApplication {

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    public static void main(String[] args) {
        SpringApplication.run(SpringBootRedisApplication.class, args);
    }

    @GetMapping("/set")
    public String set(String value) {
        redisTemplate.opsForValue().set("hello", value);
        return "success";
    }

    @GetMapping("/get")
    public String get() {
        Object value = redisTemplate.opsForValue().get("hello");
        return value.toString();
    }

}

启动项目,在浏览器中输入http://localhost:8080/set?value=world,结果如下:

在Redis中查看:

说明数据已经保存到Reids中了。

在浏览器中输入http://localhost:8080/get,结果:

四、指定序列化方式

RedisTemplate默认使用的是JDK的序列化,可以修改为指定的序列化方式,常见的序列化方式有Jackson2JsonRedisSerialize、GenericFastJsonRedisSerializer等。这里使用GenericFastJsonRedisSerializer序列化方式。

首先需要添加如下依赖:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.54</version>
</dependency>

编写配置类RedisConfig,内容如下:

package com.wuychn;

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
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.StringRedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);
        GenericFastJsonRedisSerializer genericFastJsonRedisSerializer = new GenericFastJsonRedisSerializer();
        redisTemplate.setValueSerializer(genericFastJsonRedisSerializer);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

之后重启项目,再次执行前文中的操作,可以看到Redis中存储的数据格式发生了变化:

五、RedisTemplate介绍

Spring封装了RedisTemplate对象来进行对Redis的各种操作,它支持所有的Redis原生的api。RedisTemplate中定义了对5种数据结构的操作:

redisTemplate.opsForValue(); // 操作字符串
redisTemplate.opsForHash(); // 操作hash
redisTemplate.opsForList(); // 操作list
redisTemplate.opsForSet(); // 操作set
redisTemplate.opsForZSet(); // 操作有序set

最常用的是opsForValue(),它返回的是一个ValueOperations类型的对象,ValueOperations中定义了set(k, v)、get(k)等方法用于设置、获取值,set还有重载的方法,可以指定key的过期时间。具体可以参考ValueOperations源码。

除了RedisTemplate,还有一个StringRedisTemplate,后者继承自RedisTemplate,两者的数据是不共通的。也就是说StringRedisTemplate只能管理StringRedisTemplate里面的数据,RedisTemplate只能管理RedisTemplate中的数据。StringRedisTemplate默认采用的是String的序列化策略,保存的key和value都是采用此策略序列化保存的。RedisTemplate默认采用的是JDK的序列化策略,保存的key和value都是采用此策略序列化保存的。

猜你喜欢

转载自blog.csdn.net/qmqm011/article/details/88169994