On the three-level cache strategy of pictures in Android

What is L3 cache?

Why L3 caching

The most practical significance of the three-level cache strategy is to reduce unnecessary traffic consumption and increase loading speed.

Today's app network interaction seems to be essential, and it's normal to get pictures through the network. However, every time you start the application, you need to obtain pictures from the network, or you want to browse some pictures repeatedly. Each browsing requires network acquisition, which consumes more traffic. In terms of today's traffic tariff, it will certainly easily affect the number of users.

There are also pictures loaded on the network. Sometimes the loading is very slow, which affects the user experience.

In addition, from a development perspective, the creation of bitmap consumes time and memory, which may lead to frequent GC. Using the cache strategy, the bitmap will be loaded more efficiently and the jam will be reduced, so as to reduce the reading time.

The main function of memory cache is to prevent applications from repeatedly reading picture data into memory, while hard disk cache is to prevent applications from repeatedly downloading and reading data from the network or other places.

Principle of L3 cache

The specific cache strategy can be as follows: memory as the first level cache, local as the second level cache, and network loading as the last. Among them, the memory uses lrucache, which internally holds strong references to external cache objects through LinkedHashMap; For local caching, use disklrucache. When loading pictures, first use the LRU method to search. If the specified content cannot be found, conduct local search according to the three-level cache method, and the network has not been loaded.

Image cache code implementation

It is not difficult to implement a three-level cache tool class by yourself. It can be roughly as follows:

No more details. There are many similar articles on the Internet for reference.

The core of the implementation of memory cache is to obtain the maximum memory of the app, and then put it in with lrucache < URL, bitmap > when setting. He will control the memory within a certain size according to the least used algorithm recently, and automatically recycle it when it exceeds it.

Another thing to note is that when a URL is used as a key, it will be processed with the MD5 algorithm. Finally, it will use its MD5 value as the key, which may be to avoid the influence of some special characters.

About glide's cache

In fact, it is now rare to encapsulate a three-level caching strategy. Caching strategies are added to many picture frameworks to make it easier to implement. Take glide as an example.

The use of glide is basically solved in one line of code. Like this

Of course, it's best to encapsulate it again when it is applied to the project. These are not the subject of this article. Let's go back to the cache.

Glide's memory cache

Glide turns on the memory cache by default. As long as you load a picture through glide, it will be cached in memory. As long as it has not been cleaned from memory, glide will be loaded from the memory cache next time. It greatly improves the efficiency of image loading.

Of course, if you have special requirements, you can add a line of code to turn off the memory cache opened by default.

Glide's memory cache is actually not different from what we said above. It also uses lrucache algorithm, but it also combines a weak reference mechanism to jointly complete the memory cache function.

Glide's hard disk cache

The use of glide hard disk cache is also very simple.

A diskcachestrategy () method can adjust its hard disk cache policy. There are four parameters that can be passed in:

Glide's hard disk cache is to compress and convert pictures by default and then cache them to the hard disk. This processing method will often be seen when avoiding oom.

If you need to change the hard disk cache policy, you only need to change the parameters passed in.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>