Detailed explanation of memory cache of Android image download framework UniversialImageLoader (3)
The previous two articles focus on disk caching. This article focuses on memory caching. I also want to explain the memory cache in two articles. In this article, we mainly focus on three categories,
MemoryCache, BaseMemoryCache, and LimitedMemoryCache.
First, let's take a look at the memory cache interface MemoryCache.
put(String key, Bitmap value);Bitmap get(String key);Bitmap remove(String key);Collection
keys();void clear();
We can see from the above that the overall interface design is divided into five methods, 1. Store it to a real Bitmap. 2. Obtain Bitmap through the key. 3. Delete Bitmap with the key. 4. Obtain the set of all keys iteratively. 5. Clear the memory cache.
Next we will look at the abstract class BaseMemoryCache that implements the memory cache interface.
Like the previous article, we should start with variables first.
/** Stores not strong references to objects */private final Map
> softMap = Collections.synchronizedMap(new HashMap
>());
As mentioned, this variable is used to store non-strongly referenced objects.
Pay attention to the following methods in the pipeline.
@Overridepublic Bitmap get(String key) {Bitmap result = null;Reference
reference = softMap.get(key);if (reference != null) {result = reference.get();}return result;}
Obtain the value Bitmap in the soft reference using the key.
Finally, let's take a look at the limited memory cache class LimitedMemoryCache. From the inheritance relationship, it is a further extension of BaseMemoryCache.
From the perspective of variables:
private static final int MAX_NORMAL_CACHE_SIZE_IN_MB = 16;private static final int MAX_NORMAL_CACHE_SIZE = MAX_NORMAL_CACHE_SIZE_IN_MB * 1024 * 1024;private final int sizeLimit;private final AtomicInteger cacheSize;private final List
hardCache = Collections.synchronizedList(new LinkedList
());
From the definition of variables, this includes the Maximum Cache restrictions, the current cache size, and a set of strongly referenced objects.
Analyze the Object Storage Methods:
public boolean put(String key, Bitmap value) {boolean putSuccessfully = false;// Try to add value to hard cacheint valueSize = getSize(value);int sizeLimit = getSizeLimit();int curCacheSize = cacheSize.get();if (valueSize < sizeLimit) {while (curCacheSize + valueSize > sizeLimit) {Bitmap removedValue = removeNext();if (hardCache.remove(removedValue)) {curCacheSize = cacheSize.addAndGet(-getSize(removedValue));}}hardCache.add(value);cacheSize.addAndGet(valueSize);putSuccessfully = true;}// Add value to soft cachesuper.put(key, value);return putSuccessfully;}
Obviously, when caching an image, you must first determine whether the size of the current image exceeds the size limit of the overall cache memory. If this parameter is exceeded, first Delete the image to be deleted first according to different policies. If this parameter is appropriate, insert the current image. If not, continue iteration.
OK. The first article about the image memory cache is described here, and the analysis will continue later. Hope to help you ~