RedisTemplate和StringRedisTemplate

阅读数:62 评论数:0

跳转到新版页面

分类

python/Java

正文

1、StringRedisTemplate继承RedisTemplate

2、StringRedisTemplate默认采用的是String的序列化策略,保存的key和value都是采用此策略序列化保存的,RedisTemplate默认采用的JDK的序列化策略,保存的key和value都采用此策略序列化保存的。所以两者的数据是不共通的,也就是说StringRedisTemplaet只能管理自己里面的数据,RedisTemplate也只能管理自己里面的数据。

3、RedisTemplaet要以配置序列化,比如使用json序列化

    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;
        }
    }




相关推荐

当然可以自己写redis的工具类,或者使用第三方开源jar包或代码,这里使用spring boot的集成类。 一、pom依赖 <dependency> <gro

mvn依赖 &lt;dependency&gt; &lt;groupId

String set(String key, String value) String set(String key,

不同版本和回收资源方式 1、jedis版本&lt;3.0.0 JedisPool jedisPool

1、依赖 &lt;dependency&gt; &lt;groupId&gt;org.springf

使用方式1. 使用lambda表达式 public byte[] get(byte[]

一、简介 分布式锁,其原理就是多台机器去争抢一个资源,谁争抢成功,那么就持有这把锁。 可以通过多种途径实现分布式锁,例如数据库,插入一条记录(唯一索引),谁插入成功,谁就持有;还可以通过zookeep

一、解决方法 这个警告大致的意思是拆箱时有可能空指异常。 改成下面这种方式 if (Boolean.TRUE.equals(redisTemplate.hasKey(XXXX)) { } 二、jav

一、简介 Spring Cache 提供了 @Cacheable 、@CachePut 、@CacheEvict 、@Caching 等注解,在方法上使用。 核心