aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorScott Jackson <daneren2005@gmail.com>2013-12-26 23:02:54 -0800
committerScott Jackson <daneren2005@gmail.com>2013-12-26 23:02:54 -0800
commit9faa4f340c762271f86ef5204fa7142de9300ec5 (patch)
treecfe5e80dfce37b2e3e4b30d248919ecb15cf8961 /src
parent84018b356c1ba48cdbaa111b986cab0ab4251dce (diff)
downloaddsub-9faa4f340c762271f86ef5204fa7142de9300ec5.tar.gz
dsub-9faa4f340c762271f86ef5204fa7142de9300ec5.tar.bz2
dsub-9faa4f340c762271f86ef5204fa7142de9300ec5.zip
#228 Add time sensitive serialized podcast cache
Diffstat (limited to 'src')
-rw-r--r--src/github/daneren2005/dsub/service/CachedMusicService.java22
-rw-r--r--src/github/daneren2005/dsub/util/FileUtil.java20
2 files changed, 38 insertions, 4 deletions
diff --git a/src/github/daneren2005/dsub/service/CachedMusicService.java b/src/github/daneren2005/dsub/service/CachedMusicService.java
index 3c6168d2..3930fc3e 100644
--- a/src/github/daneren2005/dsub/service/CachedMusicService.java
+++ b/src/github/daneren2005/dsub/service/CachedMusicService.java
@@ -382,7 +382,27 @@ public class CachedMusicService implements MusicService {
@Override
public MusicDirectory getPodcastEpisodes(boolean refresh, String id, Context context, ProgressListener progressListener) throws Exception {
- return musicService.getPodcastEpisodes(refresh, id, context, progressListener);
+ checkSettingsChanged(context);
+ String altId = "p-" + id;
+ TimeLimitedCache<MusicDirectory> cache = refresh ? null : cachedMusicDirectories.get(altId);
+ MusicDirectory result = (cache == null) ? null : cache.get();
+
+ if(result == null) {
+ if(!refresh) {
+ result = FileUtil.deserialize(context, getCacheName(context, "directory", altId), MusicDirectory.class, 10);
+ }
+
+ if(result == null) {
+ result = musicService.getPodcastEpisodes(refresh, id, context, progressListener);
+ FileUtil.serialize(context, result, getCacheName(context, "directory", altId));
+ }
+ cache = new TimeLimitedCache<MusicDirectory>(TTL_MUSIC_DIR, TimeUnit.SECONDS);
+ cache.set(result);
+ cachedMusicDirectories.put(altId, cache);
+ }
+ return result;
+
+
}
@Override
diff --git a/src/github/daneren2005/dsub/util/FileUtil.java b/src/github/daneren2005/dsub/util/FileUtil.java
index 57c21e98..f72ae66b 100644
--- a/src/github/daneren2005/dsub/util/FileUtil.java
+++ b/src/github/daneren2005/dsub/util/FileUtil.java
@@ -26,6 +26,7 @@ import java.io.FilenameFilter;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.util.Arrays;
+import java.util.Date;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.Iterator;
@@ -419,13 +420,26 @@ public class FileUtil {
}
}
- public static <T extends Serializable> T deserialize(Context context, String fileName, Class<T> tClass) {
+ public static <T extends Serializable> T deserialize(Context context, String fileName, Class<T> tClass) {
+ return deserialize(context, fileName, tClass, 0);
+ }
+
+ public static <T extends Serializable> T deserialize(Context context, String fileName, Class<T> tClass, int hoursOld) {
synchronized (kryo) {
Input in = null;
try {
- RandomAccessFile file = new RandomAccessFile(context.getCacheDir() + "/" + fileName, "r");
+ File file = new File(context.getCacheDir(), fileName);
+
+ Date fileDate = new Date(file.lastModified());
+ // Convert into hours
+ long age = (new Date().getTime() - fileDate.getTime()) / 1000 / 3600;
+ if(hoursOld != 0 && age > hoursOld) {
+ return null;
+ }
+
+ RandomAccessFile randomFile = new RandomAccessFile(file, "r");
- in = new Input(new FileInputStream(file.getFD()));
+ in = new Input(new FileInputStream(randomFile.getFD()));
T result = (T) kryo.readObject(in, tClass);
Log.i(TAG, "Deserialized object from " + fileName);
return result;