-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #902 from wangqifan/feature/lz4
add lz4 for metacache
- Loading branch information
Showing
11 changed files
with
155 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
core/src/main/java/com/ctrip/xpipe/spring/LZ4DecompressionInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.ctrip.xpipe.spring; | ||
|
||
import net.jpountz.lz4.LZ4Factory; | ||
import net.jpountz.lz4.LZ4FastDecompressor; | ||
import net.jpountz.lz4.LZ4SafeDecompressor; | ||
import org.apache.http.Header; | ||
import org.apache.http.HttpException; | ||
import org.apache.http.HttpResponse; | ||
import org.apache.http.HttpResponseInterceptor; | ||
import org.apache.http.entity.ByteArrayEntity; | ||
import org.apache.http.entity.InputStreamEntity; | ||
import org.apache.http.protocol.HttpContext; | ||
|
||
import net.jpountz.lz4.LZ4FrameInputStream; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
public class LZ4DecompressionInterceptor implements HttpResponseInterceptor { | ||
|
||
private static LZ4Factory factory = LZ4Factory.fastestInstance(); | ||
|
||
@Override | ||
public void process(HttpResponse response, HttpContext context) throws HttpException, IOException { | ||
Header head = response.getFirstHeader("Content-Encoding"); | ||
if (head == null) { | ||
return; | ||
} | ||
String encoding = head.getValue(); | ||
if ("lz4".equalsIgnoreCase(encoding)) { | ||
// 获取响应实体 | ||
InputStream entityStream = response.getEntity().getContent(); | ||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
byte[] buffer = new byte[1024]; | ||
int bytesRead; | ||
while ((bytesRead = entityStream.read(buffer)) != -1) { | ||
outputStream.write(buffer, 0, bytesRead); | ||
} | ||
|
||
byte[] compressed = outputStream.toByteArray(); | ||
|
||
LZ4SafeDecompressor decompressor = factory.safeDecompressor(); | ||
byte[] deCompressedData = decompressor.decompress(compressed, compressed.length * 20); | ||
|
||
// 将解压缩后的数据设置回响应实体 | ||
response.setEntity(new ByteArrayEntity(deCompressedData)); | ||
|
||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...ava/com/ctrip/xpipe/redis/console/controller/config/LZ4CompressionResponseBodyAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.ctrip.xpipe.redis.console.controller.config; | ||
|
||
import net.jpountz.lz4.LZ4Compressor; | ||
import net.jpountz.lz4.LZ4Factory; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.converter.HttpMessageConverter; | ||
import org.springframework.http.server.ServerHttpRequest; | ||
import org.springframework.http.server.ServerHttpResponse; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
import org.springframework.web.context.request.ServletRequestAttributes; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import java.util.Arrays; | ||
|
||
@RestControllerAdvice | ||
public class LZ4CompressionResponseBodyAdvice implements ResponseBodyAdvice<byte[]> { | ||
|
||
private static LZ4Factory factory = LZ4Factory.fastestInstance(); | ||
|
||
@Override | ||
public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) { | ||
|
||
if(!methodParameter.getParameterType().equals(byte[].class)) { | ||
return false; | ||
} | ||
// 获取请求头 | ||
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | ||
String encode = request.getHeader(HttpHeaders.ACCEPT_ENCODING); | ||
// 检查请求头中是否包含 Content-Encoding: lz4 | ||
return encode!= null && encode.contains("lz4"); | ||
} | ||
|
||
@Override | ||
public byte[] beforeBodyWrite(byte[] body, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) { | ||
|
||
LZ4Compressor compressor = factory.fastCompressor(); | ||
|
||
// 压缩字节数组 | ||
int maxCompressedLength = compressor.maxCompressedLength(body.length); | ||
byte[] compressedBytes = new byte[maxCompressedLength]; | ||
int compressedLength = compressor.compress(body, 0, body.length, compressedBytes, 0, maxCompressedLength); | ||
|
||
// 设置 Content-Encoding 头部为 lz4 | ||
serverHttpResponse.getHeaders().set("Content-Encoding", "lz4"); | ||
serverHttpResponse.getHeaders().setContentLength(compressedLength); | ||
|
||
// 返回压缩后的字节数组(需要截取实际压缩长度) | ||
return Arrays.copyOf(compressedBytes, compressedLength); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters