KeyStoreLruCache.java revision 81e0c8491f22c64300182c652ac2add96888dd2e
1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.systemui.recents.model; 18 19import android.util.LruCache; 20 21import java.util.HashMap; 22 23/** 24 * An LRU cache that support querying the keys as well as values. By using the Task's key, we can 25 * prevent holding onto a reference to the Task resource data, while keeping the cache data in 26 * memory where necessary. 27 */ 28public class KeyStoreLruCache<V> { 29 // We keep a set of keys that are associated with the LRU cache, so that we can find out 30 // information about the Task that was previously in the cache. 31 HashMap<Integer, Task.TaskKey> mTaskKeys = new HashMap<Integer, Task.TaskKey>(); 32 // The cache implementation 33 LruCache<Integer, V> mCache; 34 35 public KeyStoreLruCache(int cacheSize) { 36 mCache = new LruCache<Integer, V>(cacheSize) { 37 38 @Override 39 protected void entryRemoved(boolean evicted, Integer taskId, V oldV, V newV) { 40 mTaskKeys.remove(taskId); 41 } 42 }; 43 } 44 45 /** Gets a specific entry in the cache. */ 46 final V get(Task.TaskKey key) { 47 return mCache.get(key.id); 48 } 49 50 /** 51 * Returns the value only if the Task has not updated since the last time it was in the cache. 52 */ 53 final V getAndInvalidateIfModified(Task.TaskKey key) { 54 Task.TaskKey lastKey = mTaskKeys.get(key.id); 55 if (lastKey != null && (lastKey.lastActiveTime < key.lastActiveTime)) { 56 // The task has updated (been made active since the last time it was put into the 57 // LRU cache) so invalidate that item in the cache 58 remove(key); 59 return null; 60 } 61 // Either the task does not exist in the cache, or the last active time is the same as 62 // the key specified, so return what is in the cache 63 return mCache.get(key.id); 64 } 65 66 /** Puts an entry in the cache for a specific key. */ 67 final void put(Task.TaskKey key, V value) { 68 mCache.put(key.id, value); 69 mTaskKeys.put(key.id, key); 70 } 71 72 /** Removes a cache entry for a specific key. */ 73 final void remove(Task.TaskKey key) { 74 mCache.remove(key.id); 75 mTaskKeys.remove(key.id); 76 } 77 78 /** Removes all the entries in the cache. */ 79 final void evictAll() { 80 mCache.evictAll(); 81 mTaskKeys.clear(); 82 } 83 84 /** Returns the size of the cache. */ 85 final int size() { 86 return mCache.size(); 87 } 88 89 /** Trims the cache to a specific size */ 90 final void trimToSize(int cacheSize) { 91 mCache.resize(cacheSize); 92 } 93} 94