optimized binary fetch with cache
This commit is contained in:
@@ -41,10 +41,6 @@ public class DecodeLogic {
|
||||
return decodeLogic;
|
||||
}
|
||||
|
||||
public DecodeLogic() {
|
||||
|
||||
}
|
||||
|
||||
public byte[] openIRBinary(ServletContext context, IIRBinaryRepository irBinaryRepository,
|
||||
RemoteIndex remoteIndex) {
|
||||
if (null == remoteIndex) {
|
||||
@@ -57,18 +53,15 @@ 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();
|
||||
if (null != binaries) {
|
||||
System.out.println("binary content fetched from redis : " + binaries.length);
|
||||
// validate binary content
|
||||
String cachedChecksum =
|
||||
MD5Util.byteArrayToHexString(MessageDigest.getInstance("MD5")
|
||||
.digest(binaries)).toUpperCase();
|
||||
if (cachedChecksum.equals(checksum)) {
|
||||
return binaries;
|
||||
}
|
||||
byte[] binaries = irBinaryRepository.find(remoteIndex.getId());
|
||||
if (null != binaries) {
|
||||
LoggerUtil.getInstance().trace(TAG, "binary content fetched from redis : " + binaries.length);
|
||||
// validate binary content
|
||||
String cachedChecksum =
|
||||
MD5Util.byteArrayToHexString(MessageDigest.getInstance("MD5")
|
||||
.digest(binaries)).toUpperCase();
|
||||
if (cachedChecksum.equals(checksum)) {
|
||||
return binaries;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user