From a1c809804609f30f7662adbda94aee1988014cab Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Sun, 21 Jul 2013 12:21:01 -0700 Subject: Remove custom LRUCache, use standard --- .../dsub/service/CachedMusicService.java | 12 +-- .../dsub/service/DownloadServiceImpl.java | 4 +- .../src/github/daneren2005/dsub/util/LRUCache.java | 102 --------------------- 3 files changed, 7 insertions(+), 111 deletions(-) delete mode 100644 subsonic-android/src/github/daneren2005/dsub/util/LRUCache.java diff --git a/subsonic-android/src/github/daneren2005/dsub/service/CachedMusicService.java b/subsonic-android/src/github/daneren2005/dsub/service/CachedMusicService.java index e9ee9bab..943b5eb2 100644 --- a/subsonic-android/src/github/daneren2005/dsub/service/CachedMusicService.java +++ b/subsonic-android/src/github/daneren2005/dsub/service/CachedMusicService.java @@ -25,6 +25,7 @@ import org.apache.http.HttpResponse; import android.content.Context; import android.graphics.Bitmap; +import android.support.v4.util.LruCache; import github.daneren2005.dsub.domain.ChatMessage; import github.daneren2005.dsub.domain.Genre; import github.daneren2005.dsub.domain.Indexes; @@ -34,14 +35,11 @@ import github.daneren2005.dsub.domain.MusicDirectory; import github.daneren2005.dsub.domain.MusicFolder; import github.daneren2005.dsub.domain.Playlist; import github.daneren2005.dsub.domain.PodcastChannel; -import github.daneren2005.dsub.domain.PodcastEpisode; import github.daneren2005.dsub.domain.SearchCritera; import github.daneren2005.dsub.domain.SearchResult; import github.daneren2005.dsub.domain.Share; import github.daneren2005.dsub.domain.Version; import github.daneren2005.dsub.util.CancellableTask; -import github.daneren2005.dsub.util.FileUtil; -import github.daneren2005.dsub.util.LRUCache; import github.daneren2005.dsub.util.ProgressListener; import github.daneren2005.dsub.util.TimeLimitedCache; import github.daneren2005.dsub.util.Util; @@ -55,7 +53,7 @@ public class CachedMusicService implements MusicService { private static final int TTL_MUSIC_DIR = 5 * 60; // Five minutes private final MusicService musicService; - private final LRUCache> cachedMusicDirectories; + private final LruCache> cachedMusicDirectories; private final TimeLimitedCache cachedLicenseValid = new TimeLimitedCache(120, TimeUnit.SECONDS); private final TimeLimitedCache cachedIndexes = new TimeLimitedCache(60 * 60, TimeUnit.SECONDS); private final TimeLimitedCache> cachedPlaylists = new TimeLimitedCache>(3600, TimeUnit.SECONDS); @@ -66,7 +64,7 @@ public class CachedMusicService implements MusicService { public CachedMusicService(MusicService musicService) { this.musicService = musicService; - cachedMusicDirectories = new LRUCache>(MUSIC_DIR_CACHE_SIZE); + cachedMusicDirectories = new LruCache>(MUSIC_DIR_CACHE_SIZE); } @Override @@ -106,7 +104,7 @@ public class CachedMusicService implements MusicService { if (refresh) { cachedIndexes.clear(); cachedMusicFolders.clear(); - cachedMusicDirectories.clear(); + cachedMusicDirectories.evictAll(); } Indexes result = cachedIndexes.get(); if (result == null) { @@ -364,7 +362,7 @@ public class CachedMusicService implements MusicService { String newUrl = Util.getRestUrl(context, null); if (!Util.equals(newUrl, restUrl)) { cachedMusicFolders.clear(); - cachedMusicDirectories.clear(); + cachedMusicDirectories.evictAll(); cachedLicenseValid.clear(); cachedIndexes.clear(); cachedPlaylists.clear(); diff --git a/subsonic-android/src/github/daneren2005/dsub/service/DownloadServiceImpl.java b/subsonic-android/src/github/daneren2005/dsub/service/DownloadServiceImpl.java index e8803ad4..04875f34 100644 --- a/subsonic-android/src/github/daneren2005/dsub/service/DownloadServiceImpl.java +++ b/subsonic-android/src/github/daneren2005/dsub/service/DownloadServiceImpl.java @@ -34,7 +34,6 @@ import github.daneren2005.dsub.domain.RepeatMode; import github.daneren2005.dsub.receiver.MediaButtonIntentReceiver; import github.daneren2005.dsub.util.CancellableTask; import github.daneren2005.dsub.util.Constants; -import github.daneren2005.dsub.util.LRUCache; import github.daneren2005.dsub.util.ShufflePlayBuffer; import github.daneren2005.dsub.util.SimpleServiceBinder; import github.daneren2005.dsub.util.Util; @@ -62,6 +61,7 @@ import android.os.IBinder; import android.os.Looper; import android.os.PowerManager; import android.util.Log; +import android.support.v4.util.LruCache; import java.net.URLEncoder; /** @@ -95,7 +95,7 @@ public class DownloadServiceImpl extends Service implements DownloadService { private final DownloadServiceLifecycleSupport lifecycleSupport = new DownloadServiceLifecycleSupport(this); private final ShufflePlayBuffer shufflePlayBuffer = new ShufflePlayBuffer(this); - private final LRUCache downloadFileCache = new LRUCache(100); + private final LruCache downloadFileCache = new LruCache(100); private final List cleanupCandidates = new ArrayList(); private final Scrobbler scrobbler = new Scrobbler(); private final JukeboxService jukeboxService = new JukeboxService(this); diff --git a/subsonic-android/src/github/daneren2005/dsub/util/LRUCache.java b/subsonic-android/src/github/daneren2005/dsub/util/LRUCache.java deleted file mode 100644 index 9b12a204..00000000 --- a/subsonic-android/src/github/daneren2005/dsub/util/LRUCache.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - This file is part of Subsonic. - - Subsonic is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - Subsonic is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with Subsonic. If not, see . - - Copyright 2009 (C) Sindre Mehus - */ -package github.daneren2005.dsub.util; - -import java.lang.ref.SoftReference; -import java.util.HashMap; -import java.util.Map; - -/** - * @author Sindre Mehus - */ -public class LRUCache{ - - private final int capacity; - private final Map map; - - public LRUCache(int capacity) { - map = new HashMap(capacity); - this.capacity = capacity; - } - - public synchronized V get(K key) { - TimestampedValue value = map.get(key); - - V result = null; - if (value != null) { - value.updateTimestamp(); - result = value.getValue(); - } - - return result; - } - - public synchronized void put(K key, V value) { - if (map.size() >= capacity) { - removeOldest(); - } - map.put(key, new TimestampedValue(value)); - } - - public void clear() { - map.clear(); - } - - private void removeOldest() { - K oldestKey = null; - long oldestTimestamp = Long.MAX_VALUE; - - for (Map.Entry entry : map.entrySet()) { - K key = entry.getKey(); - TimestampedValue value = entry.getValue(); - if (value.getTimestamp() < oldestTimestamp) { - oldestTimestamp = value.getTimestamp(); - oldestKey = key; - } - } - - if (oldestKey != null) { - map.remove(oldestKey); - } - } - - private final class TimestampedValue { - - private final SoftReference value; - private long timestamp; - - public TimestampedValue(V value) { - this.value = new SoftReference(value); - updateTimestamp(); - } - - public V getValue() { - return value.get(); - } - - public long getTimestamp() { - return timestamp; - } - - public void updateTimestamp() { - timestamp = System.currentTimeMillis(); - } - } - -} \ No newline at end of file -- cgit v1.2.3