Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes on Disk and Memory cache #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 82 additions & 33 deletions Dumbledroid/src/io/leocad/dumbledroid/data/cache/DiskCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.content.Context;
import android.util.Log;
Expand All @@ -15,18 +17,20 @@ public class DiskCache {

private static final String TAG = "DiskCache";

private static DiskCache INSTANCE;
private static DiskCache sInstance;
/**
* @param ctx ALWAYS USE getApplicationContext() here.
* @return
*/
public static DiskCache getInstance(Context ctx) {

if (INSTANCE == null) {
INSTANCE = new DiskCache(ctx);
public static DiskCache getInstance(Context context) {
if (sInstance == null) {
synchronized (DiskCache.class) {
if (sInstance == null) {
sInstance = new DiskCache(context);
}
}
}

return INSTANCE;
return sInstance;
}

private final FileController mFileCtrl;
Expand All @@ -36,45 +40,90 @@ private DiskCache(Context ctx) {
}

public void cache(String key, AbstractModel model) {
final String fileName = getHashKey(key);
final ModelHolder holder = new ModelHolder(model, System.currentTimeMillis());

String fileName = String.valueOf(key.hashCode());

ModelHolder holder = new ModelHolder(model, System.currentTimeMillis());
FileOutputStream fos = null;
ObjectOutputStream oos = null;

try {
FileOutputStream fos = mFileCtrl.getFileOutputStream(fileName);
ObjectOutputStream oos = new ObjectOutputStream(fos);
fos = mFileCtrl.getFileOutputStream(fileName);
oos = new ObjectOutputStream(fos);
oos.writeObject(holder);

oos.flush();
oos.close();

fos.flush();
fos.close();

} catch (IOException e) {
Log.w(TAG, "The file " + fileName + "couldn't be cached. Does this app have permission to ACCESS_EXTERNAL_STORAGE?", e);
Log.w(TAG, "The file " + fileName + " couldn't be cached. Does this app have permission to ACCESS_EXTERNAL_STORAGE ?", e);
} finally {
if(fos != null) {
try {
fos.flush();
fos.close();
} catch (IOException e) {
Log.w(TAG, Log.getStackTraceString(e));
}
}
if(oos != null) {
try {
oos.flush();
oos.close();
} catch (IOException e) {
Log.w(TAG, Log.getStackTraceString(e));
}
}
}
}

public ModelHolder getCached(String key) {
final String fileName = getHashKey(key);

String fileName = String.valueOf(key.hashCode());

FileInputStream fis = null;
ObjectInputStream ois = null;
try {
FileInputStream fis = mFileCtrl.getFileInputStream(fileName);
ObjectInputStream ois = new ObjectInputStream(fis);

ModelHolder holder = (ModelHolder) ois.readObject();
fis = mFileCtrl.getFileInputStream(fileName);
ois = new ObjectInputStream(fis);
final ModelHolder holder = (ModelHolder) ois.readObject();
return holder;
} catch (IOException e) {
Log.w(TAG, "Can't access file: " + fileName, e);
} catch (ClassNotFoundException e) {
Log.w(TAG, "Can't read object from file: " + fileName, e);
} finally {
if(fis != null) {
try {
fis.close();
} catch (IOException e) {
Log.w(TAG, Log.getStackTraceString(e));
}
}
if(ois != null) {
try {
ois.close();
} catch (IOException e) {
Log.w(TAG, Log.getStackTraceString(e));
}
}
}
return null;
}

ois.close();
fis.close();
private static String getHashKey(String key) {
try {
final MessageDigest mDigest = MessageDigest.getInstance("SHA-1");
mDigest.update(key.getBytes());
return bytesToHexString(mDigest.digest());
} catch (NoSuchAlgorithmException e) {}
return String.valueOf(key.hashCode());
}

return holder;
private static String bytesToHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder();

} catch (Exception e) {
Log.w(TAG, "Can't access file: " + fileName, e);
return null;
for (int i = 0; i < bytes.length; i++) {
final String hex = Integer.toHexString(0xFF & bytes[i]);
if (hex.length() == 1) {
sb.append('0');
}
sb.append(hex);
}
return sb.toString();
}
}
}
38 changes: 23 additions & 15 deletions Dumbledroid/src/io/leocad/dumbledroid/data/cache/MemoryCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,53 @@

public class MemoryCache {

private static final MemoryCache INSTANCE = new MemoryCache();
private static MemoryCache sInstance;

public static MemoryCache getInstance() {
return INSTANCE;
if (sInstance == null) {
synchronized (MemoryCache.class) {
if (sInstance == null) {
sInstance = new MemoryCache();
}
}
}
return sInstance;
}

private Map<String, SoftReference<ModelHolder>> mMap;

private MemoryCache() {
mMap = new HashMap<String, SoftReference<ModelHolder>>();
}

public void cache(String key, AbstractModel model) {

ModelHolder holder = new ModelHolder(model, System.currentTimeMillis());

SoftReference<ModelHolder> ref = new SoftReference<ModelHolder>(holder);
mMap.put(key, ref);
}

public AbstractModel getCachedOrNull(String key) {

SoftReference<ModelHolder> softReference = mMap.get(key);

if (softReference == null) {
return null;
}

ModelHolder modelHolder = softReference.get();

if (modelHolder == null || modelHolder.isExpired()) {
//Clear from cache
softReference.clear();
mMap.remove(key);

Log.v("MemoryCache", "Expired from memory! " + key);

return null;
}

return modelHolder.model;
}
}
}