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

View File

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

View File

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