Bug 941868 - Part 1: add NonEvictingLruCache. r=mcomella
authorRichard Newman <rnewman@mozilla.com>
Fri, 22 Nov 2013 17:12:20 -0800
changeset 171820 b660e7e101847514620688a94d023b274bd199f2
parent 171819 34e6d95c9eb5c703d840fb2b4b735a99be3f9cdb
child 171821 3048b551f5b7eedcc215b4800ded8c5e74ae8914
push id3224
push userlsblakk@mozilla.com
push dateTue, 04 Feb 2014 01:06:49 +0000
treeherdermozilla-beta@60c04d0987f1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmcomella
bugs941868
milestone28.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 941868 - Part 1: add NonEvictingLruCache. r=mcomella
mobile/android/base/favicons/Favicons.java
mobile/android/base/moz.build
mobile/android/base/util/NonEvictingLruCache.java
--- a/mobile/android/base/favicons/Favicons.java
+++ b/mobile/android/base/favicons/Favicons.java
@@ -8,21 +8,21 @@ package org.mozilla.gecko.favicons;
 import org.mozilla.gecko.AboutPages;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.Tab;
 import org.mozilla.gecko.Tabs;
 import org.mozilla.gecko.db.BrowserDB;
 import org.mozilla.gecko.favicons.cache.FaviconCache;
 import org.mozilla.gecko.gfx.BitmapUtils;
 import org.mozilla.gecko.util.ThreadUtils;
+import org.mozilla.gecko.util.NonEvictingLruCache;
 
 import android.content.Context;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
-import android.support.v4.util.LruCache;
 import android.text.TextUtils;
 import android.util.Log;
 
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -31,17 +31,17 @@ import java.util.Set;
 
 public class Favicons {
     private static final String LOGTAG = "GeckoFavicons";
 
     // Size of the favicon bitmap cache, in bytes (Counting payload only).
     public static final int FAVICON_CACHE_SIZE_BYTES = 512 * 1024;
 
     // Number of URL mappings from page URL to Favicon URL to cache in memory.
-    public static final int PAGE_URL_MAPPINGS_TO_STORE = 128;
+    public static final int NUM_PAGE_URL_MAPPINGS_TO_STORE = 128;
 
     public static final int NOT_LOADING  = 0;
     public static final int LOADED       = 1;
     public static final int FLAG_PERSIST = 2;
     public static final int FLAG_SCALE   = 4;
 
     protected static Context sContext;
 
@@ -50,17 +50,17 @@ public class Favicons {
 
     // The density-adjusted default Favicon dimensions.
     public static int sDefaultFaviconSize;
 
     private static final Map<Integer, LoadFaviconTask> sLoadTasks = Collections.synchronizedMap(new HashMap<Integer, LoadFaviconTask>());
 
     // Cache to hold mappings between page URLs and Favicon URLs. Used to avoid going to the DB when
     // doing so is not necessary.
-    private static final LruCache<String, String> sPageURLMappings = new LruCache<String, String>(PAGE_URL_MAPPINGS_TO_STORE);
+    private static final NonEvictingLruCache<String, String> sPageURLMappings = new NonEvictingLruCache<String, String>(NUM_PAGE_URL_MAPPINGS_TO_STORE);
 
     public static String getFaviconURLForPageURLFromCache(String pageURL) {
         return sPageURLMappings.get(pageURL);
     }
 
     /**
      * Insert the given pageUrl->faviconUrl mapping into the memory cache of such mappings.
      * Useful for short-circuiting local database access.
--- a/mobile/android/base/moz.build
+++ b/mobile/android/base/moz.build
@@ -44,16 +44,17 @@ gujar.sources += [
     'util/GeckoBackgroundThread.java',
     'util/GeckoEventListener.java',
     'util/GeckoEventResponder.java',
     'util/GeckoJarReader.java',
     'util/HardwareUtils.java',
     'util/INIParser.java',
     'util/INISection.java',
     'util/JSONUtils.java',
+    'util/NonEvictingLruCache.java',
     'util/StringUtils.java',
     'util/ThreadUtils.java',
     'util/UiAsyncTask.java',
 ]
 gujar.extra_jars = [
     'gecko-mozglue.jar'
 ]
 gujar.javac_flags += ['-Xlint:all,-deprecation']
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/util/NonEvictingLruCache.java
@@ -0,0 +1,44 @@
+/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+package org.mozilla.gecko.util;
+
+import android.support.v4.util.LruCache;
+
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * An LruCache that also supports a set of items that will never be evicted.
+ *
+ * Alas, LruCache is final, so we compose rather than inherit.
+ */
+public class NonEvictingLruCache<K, V> {
+    private final ConcurrentHashMap<K, V> permanent = new ConcurrentHashMap<K, V>();
+    private final LruCache<K, V> evictable;
+
+    public NonEvictingLruCache(final int evictableSize) {
+        evictable = new LruCache<K, V>(evictableSize);
+    }
+
+    public V get(K key) {
+        V val = permanent.get(key);
+        if (val == null) {
+            return evictable.get(key);
+        }
+        return val;
+    }
+
+    public void putWithoutEviction(K key, V value) {
+        permanent.put(key, value);
+    }
+
+    public void put(K key, V value) {
+        evictable.put(key, value);
+    }
+
+    public void evictAll() {
+        evictable.evictAll();
+    }
+}