当前位置: 首页 > news >正文

书店网站html模板seo服务外包公司

书店网站html模板,seo服务外包公司,flash做网站的论文,南昌营销网站公司背景 最近遇到了两个Redis相关的问题,趁着清明假期,梳理整理。 1.存入Long类型对象,在代码中使用Long类型接收,结果报类型转换错误。 2.String对象的反序列化问题,直接在Redis服务器上新增一个key-value&#xff0c…

背景

最近遇到了两个Redis相关的问题,趁着清明假期,梳理整理。

1.存入Long类型对象,在代码中使用Long类型接收,结果报类型转换错误。

2.String对象的反序列化问题,直接在Redis服务器上新增一个key-value,而后在代码中get(key)时,报反序列化失败。
关于Long类型转换错误
Redis的配置如下

Redis中序列化相关的配置,我这里采用的是GenericJackson2JsonRedisSerializer类型的序列化方式(这种方式会有一个类型转换的坑,下面会提到)

    @Configuration
    @AutoConfigureAfter(RedisAutoConfiguration.class)
    public class RedisConfiguration {
     
        @Bean
        public RedisTemplate<String, Object> redisTemplate(JedisConnectionFactory redisConnectionFactory) {
            RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
            redisTemplate.setConnectionFactory(redisConnectionFactory);
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
            redisTemplate.setHashKeySerializer(new StringRedisSerializer());
            redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
            redisTemplate.afterPropertiesSet();
            return redisTemplate;
        }
    }  


存入Long对象取出Integer对象

测试方法如下

    @Test
    public void redisSerializerLong(){
        try {
            Long longValue = 123L;
            redisLongCache.set("cacheLongValue",longValue);
            Object cacheValue = redisLongCache.get("cacheLongValue");
            Long a = (Long) cacheValue;
        }catch (ClassCastException e){
            e.printStackTrace();
        }
    }


会报类型转换错误java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long。

为什么类型会变为Integer呢?跟我一起追踪源码,便会发现问题。

1. 在代码的最外层获取redis中key对应的value值

redisTemplate.opsForValue().get(key);

2.在DefaultValueOperations类中的get(Object key)方法

    public V get(Object key) {
     
        return execute(new ValueDeserializingRedisCallback(key) {
     
            @Override
            protected byte[] inRedis(byte[] rawKey, RedisConnection connection) {
                return connection.get(rawKey);
            }
        }, true);
    }


3.打断点继续往里跟,RedisTemplate中的execute(RedisCallback<T> action, boolean exposeConnection, boolean pipeline)方法里面,有一行关键代码。

T result = action.doInRedis(connToExpose); 

此为获取redis中对应的value值,并对其进行反序列化操作。

4.在抽象类AbstractOperations<K, V>中,定义了反序列化操作,对查询结果result进行反序列化。

    public final V doInRedis(RedisConnection connection) {
        byte[] result = inRedis(rawKey(key), connection);
        return deserializeValue(result);
    }


V deserializeValue(byte[] value)反序列化

    V deserializeValue(byte[] value) {
        if (valueSerializer() == null) {
            return (V) value;
        }
        return (V) valueSerializer().deserialize(value);
    }


 5.终于到了具体实现类GenericJackson2JsonRedisSerializer

    public Object deserialize(@Nullable byte[] source) throws SerializationException {
        return deserialize(source, Object.class);
    }


实现反序列化方法,注意!这里统一将结果反序列化为Object类型,所以这里便是问题的根源所在,对于数值类型,取出后统一转为Object,导致泛型类型丢失,数值自动转为了Integer类型也就不奇怪了。

    public <T> T deserialize(@Nullable byte[] source, Class<T> type) throws SerializationException {
     
        Assert.notNull(type,
                "Deserialization type must not be null! Pleaes provide Object.class to make use of Jackson2 default typing.");
     
        if (SerializationUtils.isEmpty(source)) {
            return null;
        }
     
        try {
            return mapper.readValue(source, type);
        } catch (Exception ex) {
            throw new SerializationException("Could not read JSON: " + ex.getMessage(), ex);
        }
    }  


