Spring Data Redis 教程

RedisTemplate 简介

RedisTemplate 是简化 Redis 数据库访问代码的助手类,也是 Spring Data Redis 对 Redis 支持的中心类。

RedisTemplate 将在给定对象和 Redis 存储中的底层二进制数据之间执行自动序列化(Serialization)/反序列化(Deserialization)操作。默认情况下,它对其对象使用 Java 序列化(通过 JdkSerializationRedisSerializer)。注意:对于字符串密集型操作,请考虑字符串专用的 StringRedisTemplate 模板类。

RedisTemplate 的核心方法是 execute,它支持实现 RedisCallback 接口,在接口实现中实现 Redis 访问代码。它还提供了 RedisConnection 处理,使得 RedisCallback 实现和调用代码都不需要显式地关心检索/关闭 Redis 连接,或者处理连接生命周期异常。对于典型的单步操作,有各种方便的方法。

RedisTemplate 一旦配置好,这个类就是线程安全的。

注意:虽然模板是通用的,但它取决于序列化程序/反序列化程序来正确地将给定对象与二进制数据相互转换。

构造一个 RedisTemplate

RedisTemplate 类仅提供了一个无参的构造方法,定义如下:

public RedisTemplate()

可以通过该构造方法构造一个新的 RedisTemplate 实例。代码如下:

RedisTemplate template = new RedisTemplate();

示例代码

通过手动创建 RedisTemplate,直接在 main 方法中进行测试。

添加依赖

该示例是在 Spring Boot 基础上进行测试,maven 依赖如下:

<?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
         https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.hxstrive.redis</groupId>
    <artifactId>redis_learn_spring_data</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>redis_learn_spring_data</name>
    <description>Spring Boot for redis</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- Spring Data Redis 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

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

        <!-- 这里手动添加的 jedis 依赖 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.10.2</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

注意:如果此时使用 jedis 3.+ 版本,则会抛出 Caused by: java.lang.ClassNotFoundException: redis.clients.util.Pool 错误。因此,需要将引入 jedis 2.10.2 版本。

Java 代码

package com.hxstrive.redis.manual;

import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * 手动创建 RedisTemplate
 * @author hxstrive.com 2022/8/6
 */
public class ManualCreateRedisTemplate {

    public static void main(String[] args) {
        // Redis 连接
        RedisStandaloneConfiguration config = 
                new RedisStandaloneConfiguration("127.0.0.1", 6379);
        RedisConnectionFactory connectionFactory = new JedisConnectionFactory(config);

        // 手动创建 RedisTemplate
        RedisTemplate<String,String> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);
        // 设置序列化
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.setDefaultSerializer(new StringRedisSerializer());
        redisTemplate.afterPropertiesSet();

        // 写入字符串 “hello world” 到 Redis
        ValueOperations<String,String> ops = redisTemplate.opsForValue();
        ops.set("manualKey", "hello world");

        // 从 redis 获取数据
        String value = ops.get("manualKey");
        System.out.println(value);
    }

}

运行示例输出结果:

hello world
说说我的看法
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号