标题:
Redis使用建议规范(2)
[打印本页]
作者:
look_w
时间:
2019-2-19 17:18
标题:
Redis使用建议规范(2)
对于必须要存储的大文本数据一定要压缩后存储
对于大文本【超过500字节】写入到Redis时,一定要压缩后存储!大文本数据存入Redis,除了带来极大的内存占用外,在访问量高时,很容易就会将网卡流量占满,进而造成整个服务器上的所有服务不可用,并引发雪崩效应,造成各个系统瘫痪!
public Boolean setBigValue(final byte[] key, final byte[] value, final long liveTime){
return redisTemplate.execute(new RedisCallback<Boolean>() {
public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
byte[] compressedBytes = CompressUtil.compress(value);
connection.set(key, compressedBytes);
if (liveTime > 0) {
connection.expire(key, liveTime);
}
return Boolean.TRUE;
}
});
}
压缩方式可参考
public class CompressUtil {
private static final Inflater infl = new Inflater();
private static final Deflater defl = new Deflater();
private CompressUtil(){
}
public static byte[] uncompress(byte[] inputByte) throws IOException {
int len = 0;
infl.setInput(inputByte);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] outByte = new byte[1024];
try {
while (!infl.finished()) {
len = infl.inflate(outByte);
if (len == 0) {
break;
}
bos.write(outByte, 0, len);
}
infl.end();
} catch (Exception e) {
} finally {
bos.close();
}
return bos.toByteArray();
}
public static byte[] compress(byte[] inputByte) throws IOException {
int len = 0;
defl.setInput(inputByte);
defl.finish();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] outputByte = new byte[1024];
try {
while (!defl.finished()) {
len = defl.deflate(outputByte);
bos.write(outputByte, 0, len);
}
defl.end();
} finally {
bos.close();
}
return bos.toByteArray();
}
}
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0