最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot壓縮json并寫入Redis的示例代碼

 更新時(shí)間:2024年08月28日 08:39:02   作者:HBLOG  
由于業(yè)務(wù)需要,存入redis中的緩存數(shù)據(jù)過(guò)大,占用了10+G的內(nèi)存,內(nèi)存作為重要資源,需要優(yōu)化一下大對(duì)象緩存,所以我們需要對(duì)json進(jìn)行壓縮,本文給大家介紹了SpringBoot如何壓縮Json并寫入redis,需要的朋友可以參考下

1.為什么需要壓縮json?

由于業(yè)務(wù)需要,存入redis中的緩存數(shù)據(jù)過(guò)大,占用了10+G的內(nèi)存,內(nèi)存作為重要資源,需要優(yōu)化一下大對(duì)象緩存,采用gzip壓縮存儲(chǔ),可以將 redis 的 kv 對(duì)大小縮小大約 7-8 倍,加快存儲(chǔ)、讀取速度

2.環(huán)境搭建

詳建redis模塊的docker目錄

version: '3'
services:
  redis:
    image: registry.cn-hangzhou.aliyuncs.com/zhengqing/redis:6.0.8                   
    container_name: redis                                                             
    restart: unless-stopped                                                                  
    command: redis-server /etc/redis/redis.conf --requirepass 123456 --appendonly no
#    command: redis-server --requirepass 123456 --appendonly yes 
    environment:                        
      TZ: Asia/Shanghai
      LANG: en_US.UTF-8
    volumes:                           
      - "./redis/data:/data"
      - "./redis/config/redis.conf:/etc/redis/redis.conf"  
    ports:                              
      - "6379:6379"

3.代碼工程

實(shí)驗(yàn)?zāi)繕?biāo)

實(shí)驗(yàn)存入redis的json數(shù)據(jù)壓縮和解壓縮

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springboot-demo</artifactId>
        <groupId>com.et</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>gzip</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.9.0</version>
        </dependency>

    </dependencies>
</project>

controller

package com.et.gzip.controller;

import com.et.gzip.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
public class HelloWorldController {
    @Autowired
    private RedisTemplate redisTemplateWithJackson;

    @PostMapping("/hello")
    public User showHelloWorld(@RequestBody User user){
        log.info("user:"+ user);

        return user;
    }
    @PostMapping("/redis")
    public User redis(@RequestBody User user){
        log.info("user:"+ user);
        redisTemplateWithJackson.opsForValue().set("user",user);
        User redisUser = (User) redisTemplateWithJackson.opsForValue().get("user");
        return redisUser;
    }
}

redis壓縮和解壓縮配置

壓縮類

package com.et.gzip.config;

import com.et.gzip.model.User;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.IOUtils;

import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import sun.misc.BASE64Encoder;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

@Slf4j
public class CompressRedis extends JdkSerializationRedisSerializer {

    public static final int BUFFER_SIZE = 4096;

    private JacksonRedisSerializer<User>  jacksonRedisSerializer;
    public CompressRedis() {
        this.jacksonRedisSerializer = getValueSerializer();
    }

    @Override
    public byte[] serialize(Object graph) throws SerializationException {
        if (graph == null) {
            return new byte[0];
        }
        ByteArrayOutputStream bos = null;
        GZIPOutputStream gzip = null;
        try {
            // serialize
            byte[] bytes = jacksonRedisSerializer.serialize(graph);
            log.info("bytes size{}",bytes.length);
            bos = new ByteArrayOutputStream();
            gzip = new GZIPOutputStream(bos);

            // compress
            gzip.write(bytes);
            gzip.finish();
            byte[] result = bos.toByteArray();

            log.info("result size{}",result.length);
            //return result;
            return new BASE64Encoder().encode(result).getBytes();
        } catch (Exception e) {
            throw new SerializationException("Gzip Serialization Error", e);
        } finally {
            IOUtils.closeQuietly(bos);
            IOUtils.closeQuietly(gzip);
        }
    }

    @Override
    public Object deserialize(byte[] bytes) throws SerializationException {
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        ByteArrayOutputStream bos = null;
        ByteArrayInputStream bis = null;
        GZIPInputStream gzip = null;
        try {
            bos = new ByteArrayOutputStream();
            byte[] compressed = new sun.misc.BASE64Decoder().decodeBuffer( new String(bytes));;
            bis = new ByteArrayInputStream(compressed);
            gzip = new GZIPInputStream(bis);
            byte[] buff = new byte[BUFFER_SIZE];
            int n;


            // uncompress
            while ((n = gzip.read(buff, 0, BUFFER_SIZE)) > 0) {
                bos.write(buff, 0, n);
            }
            //deserialize
            Object result = jacksonRedisSerializer.deserialize(bos.toByteArray());
            return result;
        } catch (Exception e) {
            throw new SerializationException("Gzip deserizelie error", e);
        } finally {
            IOUtils.closeQuietly(bos);
            IOUtils.closeQuietly(bis);
            IOUtils.closeQuietly(gzip);
        }
    }

