summaryrefslogtreecommitdiffstats
path: root/java/com/android/dialer/util/ExpirableCache.java
blob: a1b0afd7494d9918a9a224c3cbedf356db62a3fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.dialer.util;

import android.util.LruCache;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.ThreadSafe;

/**
 * An LRU cache in which all items can be marked as expired at a given time and it is possible to
 * query whether a particular cached value is expired or not.
 *
 * <p>A typical use case for this is caching of values which are expensive to compute but which are
 * still useful when out of date.
 *
 * <p>Consider a cache for contact information:
 *
 * <pre>{@code
 * private ExpirableCache<String, Contact> mContactCache;
 * }</pre>
 *
 * which stores the contact information for a given phone number.
 *
 * <p>When we need to store contact information for a given phone number, we can look up the info in
 * the cache:
 *
 * <pre>{@code
 * CachedValue<Contact> cachedContact = mContactCache.getCachedValue(phoneNumber);
 * }</pre>
 *
 * We might also want to fetch the contact information again if the item is expired.
 *
 * <pre>
 *     if (cachedContact.isExpired()) {
 *         fetchContactForNumber(phoneNumber,
 *                 new FetchListener() {
 *                     &#64;Override
 *                     public void onFetched(Contact contact) {
 *                         mContactCache.put(phoneNumber, contact);
 *                     }
 *                 });
 *     }</pre>
 *
 * and insert it back into the cache when the fetch completes.
 *
 * <p>At a certain point we want to expire the content of the cache because we know the content may
 * no longer be up-to-date, for instance, when resuming the activity this is shown into:
 *
 * <pre>
 *     &#64;Override
 *     protected onResume() {
 *         // We were paused for some time, the cached value might no longer be up to date.
 *         mContactCache.expireAll();
 *         super.onResume();
 *     }
 * </pre>
 *
 * The values will be still available from the cache, but they will be expired.
 *
 * <p>If interested only in the value itself, not whether it is expired or not, one should use the
 * {@link #getPossiblyExpired(Object)} method. If interested only in non-expired values, one should
 * use the {@link #get(Object)} method instead.
 *
 * <p>This class wraps around an {@link LruCache} instance: it follows the {@link LruCache} behavior
 * for evicting items when the cache is full. It is possible to supply your own subclass of LruCache
 * by using the {@link #create(LruCache)} method, which can define a custom expiration policy. Since
 * the underlying cache maps keys to cached values it can determine which items are expired and
 * which are not, allowing for an implementation that evicts expired items before non expired ones.
 *
 * <p>This class is thread-safe.
 *
 * @param <K> the type of the keys
 * @param <V> the type of the values
 */
@ThreadSafe
public class ExpirableCache<K, V> {

  /**
   * The current generation of items added to the cache.
   *
   * <p>Items in the cache can belong to a previous generation, but in that case they would be
   * expired.
   *
   * @see ExpirableCache.CachedValue#isExpired()
   */
  private final AtomicInteger generation;
  /** The underlying cache used to stored the cached values. */
  private LruCache<K, CachedValue<V>> cache;

  private ExpirableCache(LruCache<K, CachedValue<V>> cache) {
    this.cache = cache;
    generation = new AtomicInteger(0);
  }

  /**
   * Creates a new {@link ExpirableCache} that wraps the given {@link LruCache}.
   *
   * <p>The created cache takes ownership of the cache passed in as an argument.
   *
   * @param <K> the type of the keys
   * @param <V> the type of the values
   * @param cache the cache to store the value in
   * @return the newly created expirable cache
   * @throws IllegalArgumentException if the cache is not empty
   */
  public static <K, V> ExpirableCache<K, V> create(LruCache<K, CachedValue<V>> cache) {
    return new ExpirableCache<K, V>(cache);
  }

  /**
   * Creates a new {@link ExpirableCache} with the given maximum size.
   *
   * @param <K> the type of the keys
   * @param <V> the type of the values
   * @return the newly created expirable cache
   */
  public static <K, V> ExpirableCache<K, V> create(int maxSize) {
    return create(new LruCache<K, CachedValue<V>>(maxSize));
  }

