StringRedisTemplate和RedisTemplate使用的区别

本文探讨了Spring Boot中RedisTemplate与StringRedisTemplate的区别,包括它们的序列化策略、适用数据类型及数据隔离。重点介绍了StringRedisTemplate专为String设计,而RedisTemplate支持更广泛的JDK序列化,两者数据独立且互不兼容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、配置RedisTemplate

package com.lezu.springboot.configuration;

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.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;


@Configuration
public class RedisConfig {


    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(factory);
        // key采用String的序列化方式
        template.setKeySerializer(new StringRedisSerializer());
        // hash的key也采用String的序列化方式
        template.setHashKeySerializer(new StringRedisSerializer());
        // value序列化方式采用jackson
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        // hash的value序列化方式采用jackson
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }

}

String类型的值

package com.lezu.springboot;

import com.alibaba.fastjson.JSONObject;
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.SpringJUnit4ClassRunner;

@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringbootApplicationTest {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    @Test
    public void redisTest() {
        JSONObject data = new JSONObject();
        data.put("k1","v1");
        stringRedisTemplate.opsForValue().set("product_001", "100");
        redisTemplate.opsForValue().set("product_002", "100");
    }

}

 

stringRedisTemplate存入的就是int类型

RedisTemplate<String, Object>存入的是String类型

JSON类型

    @Test
    public void redisTest() {
        JSONObject data = new JSONObject();
        data.put("k1","v1");
        stringRedisTemplate.opsForValue().set("product_001", JSON.toJSONString(data));
        redisTemplate.opsForValue().set("product_002", data);
    }

 

 

hash类型

    @Test
    public void redisTest() {
        stringRedisTemplate.opsForHash().put("k1","k1","v1");
        redisTemplate.opsForHash().put("k2","k2","v2");
    }

 

 总结两者的区别:

StringRedisTemplate继承RedisTemplate。

 

1、StringRedisTemplate是String的序列化策略。

StringRedisTemplate默认采用的是String的序列化策略,保存的key和value都是采用此策略序列化保存的。

2、RedisTemplate是JDK的序列化策略。

RedisTemplate默认采用的是JDK的序列化策略,保存的key和value都是采用此策略序列化保存的。

3、两者的数据是不共通的。
因为序列化方式不同,也就是说StringRedisTemplate只能管理StringRedisTemplate里面的数据,RedisTemplate只能管理RedisTemplate中的数据。

1、RedisTemplate不能取StringRedisTemplate存入的数据。

2、StringRedisTemplate不能取RedisTemplate存入的数据 。

文章参考:StringRedisTemplate与RedisTemplate的区别_春风化作秋雨的博客-优快云博客_redistemplate和stringredistemplate

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值