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.
[Java]View Plaincopyprint?
- 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.
[Java]View Plaincopyprint?
- /** Stores not strong references to objects * *
- Private final map<string, reference<bitmap>> softmap = Collections.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
[Java]View Plaincopyprint?
- @Override
- Public 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:
[Java]View Plaincopyprint?
- 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 * 1024x768 * 1024;
- Private final int sizeLimit;
- Private final Atomicinteger cacheSize;
- Private final List<bitmap> 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:
[Java]View Plaincopyprint?
- Public Boolean put (String key, Bitmap value) {
- Boolean putsuccessfully = false;
- //Try to add value to hard cache
- int 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 cache
- super.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.
Detailed explanation android Picture download frame Universialimageloader Cache (iii)