redis--StringRedisTemplate和RedisTemplate区别

StringRedisTemplate:

1)、只能存储string类型的值,因此不能存储如对象

2)、序列化为string,如:

 

 

RedisTemplate:

1)、可以存储任意类型,含对象。

2)、序列化采用jdk的,如:

redisTemplate.opsForValue().set("key-0", "hello");

redisTemplate.opsForValue().set("key-1", User.getSampleUser());

public static User getSampleUser() {
        User user = new User();
        user.setId(123);
        user.setName("遥远2");
        return user;
    }

 

 

在java中能正确读取,但是在client中就是jdk序列化后的字符了,不利于查看。

 

======================================

RedisTemplate使用总结:

1、如果采用默认的jdk序列化,可以不用自行指定序列化即不用再RedisConfig中配置序列化方式。但是,

@Qualifier("redisTemplate")//必须加此行
private RedisTemplate redisTemplate;//不能为RedisTemplate<K,V>

一定要加@Qualifier("redisTemplate"),否则会当成StringRedisTemplate,因而报错。

2、RedisTemplate也可当做StringRedisTemplate使用,但需要配置成string序列化:

@Configuration
public class RedisConfig {
    
    @Bean("strRedisTemplate")
    public RedisTemplate<Object,Object> strRedisTemplate(RedisConnectionFactory redisConectionFactory) {
        RedisTemplate<Object,Object> template = new RedisTemplate<Object,Object>();
        template.setConnectionFactory(redisConectionFactory);
        RedisSerializer<String> stringSerializer = new StringRedisSerializer();
        template.setKeySerializer(stringSerializer);//对key序列化
        template.setValueSerializer(stringSerializer);//对value序列化
        return template;
    }
}

但,意义不大,如果用的value是string的,直接使用StringRedisTemplate即可

3、RedisTemplate对象序列化

import org.springframework.core.convert.converter.Converter;
import org.springframework.core.serializer.support.DeserializingConverter;
import org.springframework.core.serializer.support.SerializingConverter;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

public class RedisObjectSerializer implements RedisSerializer<Object> {
    private Converter<Object,byte[]> serializer = new SerializingConverter();
    private Converter<byte[],Object> deserializer = new DeserializingConverter();
    private static final byte[] EMPTY_ARRAY = new byte[0];
    
    @Override
    public Object deserialize(byte[] bytes) throws SerializationException {
        if (isEmpty(bytes)) {
            return null;
        }
        return deserializer.convert(bytes);
    }
    
    @Override
    public byte[] serialize(Object t) throws SerializationException {
        if (t == null) {
            return EMPTY_ARRAY;
        }
        return serializer.convert(t);
    }
    private boolean isEmpty(byte[] data) {
        return (data == null || data.length == 0);
    }
}

发现,插入后,依然是默认的jdk序列化。如图:

所以,此方式也不建议使用

4、json序列化

参考:https://github.com/alibaba/fastjson/wiki/%E5%9C%A8-Spring-%E4%B8%AD%E9%9B%86%E6%88%90-Fastjson

 

分别对key、value序列化:

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;

import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;

@Configuration
public class RedisConfig {
    
    @Bean("jsonRedisTemplate")
    public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConectionFactory) {
        RedisTemplate<Object,Object> template = new RedisTemplate<Object,Object>();
        template.setConnectionFactory(redisConectionFactory);
        
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericFastJsonRedisSerializer());
        
        //template.setDefaultSerializer(new FastJsonRedisSerializer(Object.class));
        return template;
    }
}

 

使用默认的:

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

@Configuration
public class RedisConfig {
    
    @Bean("jsonRedisTemplate")
    public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConectionFactory) {
        RedisTemplate<Object,Object> template = new RedisTemplate<Object,Object>();
        template.setConnectionFactory(redisConectionFactory);
        
        //template.setKeySerializer(new StringRedisSerializer());
        //template.setValueSerializer(new GenericFastJsonRedisSerializer());
        
        template.setDefaultSerializer(new GenericFastJsonRedisSerializer());
        return template;
    }
}

 

 

 此时,在redis-cli中,执行命令:

