Memory cache extension is quite important, whether it is a data structure or a specific implementation or it is worth our careful taste, below we can taste the fun of it.
The expansion of the memory cache mainly learns the following classes: Fifolimitedmemorycache, Fuzzykeymemorycache, Largestlimitedmemorycache, Limitedagememorycache, Lrulimitedmemorycache, Lrumemorycache, Usingfreqlimitedmemorycache, and Weakmemorycache.
Ok, First we learn limitedmemorycache this class. Limitedmemorycache is a subclass of Limitedmemorycache, and its member variables are as follows:
Private final list<bitmap> queue = collections.synchronizedlist (new linkedlist<bitmap> ());
The data structure of the memory cache is a thread-safe queue.
Analyzing its functions, we know that the operation of the queue can also be reflected here. Just a way to create a soft reference is to look at it.
@Overrideprotected reference<bitmap> createreference (Bitmap value) {return new weakreference<bitmap> ( value);}
Next, the second class that needs to be analyzed is Fuzzykeymemorycache, which, according to the class's annotations, is used internally within the framework, which implements the MemoryCache interface. The member variables of the current class are as follows:
Private final MemoryCache cache;private final comparator<string> keycomparator;
By memorycache This member variable and initializing it in the current constructor, the class is a wrapper, and the real function is implemented by the cache.
The next class we're going to analyze is Largestlimitedmemorycache, which is still a subclass of Limitedmemorycache,
Its member variables are as follows:
Private final Map<bitmap, integer> valuesizes = Collections.synchronizedmap (New Hashmap<bitmap, Integer> () );
Thus, the current data structure of the cache has changed, the current advantage of Hashmap,hashma is that the removal of discrete data efficiency is high.
Focus on how to delete objects:
@Overrideprotected Bitmap Removenext () {Integer maxSize = null; Bitmap largestvalue = null; Set<entry<bitmap, integer>> entries = Valuesizes.entryset (); synchronized (valuesizes) {for (entry< Bitmap, integer> entry:entries) {if (Largestvalue = = null) {Largestvalue = Entry.getkey (); maxSize = Entry.getvalue (); } else {Integer size = Entry.getvalue (); if (Size > maxSize) {maxSize = Size;largestvalue = Entry.getkey ();}}}} Valuesizes.remove (Largestvalue); return largestvalue;}
This shows that the iteration gets the largest picture and deletes it.
The next class to analyze is Limitedagememorycache,
Continue to focus on the current member variables:
Private final MemoryCache cache;private final long maxage;private final map<string, long> loadingdates = Collections . Synchronizedmap (New hashmap<string, long> ());
The current member variable loadingdates is visible, and the current is HashMap.
Focus on the following member variables
@Overridepublic Bitmap get (String key) {Long loadingdate = Loadingdates.get (key); if (loadingdate! = null && System . Currenttimemillis ()-loadingdate > MaxAge) {cache.remove (key); Loadingdates.remove (key);} return Cache.get (key);}
If the current file has reached the time of deletion, the current object is deleted directly from the cached data structure.
Because of the more, this is the point to stop the pen here, the rest of the following classes will be explained in the following article.
Learn more about Android Picture download framework Universialimageloader in-memory cache extension (iv)