The previous two articles focused on disk caching, which is mainly about memory caching. For the memory cache, it is also intended to be divided into two articles to explain. In this article, we mainly focus on three classes,
MemoryCache, Basememorycache and Limitedmemorycache.
First, let's look at the memory cache interface MemoryCache.
Put (String key, Bitmap value); Bitmap get (String key); Bitmap Remove (String key); collection<string> keys (); void clear ();
As can be seen from the above, the overall design of the interface is divided into 5 methods, 1, into the real bitmap 2, through the key to get bitmap 3, delete bitmap 4 by key, iterate to get all the keys of the set 5, empty memory cache.
Next we look at the abstract class Basememorycache of the interface that implements the memory cache.
As with the previous article, start with the variable.
/** Stores not strong references to objects */private final map<string, reference<bitmap>> softmap = Collectio Ns.synchronizedmap (New hashmap<string, reference<bitmap>> ());
As stated, this variable is an object that stores non-strong references.
A little attention to the following methods
@Overridepublic Bitmap get (String key) {Bitmap result = null; reference<bitmap> Reference = Softmap.get (key), if (Reference! = null) {result = Reference.get ();} return result;}
Gets the numeric bitmap in the soft reference by the key.
Finally, let's take a look at the cache class Limitedmemorycache of the finite memory cache space, which is a further extension of the Basememorycache from the inheritance relationship.
From the variable point of view:
private static final int max_normal_cache_size_in_mb = 16;private static final int max_normal_cache_size = Max_normal_cach E_SIZE_IN_MB * 1024x768 * 1024;private final int sizelimit;private final Atomicinteger cachesize;private final list<bitmap& Gt Hardcache = collections.synchronizedlist (New linkedlist<bitmap> ());
From the definition of a variable, it includes the maximum cache limit, the size of the current cache, and a collection of strongly referenced objects.
Take one of the object's stored methods to analyze:
Public Boolean put (String key, Bitmap value) {Boolean putsuccessfully = false;//Try To add value to hard Cacheint Valuesi Ze = 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);p utsuccessfully = true;} Add value to soft cachesuper.put (key, value); return putsuccessfully;}
Obviously, when you cache the picture, you need to determine the current picture's addition has no more than the overall size of the cached memory limit. If more than, first, according to the different policies, delete the first need to delete the picture, if appropriate, the current picture inserted, if not appropriate, continue the iteration.
Ok, the first article about the memory cache of a picture is here, and it will continue to be analyzed later. Hope to have some help for you oh ~
Learn more about Android Picture download framework Universialimageloader memory cache (c)