必须为key加上 \",才可以获取值。因此,最后总结:

当value存储的是string类型时,使用StringRedisTemplate。

否则,使用RedisTemplate,且,即使 使用RedisTemplate,也必须分别对key、value做序列化,而不是使用一个默认的,即:

 

转载于:https://www.cnblogs.com/yaoyuan2/p/9811654.html

### 回答1: stringredistemplateredistemplate都是RedisJava客户端库中的类。它们都提供了操作Redis的方法,但是在具体的使用场景中可能会有不同的选择。 stringredistemplate主要用于操作Redis中的字符串类型数据,如set、get、incr等操作。而redistemplate则提供了更为通用的操作方法,可以操作Redis中的各种数据类型,如hash、list、set、zset等。 因此,在使用Redis时,需要根据具体的业务需求选择合适的客户端库类。 ### 回答2: stringredistemplateredistemplate都是Spring Data Redis框架中提供的Redis客户端操作模板。二者的主要区别在于操作的Redis数据类型不同。 stringredistemplate用于操作Redis的字符串类型数据,其中定义了一些常用的字符串类型操作方法,比如set、get、append、increment等等。我们可以通过stringredistemplate来实现类似于缓存等场景下的读写操作。 redistemplate则可以操作Redis的所有数据类型,包括字符串、列表、哈希、集合等等。它提供了各种类型数据的操作方法,例如: - 支持Redis列表数据类型的leftPush、rightPush等 - 支持Redis哈希数据类型的put、delete等 - 支持Redis集合数据类型的add、remove、members等 redistemplate可以通过RedisCallback接口更加灵活地操作Redis,可以使用Lambda表达式或匿名内部类形式的实现RedisCallback接口的回调函数,进行复杂的Redis操作。 正因为stringredistemplateredistemplate都是Spring Data Redis的组件,因此使用起来都非常方便,并且相互兼容。如果需要仅操作字符串类型数据,那么可以只使用stringredistemplate;如果需要操作多种类型的Redis数据,那么可以使用redistemplate。同时,根据具体的业务场景和实际需要,在使用这两个操作模板时,我们应该根据具体需要来进行优化和选择,以达到更好的性能和效果。 ### 回答3: stringredistemplateredistemplate都是RedisJava客户端,用于操作Redis数据库stringredistemplate是spring-data-redis框架中的一个模板对象,一般用于对Redis中的字符串类型数据进行操作。其提供了一系列的方法如set、get、increment、append、getBit等用于对字符串数据进行增、删、改、查等操作。同时还为你内置了串行化器,在通过stringredistemplateRedis缓存中写入数据时,将使用此内部串行化器将数据序列化成字节数组,以便在Redis中存储数据。 redistemplate也是spring-data-redis框架中的一个模板对象,与stringredistemplate类似,也提供了一系列的方法,以方便用户对Redis数据库进行数据操作。不过,与stringredistemplate不同的是,redistemplate可以对Redis中所有类型的数据进行操作,包括字符串、哈希、列表、集合和有序集合、通用对象等多种类型。因此,redistemplate是一个更为通用的Redis客户端。 使用上,需要在应用程序中配置redistemplate的实例,并且根据需要提供Redis连接池等配置。之后就可以通过redistemplate提供的方法对Redis数据库进行数据操作。对于stringredistemplate,其实例的创建方式和redistemplate类似,只需要区别在于实例对象的类型不同即可。 因此,从使用的角度来看,stringredistemplate主要适用于对Redis中的字符串数据进行操作,而redistemplate则更加通用,适用于对Redis中各种类型的数据进行操作。在实际开发中,根据需要选择使用不同的模板对象,能够更好地提高开发效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值