diff --git a/build.gradle b/build.gradle index 6b42096..185ba99 100644 --- a/build.gradle +++ b/build.gradle @@ -4,9 +4,6 @@ buildscript { repositories { mavenCentral() } - dependencies { - classpath 'com.android.tools.build:gradle:0.8.+' - } } allprojects { diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index fe862f5..eb26b34 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Sat Feb 01 01:39:34 CST 2014 +#Mon Jul 14 20:08:28 FET 2014 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=http\://services.gradle.org/distributions/gradle-1.10-bin.zip +distributionUrl=http\://services.gradle.org/distributions/gradle-1.10-all.zip diff --git a/objectcache/build.gradle b/objectcache/build.gradle index fba0056..bfd6140 100644 --- a/objectcache/build.gradle +++ b/objectcache/build.gradle @@ -1,34 +1,34 @@ -apply plugin: 'android-library' +apply plugin: 'java' repositories { mavenCentral() } -android { - compileSdkVersion 19 - buildToolsVersion "19.0.1" - - defaultConfig { - minSdkVersion 8 - targetSdkVersion 19 - versionCode 1 - versionName "1.0" - } - release { - runProguard false - proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' - } -} - apply plugin: 'maven' group = 'com.iainconnor' -version = '0.0.17-SNAPSHOT' +version = '2.0.0-SNAPSHOT' project.ext.description = "A simple cache for your objects." dependencies { compile 'com.google.code.gson:gson:2.2.4' compile 'com.jakewharton:disklrucache:2.0.2' + compile 'com.netflix.rxjava:rxjava-android:0.19.6' +} + +task sourcesJar(type: Jar, dependsOn: classes) { + classifier = 'sources' + from sourceSets.main.allSource +} + +task javadocJar(type: Jar, dependsOn: javadoc) { + classifier = 'javadoc' + from javadoc.destinationDir +} + +artifacts { + archives sourcesJar + archives javadocJar } uploadArchives { @@ -41,5 +41,7 @@ uploadArchives { repository url: 'file://' + new File(System.getProperty('user.home'), '.m2/repository').absolutePath repository url: 'file://' + new File("./maven").absolutePath } + + } -} \ No newline at end of file +} diff --git a/objectcache/src/main/AndroidManifest.xml b/objectcache/src/main/AndroidManifest.xml deleted file mode 100644 index 0ed1992..0000000 --- a/objectcache/src/main/AndroidManifest.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - diff --git a/objectcache/src/main/java/com/iainconnor/objectcache/CacheManager.java b/objectcache/src/main/java/com/iainconnor/objectcache/CacheManager.java index 7b86341..d3a2035 100644 --- a/objectcache/src/main/java/com/iainconnor/objectcache/CacheManager.java +++ b/objectcache/src/main/java/com/iainconnor/objectcache/CacheManager.java @@ -1,268 +1,143 @@ package com.iainconnor.objectcache; -import android.os.AsyncTask; import com.google.gson.Gson; import java.io.IOException; import java.lang.reflect.Type; import java.util.HashMap; -public class CacheManager { - private final static int CACHE_RUSH_SECONDS = 60 * 2; - private static CacheManager ourInstance; - private DiskCache diskCache; - private HashMap runtimeCache; - - public static CacheManager getInstance ( DiskCache diskCache ) { - if (ourInstance == null) { - ourInstance = new CacheManager(diskCache); - } - - return ourInstance; - } - - private CacheManager ( DiskCache diskCache ) { - this.diskCache = diskCache; - runtimeCache = new HashMap(); - } - - public boolean exists ( String key ) { - boolean result = false; - - try { - result = diskCache.contains(key); - } catch (IOException e) { - e.printStackTrace(); - } - - return result; - } - - public Object get ( String key, Class objectClass, Type objectType ) { - Object result = null; - - CachedObject runtimeCachedObject = runtimeCache.get(key); - if (runtimeCachedObject != null && !runtimeCachedObject.isExpired()) { - result = new Gson().fromJson(runtimeCachedObject.getPayload(), objectType); - } else if (runtimeCachedObject != null && runtimeCachedObject.isSoftExpired()) { - result = new SoftCachedObject(new Gson().fromJson(runtimeCachedObject.getPayload(), objectType)); - } else { - try { - String json = diskCache.getValue(key); - if (json != null) { - CachedObject cachedObject = new Gson().fromJson(json, CachedObject.class); - if (!cachedObject.isExpired()) { - runtimeCache.put(key, cachedObject); - result = new Gson().fromJson(cachedObject.getPayload(), objectType); - } else { - if (cachedObject.isSoftExpired()) { - result = new SoftCachedObject(new Gson().fromJson(cachedObject.getPayload(), objectType)); - } - - // To avoid cache rushing, we insert the value back in the cache with a longer expiry - // Presumably, whoever received this expiration result will have inserted a fresh value by now - putAsync(key, new Gson().fromJson(cachedObject.getPayload(), objectType), CACHE_RUSH_SECONDS, false, new PutCallback() { - @Override - public void onSuccess () { - - } - - @Override - public void onFailure ( Exception e ) { - - } - }); - } - } - } catch (Exception e) { - e.printStackTrace(); - // Do nothing, return null - } - } - - return result; - } - - public void getAsync ( String key, Class objectClass, Type objectType, GetCallback getCallback ) { - new GetAsyncTask(key, objectClass, objectType, getCallback).execute(); - } - - public boolean unset ( String key ) { - return put(key, null, -1, false); - } - - public void unsetAsync ( String key, PutCallback putCallback ) { - putAsync(key, null, -1, false, putCallback); - } - - public boolean put ( String key, Object object ) { - return put(key, object, -1, false); - } - - public boolean put ( String key, Object object, int expiryTimeSeconds, boolean allowSoftExpiry ) { - boolean result = false; - - try { - String payloadJson = new Gson().toJson(object); - CachedObject cachedObject = new CachedObject(payloadJson, expiryTimeSeconds, allowSoftExpiry); - String json = new Gson().toJson(cachedObject); - runtimeCache.put(key, cachedObject); - diskCache.setKeyValue(key, json); - result = true; - } catch (Exception e) { - e.printStackTrace(); - // Do nothing, return false - } - - return result; - } - - public void putAsync ( String key, Object object, PutCallback putCallback ) { - putAsync(key, object, -1, false, putCallback); - } +import rx.Observable; +import rx.Subscriber; - public void putAsync ( String key, Object object, int expiryTimeSeconds, boolean allowSoftExpiry, PutCallback putCallback ) { - new PutAsyncTask(key, object, expiryTimeSeconds, allowSoftExpiry, putCallback).execute(); - } - - public void clear() throws IOException { - runtimeCache.clear(); - diskCache.clearCache(); - } - - public enum ExpiryTimes { - ONE_SECOND(1), - ONE_MINUTE(60), - ONE_HOUR(60 * 60), - ONE_DAY(60 * 60 * 24), - ONE_WEEK(60 * 60 * 24 * 7), - ONE_MONTH(60 * 60 * 24 * 30), - ONE_YEAR(60 * 60 * 24 * 365); - - private final int seconds; - - ExpiryTimes ( int seconds ) { - this.seconds = seconds; - } - - public int asSeconds () { - return seconds; - } - } - - @SuppressWarnings ("unchecked") - private class GetAsyncTask extends AsyncTask { - private final String key; - private final GetCallback callback; - private final Type objectType; - private final Class objectClass; - private Exception e; - - private GetAsyncTask ( String key, Class objectClass, Type objectType, GetCallback callback ) { - this.callback = callback; - this.key = key; - this.objectType = objectType; - this.objectClass = objectClass; - } - - @Override - protected Object doInBackground ( Void... voids ) { - Object result = null; - - CachedObject runtimeCachedObject = runtimeCache.get(key); - if (runtimeCachedObject != null && !runtimeCachedObject.isExpired()) { - result = new Gson().fromJson(runtimeCachedObject.getPayload(), objectType); - } else if (runtimeCachedObject != null && runtimeCachedObject.isSoftExpired()) { - result = new SoftCachedObject(new Gson().fromJson(runtimeCachedObject.getPayload(), objectType)); - } else { - try { - String json = diskCache.getValue(key); - if (json != null) { - CachedObject cachedObject = new Gson().fromJson(json, CachedObject.class); - - if (!cachedObject.isExpired()) { - result = new Gson().fromJson(cachedObject.getPayload(), objectType); - runtimeCache.put(key, cachedObject); - } else { - if (cachedObject.isSoftExpired()) { - result = new SoftCachedObject(new Gson().fromJson(cachedObject.getPayload(), objectType)); - } - - // To avoid cache rushing, we insert the value back in the cache with a longer expiry - // Presumably, whoever received this expiration result will have inserted a fresh value by now - putAsync(key, new Gson().fromJson(cachedObject.getPayload(), objectType), CACHE_RUSH_SECONDS, false, new PutCallback() { - @Override - public void onSuccess () { - - } - - @Override - public void onFailure ( Exception e ) { - - } - }); - } - } - } catch (Exception e) { - this.e = e; - } - } - - return result; - } - - @Override - protected void onPostExecute ( Object object ) { - if (callback != null) { - if (e == null) { - callback.onSuccess(object); - } else { - callback.onFailure(e); - } - } - } - } - - private class PutAsyncTask extends AsyncTask { - private final PutCallback callback; - private final String key; - private final Object payload; - private final int expiryTimeSeconds; - private final boolean allowSoftExpiry; - private Exception e; - - private PutAsyncTask ( String key, Object payload, int expiryTimeSeconds, boolean allowSoftExpiry, PutCallback callback ) { - this.key = key; - this.callback = callback; - this.payload = payload; - this.expiryTimeSeconds = expiryTimeSeconds; - this.allowSoftExpiry = allowSoftExpiry; - } - - @Override - protected Void doInBackground ( Void... voids ) { - try { - String payloadJson = new Gson().toJson(payload); - CachedObject cachedObject = new CachedObject(payloadJson, expiryTimeSeconds, allowSoftExpiry); - String json = new Gson().toJson(cachedObject); - runtimeCache.put(key, cachedObject); - diskCache.setKeyValue(key, json); - } catch (Exception e) { - this.e = e; - } - - return null; - } +public class CacheManager { - @Override - protected void onPostExecute ( Void aVoid ) { - if (callback != null) { - if (e == null) { - callback.onSuccess(); - } else { - callback.onFailure(e); - } - } - } - } + private final static int CACHE_RUSH_SECONDS = 60 * 2; + private static CacheManager ourInstance; + private DiskCache diskCache; + private HashMap runtimeCache; + + public static CacheManager getInstance(DiskCache diskCache) { + if (ourInstance == null) { + ourInstance = new CacheManager(diskCache); + } + + return ourInstance; + } + + private CacheManager(DiskCache diskCache) { + this.diskCache = diskCache; + runtimeCache = new HashMap(); + } + + public boolean exists(String key) { + boolean result = false; + + try { + result = diskCache.contains(key); + } catch (IOException e) { + e.printStackTrace(); + } + + return result; + } + + public Observable unset(String key) { + return put(key, null, -1); + } + + public Observable unsetAsync(String key) { + return put(key, null, -1); + } + + public Observable put(String key, Object object) { + return put(key, object, -1); + } + + public Observable put(final String key, final Object object, final int expiryTimeSeconds) { + return Observable.create(new Observable.OnSubscribe() { + @Override + public void call(Subscriber subscriber) { + try { + String payloadJson = new Gson().toJson(object); + CachedObject cachedObject = new CachedObject(payloadJson, expiryTimeSeconds); + String json = new Gson().toJson(cachedObject); + runtimeCache.put(key, cachedObject); + diskCache.setKeyValue(key, json); + subscriber.onNext(new Object()); + subscriber.onCompleted(); + } catch (Exception e) { + subscriber.onError(e); + } + } + }); + } + + public void clear() throws IOException { + runtimeCache.clear(); + diskCache.clearCache(); + } + + public enum ExpiryTimes { + ONE_SECOND(1), + ONE_MINUTE(60), + ONE_HOUR(60 * 60), + ONE_DAY(60 * 60 * 24), + ONE_WEEK(60 * 60 * 24 * 7), + ONE_MONTH(60 * 60 * 24 * 30), + ONE_YEAR(60 * 60 * 24 * 365); + + private final int seconds; + + ExpiryTimes(int seconds) { + this.seconds = seconds; + } + + public int asSeconds() { + return seconds; + } + } + + public Observable get(final String key, final Class objectClass, final Type objectType) { + return Observable.create(new Observable.OnSubscribe() { + @Override + public void call(Subscriber subscriber) { + try { + T result = null; + + CachedObject runtimeCachedObject = runtimeCache.get(key); + if (runtimeCachedObject != null && !runtimeCachedObject.isExpired()) { + result = new Gson().fromJson(runtimeCachedObject.getPayload(), objectType); + } else { + String json = diskCache.getValue(key); + if (json != null) { + CachedObject cachedObject = new Gson().fromJson(json, CachedObject.class); + if (!cachedObject.isExpired()) { + runtimeCache.put(key, cachedObject); + result = new Gson().fromJson(cachedObject.getPayload(), objectType); + } else { + // To avoid cache rushing, we insert the value back in the cache with a longer expiry + // Presumably, whoever received this expiration result will have inserted a fresh value by now + put(key, new Gson().fromJson(cachedObject.getPayload(), objectType), CACHE_RUSH_SECONDS).subscribe(new Subscriber() { + @Override + public void onCompleted() { } + + @Override + public void onError(Throwable e) { } + + @Override + public void onNext(Object o) { } + }); + } + } + } + if (result != null) { + subscriber.onNext(result); + } + subscriber.onCompleted(); + } catch (Exception e) { + subscriber.onError(e); + } + } + }); + } } diff --git a/objectcache/src/main/java/com/iainconnor/objectcache/CachedObject.java b/objectcache/src/main/java/com/iainconnor/objectcache/CachedObject.java index a13fa5a..843fc8a 100644 --- a/objectcache/src/main/java/com/iainconnor/objectcache/CachedObject.java +++ b/objectcache/src/main/java/com/iainconnor/objectcache/CachedObject.java @@ -4,15 +4,13 @@ class CachedObject { private int expiryTimeSeconds; private int expiryTimestamp; private int creationTimestamp; - private boolean softExpiry; private String payload; - public CachedObject ( String payload, int expiryTimeSeconds, boolean softExpiry ) { + public CachedObject ( String payload, int expiryTimeSeconds ) { this.expiryTimeSeconds = expiryTimeSeconds <= 0 ? -1 : expiryTimeSeconds; this.creationTimestamp = (int) (System.currentTimeMillis() / 1000L); this.expiryTimestamp = expiryTimeSeconds <= 0 ? -1 : this.creationTimestamp + this.expiryTimeSeconds; this.payload = payload; - this.softExpiry = softExpiry; } public boolean isExpired () { @@ -22,8 +20,4 @@ public boolean isExpired () { public String getPayload () { return payload; } - - public boolean isSoftExpired () { - return isExpired() && softExpiry; - } } diff --git a/objectcache/src/main/java/com/iainconnor/objectcache/GetCallback.java b/objectcache/src/main/java/com/iainconnor/objectcache/GetCallback.java deleted file mode 100644 index 4470b28..0000000 --- a/objectcache/src/main/java/com/iainconnor/objectcache/GetCallback.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.iainconnor.objectcache; - -public interface GetCallback { - public void onSuccess ( T object ); - - public void onFailure ( Exception e ); -} diff --git a/objectcache/src/main/java/com/iainconnor/objectcache/PutCallback.java b/objectcache/src/main/java/com/iainconnor/objectcache/PutCallback.java deleted file mode 100644 index d6a844f..0000000 --- a/objectcache/src/main/java/com/iainconnor/objectcache/PutCallback.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.iainconnor.objectcache; - -public interface PutCallback { - public void onSuccess (); - - public void onFailure ( Exception e ); -} diff --git a/objectcache/src/main/java/com/iainconnor/objectcache/SoftCachedObject.java b/objectcache/src/main/java/com/iainconnor/objectcache/SoftCachedObject.java deleted file mode 100644 index 09baf0c..0000000 --- a/objectcache/src/main/java/com/iainconnor/objectcache/SoftCachedObject.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.iainconnor.objectcache; - -public class SoftCachedObject { - T object; - - public SoftCachedObject ( T object ) { - this.object = object; - } - - public T getObject () { - return object; - } -} diff --git a/objectcache/src/main/res/drawable-hdpi/ic_launcher.png b/objectcache/src/main/res/drawable-hdpi/ic_launcher.png deleted file mode 100644 index 96a442e..0000000 Binary files a/objectcache/src/main/res/drawable-hdpi/ic_launcher.png and /dev/null differ diff --git a/objectcache/src/main/res/drawable-mdpi/ic_launcher.png b/objectcache/src/main/res/drawable-mdpi/ic_launcher.png deleted file mode 100644 index 359047d..0000000 Binary files a/objectcache/src/main/res/drawable-mdpi/ic_launcher.png and /dev/null differ diff --git a/objectcache/src/main/res/drawable-xhdpi/ic_launcher.png b/objectcache/src/main/res/drawable-xhdpi/ic_launcher.png deleted file mode 100644 index 71c6d76..0000000 Binary files a/objectcache/src/main/res/drawable-xhdpi/ic_launcher.png and /dev/null differ diff --git a/objectcache/src/main/res/drawable-xxhdpi/ic_launcher.png b/objectcache/src/main/res/drawable-xxhdpi/ic_launcher.png deleted file mode 100644 index 4df1894..0000000 Binary files a/objectcache/src/main/res/drawable-xxhdpi/ic_launcher.png and /dev/null differ diff --git a/objectcache/src/main/res/values/strings.xml b/objectcache/src/main/res/values/strings.xml deleted file mode 100644 index 960c738..0000000 --- a/objectcache/src/main/res/values/strings.xml +++ /dev/null @@ -1,3 +0,0 @@ - - ObjectCache -