Detailed explanation android Picture download frame Universialimageloader Cache (iii)

Source: Internet
Author: User

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?
    1. Put (String key, Bitmap value);
    2. Bitmap get (String key);
    3. Bitmap Remove (String key);
    4. collection<string> keys ();
    5. 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?
    1. /** Stores not strong references to objects * *
    2. 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?
  1. @Override
  2. Public Bitmap Get (String key) {
  3. Bitmap result = null;
  4. reference<bitmap> Reference = Softmap.get (key);
  5. if (Reference! = null) {
  6. result = Reference.get ();
  7. }
  8. return result;
  9. }


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?
  1. Private static final int max_normal_cache_size_in_mb = 16;
  2. Private static final int max_normal_cache_size = MAX_NORMAL_CACHE_SIZE_IN_MB * 1024x768 * 1024;
  3. Private final int sizeLimit;
  4. Private final Atomicinteger cacheSize;
  5. 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?
  1. Public Boolean put (String key, Bitmap value) {
  2. Boolean putsuccessfully = false;
  3. //Try to add value to hard cache
  4. int valuesize = getsize (value);
  5. int sizeLimit = Getsizelimit ();
  6. int curcachesize = Cachesize.get ();
  7. if (Valuesize < sizeLimit) {
  8. While (curcachesize + valuesize > SizeLimit) {
  9. Bitmap Removedvalue = Removenext ();
  10. if (Hardcache.remove (Removedvalue)) {
  11. Curcachesize = Cachesize.addandget (-getsize (Removedvalue));
  12. }
  13. }
  14. Hardcache.add (value);
  15. Cachesize.addandget (valuesize);
  16. putsuccessfully = true;
  17. }
  18. //ADD value to soft cache
  19. super.put (key, value);
  20. return putsuccessfully;
  21. }


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)

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.