Cookie的压缩以及字符集简单的探讨

本文是根据《深入分析Java Web技术内幕》一书第三章和第十章做一个摘取总结和探讨


Cookie是什么

简单来说就是用户通过http请求访问一个服务器时,服务器会将一些key/vlaue的键值对返回给客户端浏览器,用户下次访问的时候,这些数据又可以玩转的发送给服务器

  • 主要特征
    Key–Value形式
    Domain 指定cookie生成域名
    Max-Age 过期时间
    HttpOnly 是否允许js访问
    有个数以及大小的限制

为什么要压缩cookie

上面提到,cookie实际上是服务器端和客户端通过http网络传输的数据。那么试想一下,如果网站cookie有2kb左右,一天PV有1亿,那么一天就能产生约4TB的流量,但从节约贷款的成本来说是非常有必要的。

如何压缩

以gzip压缩为例

/**
     * 压缩cookie
     * @param response
     * @param c
     */
    protected void compressCookie(HttpServletResponse response, Cookie c) {
        ByteArrayOutputStream bos = null;
        // gzip 压缩
        GZIPOutputStream gzipOutputStream = null;
        try {
            bos = new ByteArrayOutputStream();
            gzipOutputStream = new GZIPOutputStream(bos);
            gzipOutputStream.write(c.getValue().getBytes());
            gzipOutputStream.close();
            System.out.println("before compress length:" + c.getValue().getBytes().length);
            // base64位编码
            Encoder encoder = Base64.getEncoder();
            String compress =  encoder.encodeToString(bos.toByteArray());
            // 添加cookie
            Cookie cookie = new Cookie(c.getName(),compress);
            cookie.setMaxAge(c.getMaxAge());
            response.addCookie(cookie);
            System.out.println("after compress length:" + compress.getBytes().length);
            bos.close();
        } catch (Exception e) {

        } finally {

        }
    }
    /**
     * cookie 解压
     * @param c
     * @return
     */
    protected byte[] unCompressCookie(Cookie c) {
        ByteArrayOutputStream bos = null;
        // gzip解压
        GZIPInputStream gzipInputStream = null;
        try {
            bos = new ByteArrayOutputStream();
            // base64 解码
            Decoder decoder = Base64.getDecoder();
            byte[] compressDatas = decoder.decode(c.getValue());
            ByteArrayInputStream bis = new ByteArrayInputStream(compressDatas);
            gzipInputStream = new GZIPInputStream(bis);
            byte[] data = new byte[1024];
            int count;
            while ((count = gzipInputStream.read(data)) > 0) {
                bos.write(data, 0, count);
            }
            gzipInputStream.close();
        } catch (Exception e) {

        }
        return bos != null ? bos.toByteArray() : null;
    }

实际测试结果:
采用gzip压缩

before compress length:1600
after compress length:68

采用Deflater压缩

before compress length:1600
after compress length:52

字符简单的探讨

书中指出遇到的一个问题,想办法压缩cookie,但是选择不同的压缩算法,字符是减少了,字节数并没有减少。
原因是如整形数字1234567当做字符来存储,采用UTF-8编码会占用7个字节,UTF-16编码会占用14个字节,但是如果将其用int类型存储只需要4个字节。
采用不用的编码存储的大小也是不同的。
这篇文章总结了常见的编码大小
常见编码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值