    private static JacksonRedisSerializer<User> getValueSerializer() {
        JacksonRedisSerializer<User> jackson2JsonRedisSerializer = new JacksonRedisSerializer<>(User.class);
        ObjectMapper mapper=new ObjectMapper();
        jackson2JsonRedisSerializer.setObjectMapper(mapper);
        return jackson2JsonRedisSerializer;
    }

}

java序列化

package com.et.gzip.config;


import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import lombok.extern.slf4j.Slf4j;

import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

@Slf4j
public class JacksonRedisSerializer<T> implements RedisSerializer<T> {
    public static final Charset DEFAULT_CHARSET;
    private final JavaType javaType;
    private ObjectMapper objectMapper = new ObjectMapper();

    public JacksonRedisSerializer(Class<T> type) {
        this.javaType = this.getJavaType(type);
    }

    public JacksonRedisSerializer(JavaType javaType) {
        this.javaType = javaType;
    }

    public T deserialize(@Nullable byte[] bytes) throws SerializationException {
        if (bytes == null || bytes.length == 0) {
            return null;
        } else {
            try {
                return this.objectMapper.readValue(bytes, 0, bytes.length, this.javaType);

            } catch (Exception var3) {
                throw new SerializationException("Could not read JSON: " + var3.getMessage(), var3);
            }
        }
    }

    public byte[] serialize(@Nullable Object t) throws SerializationException {
        if (t == null) {
            return  new byte[0];
        } else {
            try {
                return this.objectMapper.writeValueAsBytes(t);
            } catch (Exception var3) {
                throw new SerializationException("Could not write JSON: " + var3.getMessage(), var3);
            }
        }
    }

    public void setObjectMapper(ObjectMapper objectMapper) {
        Assert.notNull(objectMapper, "'objectMapper' must not be null");
        this.objectMapper = objectMapper;
    }

    protected JavaType getJavaType(Class<?> clazz) {
        return TypeFactory.defaultInstance().constructType(clazz);
    }

    static {
        DEFAULT_CHARSET = StandardCharsets.UTF_8;
    }
}

redis序列化

package com.et.gzip.config;

import com.et.gzip.model.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;


@Configuration
public class RedisWithJacksonConfig {


    @Bean(name="redisTemplateWithJackson")
    public RedisTemplate<String, User> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {

        CompressRedis  compressRedis =  new CompressRedis();
        //redisTemplate
        RedisTemplate<String, User> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(lettuceConnectionFactory);
        RedisSerializer<?> stringSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringSerializer);
        redisTemplate.setValueSerializer(compressRedis);
        redisTemplate.setHashKeySerializer(stringSerializer);
        redisTemplate.setHashValueSerializer(compressRedis);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

application.yaml

spring:
  redis:
    host: 127.0.0.1
    port: 6379
    database: 10
    password: 123456
    timeout: 10s
    lettuce:
      pool:
        min-idle: 0
        max-idle: 8
        max-active: 8
        max-wait: -1ms
server:
  port: 8088
  compression:
    enabled: true
    mime-types: application/json,application/xml,text/html,text/plain,text/css,application/x-javascript

以上只是一些關(guān)鍵代碼,所有代碼請(qǐng)參見(jiàn)下面代碼倉(cāng)庫(kù)

代碼倉(cāng)庫(kù)

4.測(cè)試

  • 啟動(dòng)spring boot應(yīng)用
  • 用postman訪問(wèn)http://127.0.0.1:8088/redis

可以看到redis里面存儲(chǔ)的是gzip壓縮的內(nèi)容

查看控制臺(tái)日志

2024-08-26 14:37:56.445 INFO 43832 --- [nio-8088-exec-5] com.et.gzip.config.CompressRedis : bytes size371
2024-08-26 14:37:56.445 INFO 43832 --- [nio-8088-exec-5] com.et.gzip.config.CompressRedis : result size58

JSON經(jīng)過(guò)gzip壓縮,371-->58, 數(shù)據(jù)大小減少7-8倍

到此這篇關(guān)于SpringBoot壓縮json并寫入Redis的示例代碼的文章就介紹到這了,更多相關(guān)SpringBoot壓縮json內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot集成vue的開(kāi)發(fā)解決方案

    SpringBoot集成vue的開(kāi)發(fā)解決方案

