Android image download framework UniversialImageLoader memory cache extension (4)

Source: Internet
Author: User

Android image download framework UniversialImageLoader memory cache extension (4)

The expansion of the memory cache is very important. Whether it is the data structure or the specific implementation, it is worth our taste. Let's taste the taste here.

The memory cache extension mainly involves the following types: FIFOLimitedMemoryCache, FuzzyKeyMemoryCache, LargestLimitedMemoryCache, LimitedAgeMemoryCache, LRULimitedMemoryCache, LruMemoryCache, expires, and WeakMemoryCache.

OK. First, we will learn the LimitedMemoryCache class. LimitedMemoryCache is a subclass of LimitedMemoryCache. Its member variables are as follows:

private final List
 
   queue = Collections.synchronizedList(new LinkedList
  
   ());
  
 
The data structure of the memory cache is a thread-safe queue.

By analyzing its functions, we know that queue operations can also be reflected here. You can still observe the method for creating soft references.

@Overrideprotected Reference
 
   createReference(Bitmap value) {return new WeakReference
  
   (value);}
  
 
Next, the second class to be analyzed is FuzzyKeyMemoryCache. According to the class annotation, this class is used inside the framework and it implements the MemoryCache interface. The member variables of the current class are as follows:

private final MemoryCache cache;private final Comparator
 
   keyComparator;
 

According to the MemoryCache member variable and its initialization in the current constructor, this class is a package and the real function is implemented by the cache.

Next we will analyze the class LargestLimitedMemoryCache, which is still a subclass of LimitedMemoryCache,

The member variables are as follows:

private final Map
 
   valueSizes = Collections.synchronizedMap(new HashMap
  
   ());
  
 
It can be seen that the data structure of the current cache has changed, and it is now HashMap. The advantage of HashMa is that it is highly efficient to delete discrete data.

Focus on the methods for deleting objects:

@Overrideprotected Bitmap removeNext() {Integer maxSize = null;Bitmap largestValue = null;Set
 
  > entries = valueSizes.entrySet();synchronized (valueSizes) {for (Entry
  
    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;}
  
 
It can be seen that the iteration gets the largest image and deletes it.


The class to be analyzed is LimitedAgeMemoryCache,

Continue to follow the current member variable:

private final MemoryCache cache;private final long maxAge;private final Map
 
   loadingDates = Collections.synchronizedMap(new HashMap
  
   ());
  
 

It can be seen that the current member variable loadingDates 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 deletion time, the current object will be deleted directly from the cached data structure.


Due to the large number of cases, this lecture will be closed, and the remaining classes will be explained in the subsequent articles.

 



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.