String对象转义问题 

测试方法

    @Test
    public void redisSerializerString() {
        try {
            String stringValue = "abc";
            redisStringCache.set("codeStringValue", stringValue);
            String cacheValue = redisStringCache.get("codeStringValue");     // 序列化失败
            String serverInsert = redisStringCache.get("serverInsertValue");
            if (Objects.equals(cacheValue, serverInsert)) {
                System.out.println("serializer ok");
            } else {
                System.out.println("serializer err");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


提前在redis服务器上插入一个非Json格式的String对象

直接在Redis服务器上使用set命令新增一对Key-Value,在代码中取出会反序列化失败, 

    org.springframework.data.redis.serializer.SerializationException: Could not read JSON: Unrecognized token 'abc': was expecting ('true', 'false' or 'null')
     at [Source: (byte[])"abc"; line: 1, column: 7]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'abc': was expecting ('true', 'false' or 'null')
     at [Source: (byte[])"abc"; line: 1, column: 7]
        at org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer.deserialize(GenericJackson2JsonRedisSerializer.java:132)
        at org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer.deserialize(GenericJackson2JsonRedisSerializer.java:110)
        at org.springframework.data.redis.core.AbstractOperations.deserializeValue(AbstractOperations.java:334)
        at org.springframework.data.redis.core.AbstractOperations$ValueDeserializingRedisCallback.doInRedis(AbstractOperations.java:60)
        at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:224)
        at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:184)
        at org.springframework.data.redis.core.AbstractOperations.execute(AbstractOperations.java:95)
        at org.springframework.data.redis.core.DefaultValueOperations.get(DefaultValueOperations.java:48)  


小总结

这个问题是因为,自己在测试的过程中,没有按照代码流程执行,想当然的认为,代码跑出来的结果和自己手动插入的结果是一样的。

在相关的测试验证过程中应该严格的控制变量,不能凭借下意识的决断来操作,谨记软件之事——必作于细!
————————————————

http://www.15wanjia.com/news/16108.html

相关文章:

  • seo网站有哪些小学生收集的新闻10条
  • 做恶搞网站软件下载广州网络推广专员
  • jsp网站开发可行性分析小说风云榜
  • 设计logo网站是平面设计不千峰培训
  • 建设一个微网站要花多少钱服务器ip域名解析
  • wordpress首页热门排行显示安卓优化大师官方下载
  • 女的和女的做那个视频网站简单的seo
  • 备案新增网站备案百度网盘网站入口
  • 购物网站做推广西安seo网站排名
  • 公司注册大概多少钱百度整站优化
  • 动态网站用什么语言做百度seo关键词排名查询
  • WordPress网站远程访问泉州百度推广排名优化
  • 帮人家做网站维护抄一则新闻四年级
  • 制作app网站推广app拿返佣的平台
  • wordpress织梦博客seo优化技术
  • 南宁做网站推广seo关键词优化推广报价表
  • 做电影网站采集什么意思app运营方案
  • 苏州吴江网站建设广州百度推广代理公司
  • 虚拟主机怎么上传网站最近最火的关键词
  • 广东专业移动网站建设哪家好重庆发布的最新消息今天
  • web 设计网站模板下载产品推广软文范文
  • 公司注册核名在哪个网站2022适合小学生的简短新闻
  • 淘宝做网站很便宜清远新闻最新
  • 好看的旅游网站模版seo营销的概念
  • 日照网站制作公司网络工程师是干什么的
  • 杭州清风室内设计学校青岛seo公司
  • 人力资源公司怎么开seo网络推广员招聘
  • 做1688网站到哪里找图片百度一下就会知道了
  • 微信开发网站建设企业建网站一般要多少钱
  • 备案网站电子照幕布百度博客收录提交入口