zoukankan      html  css  js  c++  java
  • springboot整合redis 实现序列化

     <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>

    配置类:(实现redis的序列化)

    package com.example.mypay.config;
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.serializer.SerializerFeature;
    import org.springframework.data.redis.serializer.RedisSerializer;
    import org.springframework.data.redis.serializer.SerializationException;
    
    import java.nio.charset.Charset;
    
    public class FastJsonRedisSerializer<T> implements RedisSerializer<T> {
    
        public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
    
        private Class<T> clazz;
    
        public FastJsonRedisSerializer(Class<T> clazz) {
            super();
            this.clazz = clazz;
        }
    
        @Override
        public byte[] serialize(T t) throws SerializationException {
            if (t == null) {
                return new byte[0];
            }
            return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);
        }
    
        @Override
        public T deserialize(byte[] bytes) throws SerializationException {
            if (bytes == null || bytes.length <= 0) {
                return null;
            }
            String str = new String(bytes, DEFAULT_CHARSET);
            return (T) JSON.parseObject(str, clazz);
        }
    
    }

    配置类:

    package com.example.mypay.config;
    
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.parser.ParserConfig;
    import com.alibaba.fastjson.support.config.FastJsonConfig;
    
    import org.springframework.cache.annotation.CachingConfigurerSupport;
    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;
    
    @Configuration
    public class RedisConfig {
        RedisConfig(){
            //打开autotype功能,需要强转的类一次添加其后
            ParserConfig.getGlobalInstance()
                    .addAccept("com.example.mypay.entity");
        }
    
        @Bean
        public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
            RedisTemplate<Object,Object> template =new RedisTemplate<>();
            //template .setConnectionFactory(redisConnectionFactory);
            FastJsonRedisSerializer<Object> fastJsonRedisSerializer=new FastJsonRedisSerializer<>(Object.class);
            template.setValueSerializer(fastJsonRedisSerializer);
            template.setKeySerializer(new StringRedisSerializer());
            template.setHashValueSerializer(fastJsonRedisSerializer);
            template.setHashKeySerializer(new StringRedisSerializer());
            template.setConnectionFactory(redisConnectionFactory);
                return template;
    
        }
    
      
    }

    使用

      可以直接取出对象

    redis数据样式

  • 相关阅读:
    [HNOI 2015]菜肴制作
    [HNOI 2015]落忆枫音
    [NOIp 2009]靶形数独
    [HNOI 2010]Bounce 弹飞绵羊
    [CTSC 1999]拯救大兵瑞恩&[网络流24题]孤岛营救问题
    [SDOI 2008]Cave 洞穴勘测
    pandas 5 str 参考:https://mp.weixin.qq.com/s/Pwz9iwmQ_YQxUgWTVje9DQ
    比较工具
    当小内存遇上大量数据,你该怎么解决这个问题?
    python高性能编程 读书笔记
  • 原文地址:https://www.cnblogs.com/dkws/p/12455645.html
Copyright © 2011-2022 走看看