微信公众号开发--善忘影视(九)

  |   0 评论   |   431 浏览

主要解决的问题: 影视的数据十五分钟更新一次, 为了减少数据库的访问,添加了redis缓存,缓存数据查询的结果,这样相同的数据就不会再查询一次了数据了,加快查询结果的返回

序列化对象工具类

一共四个方法,序列化对象和数组,反序列化对象和数组

    //序列化bean对象--object-->byte[]
    public static <T> byte[] serialize(T obj) {
        if (obj == null) {
            throw new RuntimeException("序列化对象(" + obj + ")!");
        }
        @SuppressWarnings("unchecked")
        Schema<T> schema = (Schema<T>) RuntimeSchema.getSchema(obj.getClass());
        LinkedBuffer buffer = LinkedBuffer.allocate(1024 * 1024);
        byte[] protostuff = null;
        try {
            protostuff = ProtostuffIOUtil.toByteArray(obj, schema, buffer);
        } catch (Exception e) {
            throw new RuntimeException("序列化(" + obj.getClass() + ")对象(" + obj + ")发生异常!", e);
        } finally {
            buffer.clear();
        }
        return protostuff;
    }
    //反序列化Bean对象--byte[]-->object
    public static <T> T deserialize(byte[] paramArrayOfByte, Class<T> targetClass) {
        if (paramArrayOfByte == null || paramArrayOfByte.length == 0) {
            throw new RuntimeException("反序列化对象发生异常,byte序列为空!");
        }
        T instance = null;
        try {
            instance = targetClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            throw new RuntimeException("反序列化过程中依据类型创建对象失败!", e);
        }
        Schema<T> schema = RuntimeSchema.getSchema(targetClass);
        ProtostuffIOUtil.mergeFrom(paramArrayOfByte, instance, schema);
        return instance;
    }
    //序列化List数组--List数组-->byte[]
    public static <T> byte[] serializeList(List<T> objList) {
        if (objList == null || objList.isEmpty()) {
            throw new RuntimeException("序列化对象列表(" + objList + ")参数异常!");
        }
        @SuppressWarnings("unchecked")
        Schema<T> schema = (Schema<T>) RuntimeSchema.getSchema(objList.get(0).getClass());
        LinkedBuffer buffer = LinkedBuffer.allocate(1024 * 1024);
        byte[] protostuff = null;
        ByteArrayOutputStream bos = null;
        try {
            bos = new ByteArrayOutputStream();
            ProtostuffIOUtil.writeListTo(bos, objList, schema, buffer);
            protostuff = bos.toByteArray();
        } catch (Exception e) {
            throw new RuntimeException("序列化对象列表(" + objList + ")发生异常!", e);
        } finally {
            buffer.clear();
            try {
                if(bos!=null){
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return protostuff;
    }
    //反序列化Bean对象--byte[]-->List数组
    public static <T> List<T> deserializeList(byte[] paramArrayOfByte, Class<T> targetClass) {
        if (paramArrayOfByte == null || paramArrayOfByte.length == 0) {
            throw new RuntimeException("反序列化对象发生异常,byte序列为空!");
        }
        Schema<T> schema = RuntimeSchema.getSchema(targetClass);
        List<T> result = null;
        try {
            result = ProtostuffIOUtil.parseListFrom(new ByteArrayInputStream(paramArrayOfByte), schema);
        } catch (IOException e) {
            throw new RuntimeException("反序列化对象列表发生异常!",e);
        }
        return result;
    }

Redis工具类

使用RedisUtil类时,然后复制redisConfig-simple.properties属性文件到自己项目下的资源根目录下,并改名为redisConfig.properties

redisConfig-simple.properties Config.java RedisUtil.java

添加支持的节点

如下面是获取影视详情的页面

    //从redis中获取详情页对应ID在redis中数据,如果为空就去数据库中查询
    byte[] bs = RedisUtil.getb("detail_" + id, 2);
    if (bs != null && bs.length > 0) {
        //反序列化需要的详情数据对象
        MovieCrawl mc = SerializeUtil.deserialize(bs, MovieCrawl.class);
        model.addAttribute("mc", mc);
    } else {
        MovieCrawl mc = weiXinService.getMovieDetail(id);
        model.addAttribute("mc", mc);
        if (mc != null) {
            //如果查到对象不为空,设置到redis中去,有效期15分钟
            RedisUtil.setb("detail_" + id, SerializeUtil.serialize(mc), 60 * 15, 2);
        }
    }
    //从redis中获取详情页对应ID在redis中下载链接数据,如果为空就去数据库中查询
    byte[] blinks = RedisUtil.getb("detail_links_" + id, 2);
    if (blinks != null && blinks.length > 0) {
        //反序列化下载链接列表
        List<String> list = SerializeUtil.deserializeList(blinks, String.class);
        model.addAttribute("list", list);
    }else{
        List<String> list = weiXinService.getDownlinks(id);
        model.addAttribute("list", list);
        if (!CollectionUtils.isEmpty(list)) {
            //如果查到下载链接列表不为空,设置到redis中去,有效期15分钟
            RedisUtil.setb("detail_links_" + id, SerializeUtil.serializeList(list), 60 * 15, 2);
        }
    }

评论

发表评论

validate