    這篇文章主要介紹了SpringBoot集成vue的開(kāi)發(fā)解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • [Spring MVC]-詳解SpringMVC的各種參數(shù)綁定方式

    [Spring MVC]-詳解SpringMVC的各種參數(shù)綁定方式

    本篇文章主要介紹了SpringMVC的各種參數(shù)綁定方式 ,具有一定的參考價(jià)值,有需要的可以了解一下。
    2016-12-12
  • Java對(duì)象創(chuàng)建的過(guò)程流程分析

    Java對(duì)象創(chuàng)建的過(guò)程流程分析

    本文詳細(xì)介紹了Java對(duì)象創(chuàng)建的整個(gè)過(guò)程,包括類加載、內(nèi)存分配、對(duì)象頭設(shè)置、構(gòu)造器初始化以及內(nèi)存分配的安全問(wèn)題,文章還討論了新生代和老年代的內(nèi)存分配策略,特別是使用指針碰撞和空閑列表兩種方式,感興趣的朋友跟隨小編一起看看吧
    2026-01-01
  • Java的RxJava庫(kù)操作符的用法及實(shí)例講解

    Java的RxJava庫(kù)操作符的用法及實(shí)例講解

    RxJava由于提供異步和基于事件的支持在Android開(kāi)發(fā)者中獲得了不少人氣,這里我們就來(lái)看一下Java的RxJava庫(kù)操作符的用法及實(shí)例講解,需要的朋友可以參考下
    2016-06-06
  • Java面試必備八股文整理

    Java面試必備八股文整理

    這篇文章主要介紹了Java面試必備八股文整理,小伙伴們出去面試的時(shí)候會(huì)被問(wèn)到很多java專業(yè)性的知識(shí),那么八股文就是為此而出現(xiàn)的,需要的朋友可以參考下
    2023-03-03
  • Java 中 String,StringBuffer 和 StringBuilder 的區(qū)別及用法

    Java 中 String,StringBuffer 和 StringBuilder 的區(qū)別及用法

    這篇文章主要介紹了Java 中 String,StringBuffer 和 StringBuilder 的區(qū)別及用法的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Java數(shù)據(jù)結(jié)構(gòu)徹底理解關(guān)于KMP算法

    Java數(shù)據(jù)結(jié)構(gòu)徹底理解關(guān)于KMP算法

    這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)關(guān)于KMP算法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • SpringBoot通過(guò)main方法啟動(dòng)web項(xiàng)目實(shí)踐

    SpringBoot通過(guò)main方法啟動(dòng)web項(xiàng)目實(shí)踐

    SpringBoot通過(guò)SpringApplication.run()啟動(dòng)Web項(xiàng)目,自動(dòng)推斷應(yīng)用類型,加載初始化器與監(jiān)聽(tīng)器,配置SpringMVC組件,啟動(dòng)嵌入式服務(wù)器,實(shí)現(xiàn)零配置啟動(dòng)
    2025-08-08
  • 分布式消息隊(duì)列RocketMQ概念詳解

    分布式消息隊(duì)列RocketMQ概念詳解

    RocketMQ?是阿里開(kāi)源的分布式消息中間件,跟其它中間件相比,RocketMQ?的特點(diǎn)是純JAVA實(shí)現(xiàn),是一套提供了消息生產(chǎn),存儲(chǔ),消費(fèi)全過(guò)程API的軟件系統(tǒng),本文詳細(xì)介紹了分布式消息隊(duì)列RocketMQ概念,需要的朋友可以參考下
    2023-05-05
  • java后端請(qǐng)求過(guò)濾options方式

    java后端請(qǐng)求過(guò)濾options方式

    Optional項(xiàng)是一個(gè)容器對(duì)象,它可以包含非空值,也可以不包含非空值,它用于表示沒(méi)有值,而不是使用?null,引入Optional項(xiàng)是為了幫助開(kāi)發(fā)人員編寫更簡(jiǎn)潔、更具表現(xiàn)力的代碼,并避免?NullPointerException
    2024-01-01

最新評(píng)論

合江县| 龙泉市| 崇文区| 普兰县| 南宫市| 太仆寺旗| 西和县| 呼和浩特市| 溆浦县| 溧阳市| 双江| 大埔区| 年辖:市辖区| 东乌| 永靖县| 湄潭县| 乌兰察布市| 商都县| 加查县| 通河县| 茌平县| 务川| 洱源县| 肃北| 灯塔市| 中牟县| 温州市| 保亭| 汕头市| 琼结县| 栖霞市| 安阳县| 会泽县| 邮箱| 武功县| 萨嘎县| 平罗县| 南汇区| 常熟市| 大安市| 长治市|