Cache Usage in Android

Source: Internet
Author: User

During Image Processing in Android, cache is generally used. cache mainly includes memory cache and hard disk cache. Here we mainly introduce the implementation mechanism of memory cache and hard disk cache, this is not a specific implementation.

Hard disk cache
public interface DiscCacheAware {void put(String key,File file);File get(String key);void clear();}

For a hard disk cache, the above three operations are usually required. Note: The put operation does not store a file serial number (to the hard disk), but calculates whether the current hard disk cache (the cache must be limited) is full, you need to use an algorithm to delete some images.
I usually Delete the first image to be added: the following field is defined in the Implementation class: private final map <file, long> lastusagedates = collections. synchronizedmap (New hashmap <file, long> (); takes the file as the key, the last modification time of the file as the value, and counts whether the cache capacity is full in the put method, when it is full, the first file memory cache added will be deleted.

public interface MemoryCacheAware <K,V>{boolean put(K key,V value);V get(K key);void remove(K key);Collection<K> keys();void clear();}

When I use the memory cache, I generally use the LRU algorithm. The following fields are introduced in the implementation class:

private final Map<String,Reference<Bitmap>> softMap=Collections.synchronizedMap(new HashMap<String,Reference<Bitmap>>());private final List<Bitmap> hardCache = Collections.synchronizedList(new LinkedList<Bitmap>());private final Map<String, Bitmap> lruCache = Collections.synchronizedMap(new LinkedHashMap<String, Bitmap>(10, 1.1f, true));

The first hashmap uses the file name (or some encoding of the file name) as the key, and the weak reference of the image is the value. Simply put, it is the memory cache. The second list is the strong reference of all currently stored images. Therefore, as long as a bitmap exists in the list, it must exist in softmap. If we delete a bitmap in hardcache, the next time the Garbage Collector starts, bitmap in softmap will also be recycled. Use hardcache to check whether the current cache is full. If it is full, it will be deleted from the list, the image in listmap will also be deleted. Because I use the LRU algorithm, I used the third javashashmap container. It features a very similar to LRU. It will move the accessed object to the end and use this container, it is easy to know that the image is not used for the longest time.

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.