From 9277fbd0d30ba6aab31eb78cbd262b66075f920b Mon Sep 17 00:00:00 2001 From: Atul Singh Date: Wed, 13 Jun 2018 10:28:58 +0530 Subject: [PATCH] Create DiskCache Every time when clearing the cache, app gets crashed when immediately request to get something from cache. Please check the comment with "// Check before proceed." I've added a check and each time it will check journalWriter is null or not. --- DiskCache | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 DiskCache diff --git a/DiskCache b/DiskCache new file mode 100644 index 0000000..6b8477c --- /dev/null +++ b/DiskCache @@ -0,0 +1,140 @@ +import com.jakewharton.disklrucache.DiskLruCache; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; +import java.math.BigInteger; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +public class DiskCache implements Cache { + private final String HASH_ALGORITHM = "MD5"; + private final String STRING_ENCODING = "UTF-8"; + private final int DEFAULT_CACHE_SIZE = 1024 * 1024 * 10; + private final int VALUE_COUNT = 1; + private DiskLruCache diskLruCache; + + private File cacheDirectory; + private int appVersion; + private int cacheSizeKb; + + public DiskCache(File cacheDirectory, int appVersion, int cacheSizeKb) throws IOException { + this.cacheDirectory = cacheDirectory; + this.appVersion = appVersion; + this.cacheSizeKb = cacheSizeKb; + open(); + } + + private void open() throws IOException { + diskLruCache = DiskLruCache.open(cacheDirectory, appVersion, VALUE_COUNT, + cacheSizeKb <= 0 ? DEFAULT_CACHE_SIZE : cacheSizeKb); + } + + @Override + public String getValue(String key) throws IOException { + String value = null; + if (!diskLruCache.isClosed()) { + DiskLruCache.Snapshot snapshot = null; + + try { + snapshot = diskLruCache.get(getHashOf(key)); + if (snapshot == null) { + return null; + } + + value = snapshot.getString(0); + } finally { + if (snapshot != null) { + snapshot.close(); + } + } + } + + return value; + } + + @Override + public boolean contains(String key) throws IOException { + boolean found = false; + if (!diskLruCache.isClosed()) { + DiskLruCache.Snapshot snapshot = null; + + try { + snapshot = diskLruCache.get(getHashOf(key)); + if (snapshot != null) { + found = true; + } + } finally { + if (snapshot != null) { + snapshot.close(); + } + } + } + + return found; + } + + @Override + public void setKeyValue(String key, String value) throws IOException { + DiskLruCache.Editor editor = null; + if (!diskLruCache.isClosed()) { + try { + editor = diskLruCache.edit(getHashOf(key)); + if (editor == null) { + return; + } + + if (writeValueToCache(value, editor)) { + diskLruCache.flush(); + editor.commit(); + } else { + editor.abort(); + } + } catch (IOException e) { + if (editor != null) { + editor.abort(); + } + + throw e; + } + } + } + + @Override + public void clearCache() throws IOException { + diskLruCache.delete(); + //Open the cache again to use the new empty cache + open(); + } + + protected boolean writeValueToCache(String value, DiskLruCache.Editor editor) throws IOException { + OutputStream outputStream = null; + try { + outputStream = new BufferedOutputStream(editor.newOutputStream(0)); + outputStream.write(value.getBytes(STRING_ENCODING)); + } finally { + if (outputStream != null) { + outputStream.close(); + } + } + + return true; + } + + protected String getHashOf(String string) throws UnsupportedEncodingException { + try { + MessageDigest messageDigest = null; + messageDigest = MessageDigest.getInstance(HASH_ALGORITHM); + messageDigest.update(string.getBytes(STRING_ENCODING)); + byte[] digest = messageDigest.digest(); + BigInteger bigInt = new BigInteger(1, digest); + + return bigInt.toString(16); + } catch (NoSuchAlgorithmException e) { + + return string; + } + } + +}