Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
1 /*
2 * Copyright (C) 2011 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 package com.squareup.picasso;
18 import android.content.Context;
19 import android.graphics.Bitmap;
20 import java.util.LinkedHashMap;
21 import java.util.Map;
23 /** A memory cache which uses a least-recently used eviction policy. */
24 public class LruCache implements Cache {
25 final LinkedHashMap<String, Bitmap> map;
26 private final int maxSize;
28 private int size;
29 private int putCount;
30 private int evictionCount;
31 private int hitCount;
32 private int missCount;
34 /** Create a cache using an appropriate portion of the available RAM as the maximum size. */
35 public LruCache(Context context) {
36 this(Utils.calculateMemoryCacheSize(context));
37 }
39 /** Create a cache with a given maximum size in bytes. */
40 public LruCache(int maxSize) {
41 if (maxSize <= 0) {
42 throw new IllegalArgumentException("Max size must be positive.");
43 }
44 this.maxSize = maxSize;
45 this.map = new LinkedHashMap<String, Bitmap>(0, 0.75f, true);
46 }
48 @Override public Bitmap get(String key) {
49 if (key == null) {
50 throw new NullPointerException("key == null");
51 }
53 Bitmap mapValue;
54 synchronized (this) {
55 mapValue = map.get(key);
56 if (mapValue != null) {
57 hitCount++;
58 return mapValue;
59 }
60 missCount++;
61 }
63 return null;
64 }
66 @Override public void set(String key, Bitmap bitmap) {
67 if (key == null || bitmap == null) {
68 throw new NullPointerException("key == null || bitmap == null");
69 }
71 Bitmap previous;
72 synchronized (this) {
73 putCount++;
74 size += Utils.getBitmapBytes(bitmap);
75 previous = map.put(key, bitmap);
76 if (previous != null) {
77 size -= Utils.getBitmapBytes(previous);
78 }
79 }
81 trimToSize(maxSize);
82 }
84 private void trimToSize(int maxSize) {
85 while (true) {
86 String key;
87 Bitmap value;
88 synchronized (this) {
89 if (size < 0 || (map.isEmpty() && size != 0)) {
90 throw new IllegalStateException(
91 getClass().getName() + ".sizeOf() is reporting inconsistent results!");
92 }
94 if (size <= maxSize || map.isEmpty()) {
95 break;
96 }
98 Map.Entry<String, Bitmap> toEvict = map.entrySet().iterator().next();
99 key = toEvict.getKey();
100 value = toEvict.getValue();
101 map.remove(key);
102 size -= Utils.getBitmapBytes(value);
103 evictionCount++;
104 }
105 }
106 }
108 /** Clear the cache. */
109 public final void evictAll() {
110 trimToSize(-1); // -1 will evict 0-sized elements
111 }
113 /** Returns the sum of the sizes of the entries in this cache. */
114 public final synchronized int size() {
115 return size;
116 }
118 /** Returns the maximum sum of the sizes of the entries in this cache. */
119 public final synchronized int maxSize() {
120 return maxSize;
121 }
123 public final synchronized void clear() {
124 evictAll();
125 }
127 /** Returns the number of times {@link #get} returned a value. */
128 public final synchronized int hitCount() {
129 return hitCount;
130 }
132 /** Returns the number of times {@link #get} returned {@code null}. */
133 public final synchronized int missCount() {
134 return missCount;
135 }
137 /** Returns the number of times {@link #set(String, Bitmap)} was called. */
138 public final synchronized int putCount() {
139 return putCount;
140 }
142 /** Returns the number of values that have been evicted. */
143 public final synchronized int evictionCount() {
144 return evictionCount;
145 }
146 }