redisTemplate一opsForValue操作

当我们的数据存储到Redis的时候,我们的键(key)和值(value)都是通过Spring提供的Serializer序列化到数据库的。RedisTemplate默认使用的是JdkSerializationRedisSerializer,StringRedisTemplate默认使用的是StringRedisSerializer。

Spring-data-redis的序列化类有下面这几个:GenericToStringSerializer: 可以将任何对象泛化为字符串并序列化;Jackson2JsonRedisSerializer: 跟JacksonJsonRedisSerializer实际上是一样的;JacksonJsonRedisSerializer: 序列化object对象为json字符串;JdkSerializationRedisSerializer: 序列化java对象(被序列化的对象必须实现Serializable接口);StringRedisSerializer: 简单的字符串序列化;GenericToStringSerializer:类似StringRedisSerializer的字符串序列化;GenericJackson2JsonRedisSerializer:类似Jackson2JsonRedisSerializer,但使用时构造函数不用特定的类参考以上序列化,自定义序列化类;

Spring Data JPA为我们提供了下面的Serializer:GenericToStringSerializer、Jackson2JsonRedisSerializer、JacksonJsonRedisSerializer、JdkSerializationRedisSerializer、OxmSerializer、StringRedisSerializer。

JdkSerializationRedisSerializer: 使用JDK提供的序列化功能。优点是反序列化时不需要提供类型信息(class),但缺点是需要实现Serializable接口,还有序列化后的结果非常庞大,是JSON格式的5倍左右,这样就会消耗redis服务器的大量内存。

Jackson2JsonRedisSerializer: 使用Jackson库将对象序列化为JSON字符串。优点是速度快,序列化后的字符串短小精悍,不需要实现Serializable接口。但缺点也非常致命,那就是此类的构造函数中有一个类型参数,必须提供要序列化对象的类型信息(.class对象)。 通过查看源代码,发现其只在反序列化过程中用到了类型信息。

key和hashKey:推荐使用     StringRedisSerializer: 简单的字符串序列化

hashValue:推荐使用     GenericJackson2JsonRedisSerializer:类似Jackson2JsonRedisSerializer,但使用时构造函数不用特定的类


/**

* redis配置类

*/

@Configuration

public class RedisConfig {


    @Bean

    @SuppressWarnings("all")

    public RedisTemplate redisTemplate(RedisConnectionFactory factory) {


        RedisTemplate template = new RedisTemplate();

        template.setConnectionFactory(factory);

        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

        ObjectMapper om = new ObjectMapper();

        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);

        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

        jackson2JsonRedisSerializer.setObjectMapper(om);

        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();

        // key采用String的序列化方式

        template.setKeySerializer(stringRedisSerializer);

        // hash的key也采用String的序列化方式

        template.setHashKeySerializer(stringRedisSerializer);

        // value也采用String的序列化方式

        template.setValueSerializer(stringRedisSerializer);

        // hash的value序列化方式采用jackson

        template.setHashValueSerializer(jackson2JsonRedisSerializer);

        template.afterPropertiesSet();

        return template;


    }

}


//redisTemplate string类型数据读取 V get(Object key);

        redisTemplate.delete("hello");

        redisTemplate.opsForValue().set("hello","hello");

        System.out.println(redisTemplate.opsForValue().get("hello"));  //hello


        //加入失效机制 void set(K key, V value, long timeout, TimeUnit unit);

        redisTemplate.delete("world");

        redisTemplate.opsForValue().set("world", "world", 2, TimeUnit.SECONDS);

        Thread.sleep(1000);

        System.out.println(redisTemplate.opsForValue().get("world"));  //world

        Thread.sleep(1000);

        System.out.println(redisTemplate.opsForValue().get("world"));  //null


        //k v值是否与数据匹配 Boolean setIfAbsent(K key, V value);

        System.out.println(redisTemplate.opsForValue().setIfAbsent("hello","hello")); //true

        System.out.println(redisTemplate.opsForValue().setIfAbsent("world","world")); //false


        //为多个键赋值,取值

        //void multiSet(Map < ? extends K, ? extends V > map);

        //List multiGet(Collection < K > keys);

        //Boolean multiSetIfAbsent(Map< ? extends K, ? extends V > map);

        Map map = new HashMap();

        map.put("hello1","hello1");

        map.put("hello2","hello2");

        map.put("hello3","hello3");

        redisTemplate.opsForValue().multiSet(map);

        List keys = new ArrayList();

        keys.add("hello1");

        keys.add("hello2");

        keys.add("hello3");

        List results = redisTemplate.opsForValue().multiGet(keys);

        for(String result :results){

            System.out.println(result);

        }

        map.put("hello4","hello4");

        System.out.println(redisTemplate.opsForValue().multiSetIfAbsent(map));  //false


        //设置新值并返回旧值 V getAndSet(K key, V value);

        System.out.println(redisTemplate.opsForValue().getAndSet("hello1","hello world")); //hello1

        System.out.println(redisTemplate.opsForValue().get("hello1")); //hello world


        redisTemplate.delete("hello6");

        //数据追加 Integer append(K key, String value);

        redisTemplate.opsForValue().set("hello6","hello");

        System.out.println(redisTemplate.opsForValue().get("hello6")); //hello

        Integer append = redisTemplate.opsForValue().append("hello6", "world");

        System.out.println(redisTemplate.opsForValue().get("hello6")); //helloworld


        //数据读取 String get(K key, long start, long end);

        System.out.println(redisTemplate.opsForValue().get("hello6",0,2)); //hel


        //获取字符串长度

        System.out.println(redisTemplate.opsForValue().size("hello6")); //10

}


/** 输出结果

hello

world

null

false

true

hello1

hello2

hello3

false

hello1

hello world

hello

helloworld

hel

10

*/


但是这里有一个坑

由于选择序列化器的原因 不能测试这段代码,需要测试的话可以将

// value序列化方式采用jackson

template.setValueSerializer(jackson2JsonRedisSerializer);


//整形数据增加 Long increment(K key, long delta);

//浮点型数据增加 Double increment(K key, double delta);

redisTemplate.opsForValue().set("hello5",1);

redisTemplate.opsForValue().increment("hello5",2);

System.out.println(redisTemplate.opsForValue().get("hello5")); //3

redisTemplate.opsForValue().increment("hello5",2.1);

System.out.println(redisTemplate.opsForValue().get("hello5")); //5.1


然而将序列化器修改之后,又会出来一个新的坑


redisTemplate.delete("hello6");

//数据追加 Integer append(K key, String value);

redisTemplate.opsForValue().set("hello6","hello");

System.out.println(redisTemplate.opsForValue().get("hello6")); //hello

Integer append = redisTemplate.opsForValue().append("hello6", "world");

System.out.println(redisTemplate.opsForValue().get("hello6")); //hello

//数据读取 String get(K key, long start, long end);

System.out.println(redisTemplate.opsForValue().get("hello6",0,2)); //"he

//获取字符串长度

System.out.println(redisTemplate.opsForValue().size("hello6")); //12

而且这里的append之后立刻读取的数据仍然是第一次设置的值。

而且如上图,hello6的值也会加上两个引号

你可能感兴趣的:(redisTemplate一opsForValue操作)