optimized binary fetch with cache

This commit is contained in:
strawmanbobi
2019-01-03 22:02:55 +08:00
parent 83536a6857
commit 967245c2d7
3 changed files with 19 additions and 26 deletions

View File

@@ -41,10 +41,6 @@ public class DecodeLogic {
return decodeLogic;
}
public DecodeLogic() {
}
public byte[] openIRBinary(ServletContext context, IIRBinaryRepository irBinaryRepository,
RemoteIndex remoteIndex) {
if (null == remoteIndex) {
@@ -57,11 +53,9 @@ public class DecodeLogic {
LoggerUtil.getInstance().trace(TAG, "checksum for remoteIndex " +
remoteIndex.getId() + " = " + checksum);
IRBinary irBinary = irBinaryRepository.find(remoteIndex.getId());
if (null != irBinary) {
byte[] binaries = irBinary.getBinary();
byte[] binaries = irBinaryRepository.find(remoteIndex.getId());
if (null != binaries) {
System.out.println("binary content fetched from redis : " + binaries.length);
LoggerUtil.getInstance().trace(TAG, "binary content fetched from redis : " + binaries.length);
// validate binary content
String cachedChecksum =
MD5Util.byteArrayToHexString(MessageDigest.getInstance("MD5")
@@ -70,7 +64,6 @@ public class DecodeLogic {
return binaries;
}
}
}
// otherwise, read from file or OSS
if (null != context) {
@@ -81,9 +74,10 @@ public class DecodeLogic {
File binFile = new File(localFilePath);
FileInputStream fin = getFile(binFile, downloadPath, fileName, checksum);
if (null != fin) {
byte[] binaries = IOUtils.toByteArray(fin);
System.out.println("binary content get, save it to redis");
irBinaryRepository.add(new IRBinary(remoteIndex.getId(), binaries));
byte[] newBinaries = IOUtils.toByteArray(fin);
LoggerUtil.getInstance().trace(TAG, "binary content get, save it to redis");
irBinaryRepository.add(remoteIndex.getId(), newBinaries);
return binaries;
}
} else {

View File

@@ -18,9 +18,9 @@ public interface IIRBinaryRepository {
Map<Object, Object> findAllIRBinaries();
void add(IRBinary irBinary);
void add(Integer id, byte[] binaries);
void delete(Integer id);
IRBinary find(Integer id);
byte[] find(Integer id);
}

View File

@@ -1,6 +1,5 @@
package net.irext.decoder.cache.impl;
import net.irext.decoder.model.IRBinary;
import net.irext.decoder.cache.IIRBinaryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
@@ -39,16 +38,16 @@ public class IRBinaryRepositoryImpl implements IIRBinaryRepository {
hashOperations = redisTemplate.opsForHash();
}
public void add(final IRBinary IRBinary) {
hashOperations.put(KEY, IRBinary.getId(), IRBinary.getBinary());
public void add(Integer id, byte[] binaries) {
hashOperations.put(KEY, id, binaries);
}
public void delete(final Integer id) {
hashOperations.delete(KEY, id);
}
public IRBinary find(final Integer id) {
return (IRBinary) hashOperations.get(KEY, id);
public byte[] find(final Integer id) {
return (byte[])hashOperations.get(KEY, id);
}
public Map<Object, Object> findAllIRBinaries() {