diff options
-rw-r--r-- | src/github/daneren2005/dsub/service/CachedMusicService.java | 22 | ||||
-rw-r--r-- | src/github/daneren2005/dsub/util/FileUtil.java | 20 |
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; |