我们日常在使用redis的时候, 有时会碰到大Value的问题, 超级大的一个Value存到redis中去, 这样其实不好, 我们可以把value进行压缩. 

下面我们使用java自带的压缩, 对字符串进行压缩.

/**
     * 使用gzip压缩字符串
     *
     * @param originString 要压缩的字符串
     * @return 压缩后的字符串
     */
    public static String compress(String originString) {
        if (originString == null || originString.length() == 0) {
            return originString;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try (
                GZIPOutputStream gzip = new GZIPOutputStream(out);
        ) {
            gzip.write(originString.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new sun.misc.BASE64Encoder().encode(out.toByteArray());
    }

    /**
     * 使用gzip解压缩
     *
     * @param compressedString 压缩字符串
     * @return
     */
    public static String uncompress(String compressedString) {
        if (compressedString == null || compressedString.length() == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] compressedByte = new byte[0];
        try {
            compressedByte = new sun.misc.BASE64Decoder().decodeBuffer(compressedString);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String originString = null;
        try (
                ByteArrayInputStream in = new ByteArrayInputStream(compressedByte);
                GZIPInputStream ginzip = new GZIPInputStream(in);
        ) {
            byte[] buffer = new byte[1024];
            int offset = -1;
            while ((offset = ginzip.read(buffer)) != -1) {
                out.write(buffer, 0, offset);
            }
            originString = out.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return originString;
    }

测试方法:

public static void main(String[] args) {
        String compress = compress("parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@68dc098b\n" +
                "2020-10-27 17:53:22,377 [] [main] INFO  org.springframework.data.repository.config.RepositoryConfigurationDelegate -TID: N/A - Multiple Spring Data modules found, entering strict repository configuration mode!\n" +
                "2020-10-27 17:53:22,751 [] [main] WARN  org.springframework.context.annotation.ConfigurationClassPostProcessor -TID: N/A - Cannot enhance @Configuration bean definition 'com.ctrip.framework.apollo.spring.boot.ApolloAutoConfiguration' since its singleton instance has been created too early. The typical cause is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor return type: Consider declaring such methods as 'static'.\n" +
                "2020-10-27 17:53:23,043 [] [main] INFO  org.springframework.cloud.context.scope.GenericScope -TID: N/A - BeanFactory id=96a1a9f4-ca0a-3f29-9933-63914650547a\n" +
                "2020-10-27 17:53:23,103 [] [main] INFO  o.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor -TID: N/A - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring\n" +
                "2020-10-27 17:53:23,141 [] [main] WARN  o.s.boot.context.properties.ConfigurationPropertiesBindingPostProcessor -TID: N/A - Multiple PropertySourcesPlaceholderConfigurer beans registered [propertySourcesPlaceholderConfigurer, org.springframework.context.support.PropertySourcesPlaceholderConfigurer], falling back to Environment\n" +
                "2020-10-27 17:53:23,173 [] [main] INFO  o.s.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker -TID: N/A - Bean 'com.ydcloud.qurey.customer.feign.customer.ActUserFollowService' of type [org.springframework.cloud.netflix.feign.FeignClientFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)\n" +
                "2020-10-27 17:53:23,175 [] [main] INFO  o.s.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker -TID: N/A - Bean 'com.ydcloud.qurey.customer.feign.customer.CueManagementService' of type [org.springframework.cloud.netflix.feign.FeignClientFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)\n" +
                "2020-10-27 17:53:23,178 [] [main] INFO  o.s.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker -TID: N/A - Bean 'com.ydcloud.qurey.customer.feign.customer.CustomerDemandSolutionServiceV2' of type [org.springframework.cloud.netflix.feign.FeignClientFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)\n" +
                "2020-10-27 17:53:23,181 [] [main] INFO  o.s.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker -TID: N/A - Bean 'com.ydcloud.qurey.customer.feign.customer.CustomerFollowUpTaskService' of type [org.springframework.cloud.netflix.feign.FeignClientFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)\n" +
                "2020-10-27 17:53:23,183 [] [main] INFO  o.s.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker -TID: N/A - Bean 'com.ydcloud.qurey.customer.feign.customer.CustomerService' of type [org.springframework.cloud.netflix.feign.FeignClientFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)\n" +
                "2020-10-27 17:53:23,186 [] [main] INFO  o.s.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker -TID: N/A - Bean 'com.ydcloud.qurey.customer.feign.resource.BuildingFloorRoomService' of type [org.springframework.cloud.netflix.feign.FeignClientFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)\n" +
                "2020-10-27 17:53:23,188 [] [main] INFO  o.s.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker -TID: N/A - Bean 'com.ydcloud.qurey.customer.feign.resource.BuildingService' of type [org.springframework.cloud.netflix.feign.FeignClientFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)\n" +
                "2020-10-27 17:53:23,190 [] [main] INFO  o.s.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker -TID: N/A - Bean 'com.ydcloud.qurey.customer.feign.resource.ParkProjectService' of type [org.springframework.cloud.netflix.feign.FeignClientFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)");

        System.out.println(compress);
        String uncompress = uncompress(compress);
        System.out.println(uncompress);

        System.out.println(compress.length());
        System.out.println(uncompress.length());
    }

 运行测试代码, 得到以下结果, 压缩后的字符串长度是: 1252, 原始字符串的长度是:  4515, 有很大的改善效果.

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