Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the three caching strategies of Android?

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces what are the three Android caching strategies, the content is very detailed, interested friends can refer to, I hope it can be helpful to you.

Here to write some practical cache technology, to be verified, please want to use children's shoes to try ha!

At present, many commercial applications will involve the problem of reading picture data from the network. In order to save user traffic, applications generally cache pictures. Picture cache is generally divided into in-memory cache and out-of-memory cache. Memory cache uses the caching mechanism of java. After the program exits completely, the memory space of the cache may be occupied by other applications and lost. The external cache is generally placed in the program-specific access space or SD card, and the resources stored in the SD card are public resources, and other programs can also access them, and there is no standard mechanism for users to force the cache to be cleared. To sum up, this article uses to place the cached pictures in the unique space of the program, which can not be accessed by other applications, and the user can clear the cache in the "clear data" option in the application management.

This paper provides three caching strategies: (1) LRU algorithm, fixed number of cached pictures (max_num), when the number of pictures exceeds max_num, delete the least recently used pictures in the cache. (2) FTU algorithm, fixed cache time limit for each image, calculated from the last use, and deleted after the time limit is exceeded. (3) FMU algorithm, fixed a certain size of storage space in the memory, and delete the maximum size of the image in the cache after exceeding the fixed space. When using, you only need to pass the URL of the picture to the body of the method.

How to use it:

1. Import jar

two。 Get servic

3. Submit the url to the program to determine whether to download it or not.

Public class ImagecachetacticsdemoActivity extends Activity {public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.item); / * FMU*/p_w_picpathCacheManager = ImageCacheManager.getImageCacheService (this,ImageCacheManager.MODE_FIXED_MEMORY_USED, "memory"); p_w_picpathCacheManager.setMax_Memory (1024 * 1024); / * FTU*/// p_w_picpathCacheManager = ImageCacheManager.getImageCacheService (this,// ImageCacheManager.MODE_FIXED_TIMED_USED, "time") / / p_w_picpathCacheManager.setDelay_millisecond (3 * 60 * 1000); / * LRU*/// p_w_picpathCacheManager = ImageCacheManager.getImageCacheService (this,// ImageCacheManager.MODE_LEAST_RECENTLY_USED, "num"); / / p_w_picpathCacheManager.setMax_num (5); / / p_w_picpathCacheManager = ImageCacheManager.getImageCacheService (this,// ImageCacheManager.MODE_NO_CACHE_USED, "nocache"); mImageView = (ImageView) findViewById (R.id.p_w_picpathView) New DownloadTask () .execute ("http://www.touxiang99.com/uploads/allimg/110417/1_110417112640_2.jpg");}private class DownloadTask extends AsyncTask {@ Overrideprotected Bitmap doInString..." Params) {try {return p_w_picpathCacheManager.downlaodImage (new URL (params [0]));} catch (IOException e) {e.printStackTrace ();} return null;} @ Overrideprotected void onPostExecute (Bitmap result) {mImageView.setImageBitmap (result); super.onPostExecute (result);} @ Overrideprotected void onPreExecute () {mImageView.setImageResource (R.drawable.ic_launcher); super.onPreExecute ();}} private ImageView mImageView;private ImageCacheManager paired picpathCacheManager;}

This is the end of what are the three caching strategies of Android. I hope the above content can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report