|
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 package org.mozilla.gecko.util; |
|
7 |
|
8 import android.support.v4.util.LruCache; |
|
9 |
|
10 import java.util.concurrent.ConcurrentHashMap; |
|
11 |
|
12 /** |
|
13 * An LruCache that also supports a set of items that will never be evicted. |
|
14 * |
|
15 * Alas, LruCache is final, so we compose rather than inherit. |
|
16 */ |
|
17 public class NonEvictingLruCache<K, V> { |
|
18 private final ConcurrentHashMap<K, V> permanent = new ConcurrentHashMap<K, V>(); |
|
19 private final LruCache<K, V> evictable; |
|
20 |
|
21 public NonEvictingLruCache(final int evictableSize) { |
|
22 evictable = new LruCache<K, V>(evictableSize); |
|
23 } |
|
24 |
|
25 public V get(K key) { |
|
26 V val = permanent.get(key); |
|
27 if (val == null) { |
|
28 return evictable.get(key); |
|
29 } |
|
30 return val; |
|
31 } |
|
32 |
|
33 public void putWithoutEviction(K key, V value) { |
|
34 permanent.put(key, value); |
|
35 } |
|
36 |
|
37 public void put(K key, V value) { |
|
38 evictable.put(key, value); |
|
39 } |
|
40 |
|
41 public void evictAll() { |
|
42 evictable.evictAll(); |
|
43 } |
|
44 } |