Skip to content

Commit

Permalink
fix cache reference from interface
Browse files Browse the repository at this point in the history
* update cache
* fix reference from storage inside cache
* update tests
  • Loading branch information
FabianTerhorst committed Jan 10, 2016
1 parent b35d77b commit 01087a2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 45 deletions.
33 changes: 2 additions & 31 deletions iron/src/main/java/io/fabianterhorst/iron/Cache.java
Expand Up @@ -10,36 +10,7 @@ public interface Cache {

Object remove(String key);

void setStorage(Storage storage);
int NONE = 1;

Cache MEMORY = new LruCache();

Cache NONE = new Cache() {

Storage storage;

@Override
public void evictAll() {
}

@Override
public Object put(String key, Object value) {
return null;
}

@Override
public Object get(String key) {
return storage.doSelect(key);
}

@Override
public Object remove(String key) {
return null;
}

@Override
public void setStorage(Storage storage) {
this.storage = storage;
}
};
int MEMORY = 2;
}
2 changes: 1 addition & 1 deletion iron/src/main/java/io/fabianterhorst/iron/Chest.java
Expand Up @@ -24,7 +24,7 @@ public interface ReadCallback<T> {

private final IronLoadExtension mLoaderExtension;

protected Chest(Context context, String dbName, IronLoadExtension loadExtension, IronEncryptionExtension encryptionExtension, Cache cache) {
protected Chest(Context context, String dbName, IronLoadExtension loadExtension, IronEncryptionExtension encryptionExtension, int cache) {
mStorage = new DbStoragePlainFile(context.getApplicationContext(), dbName, encryptionExtension, cache);
mLoaderExtension = loadExtension;
}
Expand Down
32 changes: 29 additions & 3 deletions iron/src/main/java/io/fabianterhorst/iron/DbStoragePlainFile.java
Expand Up @@ -74,12 +74,38 @@ private Kryo createKryoInstance() {
return kryo;
}

public DbStoragePlainFile(Context context, String dbName, IronEncryptionExtension encryptionExtension, Cache cache) {
public DbStoragePlainFile(Context context, String dbName, IronEncryptionExtension encryptionExtension, int cache) {
mContext = context;
mDbName = dbName;
mEncryptionExtension = encryptionExtension;
mMemoryCache = cache;
mMemoryCache.setStorage(this);
if(cache == Cache.NONE)
mMemoryCache = new Cache() {
@Override
public void evictAll() {
}

@Override
public Object put(String key, Object value) {
return null;
}

@Override
public Object get(String key) {
return doSelect(key);
}

@Override
public Object remove(String key) {
return null;
}
};
else
mMemoryCache = new LruCache() {
@Override
public Object create(String key) {
return doSelect(key);
}
};
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions iron/src/main/java/io/fabianterhorst/iron/Iron.java
Expand Up @@ -29,7 +29,7 @@ public class Iron {

private static IronEncryptionExtension mEncryptionExtension;

private static Cache mCache = Cache.NONE;
private static int mCache = Cache.NONE;

private static final ConcurrentHashMap<String, Chest> mChestMap = new ConcurrentHashMap<>();

Expand Down Expand Up @@ -62,7 +62,7 @@ public static void setEncryptionExtension(IronEncryptionExtension encryptionExte
mEncryptionExtension = encryptionExtension;
}

public static void setCache(Cache cache){
public static void setCache(int cache){
mCache = cache;
}

Expand Down
9 changes: 1 addition & 8 deletions iron/src/main/java/io/fabianterhorst/iron/LruCache.java
Expand Up @@ -46,8 +46,6 @@
public class LruCache implements Cache {
private final LinkedHashMap<String, Object> map;

private Storage storage;

/** Size of this cache in units. Not necessarily the number of elements. */
private int size;
private int maxSize;
Expand Down Expand Up @@ -268,7 +266,7 @@ protected void entryRemoved(boolean evicted, String key, Object oldValue, Object
* key.
*/
public Object create(String key) {
return storage.doSelect(key);
return null;
}

private int safeSizeOf(String key, Object value) {
Expand Down Expand Up @@ -367,9 +365,4 @@ public synchronized final Map<String, Object> snapshot() {
return String.format("LruCache[maxSize=%d,hits=%d,misses=%d,hitRate=%d%%]",
maxSize, hitCount, missCount, hitPercent);
}

@Override
public void setStorage(Storage storage){
this.storage = storage;
}
}

0 comments on commit 01087a2

Please sign in to comment.