  /**
   * Returns the cached value for the given key, or null if no value exists.
   *
   * <p>The cached value gives access both to the value associated with the key and whether it is
   * expired or not.
   *
   * <p>If not interested in whether the value is expired, use {@link #getPossiblyExpired(Object)}
   * instead.
   *
   * <p>If only wants values that are not expired, use {@link #get(Object)} instead.
   *
   * @param key the key to look up
   */
  public CachedValue<V> getCachedValue(K key) {
    return cache.get(key);
  }

  /**
   * Returns the value for the given key, or null if no value exists.
   *
   * <p>When using this method, it is not possible to determine whether the value is expired or not.
   * Use {@link #getCachedValue(Object)} to achieve that instead. However, if using {@link
   * #getCachedValue(Object)} to determine if an item is expired, one should use the item within the
   * {@link CachedValue} and not call {@link #getPossiblyExpired(Object)} to get the value
   * afterwards, since that is not guaranteed to return the same value or that the newly returned
   * value is in the same state.
   *
   * @param key the key to look up
   */
  public V getPossiblyExpired(K key) {
    CachedValue<V> cachedValue = getCachedValue(key);
    return cachedValue == null ? null : cachedValue.getValue();
  }

  /**
   * Returns the value for the given key only if it is not expired, or null if no value exists or is
   * expired.
   *
   * <p>This method will return null if either there is no value associated with this key or if the
   * associated value is expired.
   *
   * @param key the key to look up
   */
  public V get(K key) {
    CachedValue<V> cachedValue = getCachedValue(key);
    return cachedValue == null || cachedValue.isExpired() ? null : cachedValue.getValue();
  }

  /**
   * Puts an item in the cache.
   *
   * <p>Newly added item will not be expired until {@link #expireAll()} is next called.
   *
   * @param key the key to look up
   * @param value the value to associate with the key
   */
  public void put(K key, V value) {
    cache.put(key, newCachedValue(value));
  }

  /**
   * Mark all items currently in the cache as expired.
   *
   * <p>Newly added items after this call will be marked as not expired.
   *
   * <p>Expiring the items in the cache does not imply they will be evicted.
   */
  public void expireAll() {
    generation.incrementAndGet();
  }

  /**
   * Creates a new {@link CachedValue} instance to be stored in this cache.
   *
   * <p>Implementation of {@link LruCache#create(K)} can use this method to create a new entry.
   */
  public CachedValue<V> newCachedValue(V value) {
    return new GenerationalCachedValue<V>(value, generation);
  }

  /**
   * A cached value stored inside the cache.
   *
   * <p>It provides access to the value stored in the cache but also allows to check whether the
   * value is expired.
   *
   * @param <V> the type of value stored in the cache
   */
  public interface CachedValue<V> {

    /** Returns the value stored in the cache for a given key. */
    V getValue();

    /**
     * Checks whether the value, while still being present in the cache, is expired.
     *
     * @return true if the value is expired
     */
    boolean isExpired();
  }

  /** Cached values storing the generation at which they were added. */
  @Immutable
  private static class GenerationalCachedValue<V> implements ExpirableCache.CachedValue<V> {

    /** The value stored in the cache. */
    public final V value;
    /** The generation at which the value was added to the cache. */
    private final int generation;
    /** The atomic integer storing the current generation of the cache it belongs to. */
    private final AtomicInteger cacheGeneration;

    /**
     * @param cacheGeneration the atomic integer storing the generation of the cache in which this
     *     value will be stored
     */
    public GenerationalCachedValue(V value, AtomicInteger cacheGeneration) {
      this.value = value;
      this.cacheGeneration = cacheGeneration;
      // Snapshot the current generation.
      generation = this.cacheGeneration.get();
    }

    @Override
    public V getValue() {
      return value;
    }

    @Override
    public boolean isExpired() {
      return generation != cacheGeneration.get();
    }
  }
}