From b846490066d9643cf78ed2afabebb3da275670a4 Mon Sep 17 00:00:00 2001 From: daneren2005 Date: Mon, 23 Sep 2013 17:28:23 -0700 Subject: #159: First stab at caching structure using serialization --- .../daneren2005/dsub/service/CachedMusicService.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'src/github/daneren2005') diff --git a/src/github/daneren2005/dsub/service/CachedMusicService.java b/src/github/daneren2005/dsub/service/CachedMusicService.java index cc8d2dbd..767bb87c 100644 --- a/src/github/daneren2005/dsub/service/CachedMusicService.java +++ b/src/github/daneren2005/dsub/service/CachedMusicService.java @@ -42,6 +42,7 @@ import github.daneren2005.dsub.domain.Version; import github.daneren2005.dsub.util.CancellableTask; import github.daneren2005.dsub.util.ProgressListener; import github.daneren2005.dsub.util.TimeLimitedCache; +import github.daneren2005.dsub.util.FileUtil; import github.daneren2005.dsub.util.Util; /** @@ -120,10 +121,19 @@ public class CachedMusicService implements MusicService { TimeLimitedCache cache = refresh ? null : cachedMusicDirectories.get(id); MusicDirectory dir = cache == null ? null : cache.get(); if (dir == null) { + if(!refresh) { + dir = FileUtil.deserialize(context, getCacheName(context, "directory", id), MusicDirectory.class); + + if(dir != null) { + return dir; + } + } + dir = musicService.getMusicDirectory(id, name, refresh, context, progressListener); cache = new TimeLimitedCache(TTL_MUSIC_DIR, TimeUnit.SECONDS); cache.set(dir); cachedMusicDirectories.put(id, cache); + FileUtil.serialize(context, dir, getCacheName(context, "directory", id)); } return dir; } @@ -356,7 +366,10 @@ public class CachedMusicService implements MusicService { return musicService.processOfflineSyncs(context, progressListener); } - + private void getCacheName(Context context, String name, String id) { + String s = Util.getRestUrl(context, null) + id; + return name + "-" + s.hashCode() + ".ser"; + } private void checkSettingsChanged(Context context) { String newUrl = Util.getRestUrl(context, null); -- cgit v1.2.3