The LruCache of Picasso

Source: Internet
Author: User

LRU is a page substitution algorithm that, when the page that the process accesses is not in memory and the memory is free of free space, the system must pull a page from memory to the swap area of the disk.

Which page is the page that is paged out? LRU is one of the algorithms, called "the most recent unused algorithm."

Starting from the construction method,

 /** create a cache using  an appropriate portion of the available ram as the maximum  size. */  public lrucache (Context context)  {    this ( Utils.calculatememorycachesize (context));   }  /** create a cache with  a given maximum size in bytes. */  public lrucache (int  maxsize)  {    if  (maxsize <= 0)  {       throw new illegalargumentexception ("max size must be positive.");     }    this.maxSize = maxSize;     This.map = new linkedhashmap<string, bitmap> (0, 0.75f, true);   } 

The maximum cache size was initialized, and the Linkedhashmap

The most important should be the Get,set method:

@Override  public bitmap get (String key)  {    if  (key ==  null)  {      throw new nullpointerexception ("key ==  null ");    }    bitmap mapvalue;     synchronized  (This)  {          mapvalue =  map.get (key);     //takes the corresponding bitmap      if  from the inside according to Key ( Mapvalue != null)  {    //can fetch the words, record the number of hits hitcount++, return to the bitmap         hitCount++;        return  mapvalue;      }      misscount++;                 //not taken, record the number of misses misscount+ +    }    return null;                 //finally returns NULL, indicating no cache  &nbsp found;}
@Override  public void set (String key, bitmap bitmap)  {     if  (key == null | |  bitmap == null)  {      throw new  NullPointerException ("key == null | |  bitmap == null ");    }    bitmap previous;     synchronized  (This)  {      putCount++;                   size +=  utils.getbitmapbytes (bitmap);    //  represents the size of the cache size plus the sizes of the newly placed bitmap        previous = map.put (Key, bitmap);   //put () returns the corresponding bitmap before the key, no, Back to null      if  (previous != null)  {                      size -= utils.getbitmapbytes (previous);         //If the previous key does have a corresponding bitmap, it is now updated, naturally minus the previous bitmap size        }    }    trimtosize (maxSize);   }

Private void trimtosize (int maxsize)  {    while  (True)  {       String key;      Bitmap value;       synchronized  (This)  {         if  (size < 0 | |   (Map.isempty ()  && size != 0))  {           throw new illegalstateexception (               getclass (). GetName ()  +  ". SizeOf ()  is reporting  inconsistent results! ");         }        if  (Size  <= maxsize | |  map.isempty ())  {    //If the size of the cache has not exceeded the limit, do not do the processing            break;        }         /* Can be executed here to show that the cache has exceeded the maximum limit, so here should use the LRU algorithm, the larger than the maximum cache size removed            Note: The map used in the Linkedhashmap, we new, is used to have three parameters of the construction method, the third parameter           The  true represents the least recently used at the front, and false indicates sorting in order of insertion. So, when specified as true, that is, the map is using the LRU algorithm            at this time, we take the first one, each time only delete one, Loop down and know size <= maxsize        */                 Map.Entry<String, Bitmap>  toevict = map.entryset (). iterator (). Next ();         key  = toevict.getkey ();        value =  Toevict.getvalue ();         map.remove (key); &NBSP;&NBSP;&NBSP;&NBSp;    size -= utils.getbitmapbytes (value);         evictioncount++;      }}

Empty cache

/** Clear the cache. */public final void Evictall () {trimtosize ( -1);//-1 'll evict 0-sized elements}

There is also a way to implement the cache interface. Clears the specified cache.

is to go through each one, each to compare

@Override  public final synchronized void clearkeyuri (String uri)  {     boolean sizechanged = false;    int urilength =  uri.length ();    for  (iterator<map.entry<string, bitmap>>  i = map.entryset (). iterator ();  i.hasnext ();)  {       Map.entry<string, bitmap> entry = i.next ();       String key = entry.getkey ();      bitmap value =  Entry.getvalue ();       int newlineindex = key.indexof (KEY_ SEPARATOR);      if  (newlineindex == urilength &&  key.substring (0, newlineindex). Equals (URI))  {         I.remove ();     &nBsp;   size -= utils.getbitmapbytes (value);         sizeChanged = true;      }    }     if  (sizeChanged)  {      trimtosize (maxSize);     }  }

At this point, LRUCache is finished.

The LruCache of Picasso

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.