Imageloader local cache developed by Android

Imageloader is an open source library for image caching, which provides a powerful image caching mechanism, which is used by many developers. Today, I'd like to introduce the local cache of imageloader developed by Android. The specific contents are as follows:

Local cache provides two ways to modify the file name when caching a file, each of which corresponds to a Java class

1) Hashcodefilenamegenerator, which is responsible for obtaining the hashcode of the file name and converting it into a string.

2) Md5filenamegenerator, which saves the name of the source file after MD5 encryption.

Both classes inherit the filenamegenerator interface

A factory method createfilenamegenerator is provided in the defaultconfigurationfactory class, which returns a default filenamegenerator object: hashcodefilenamegenerator

realization

Firstly, the disccacheware interface is defined, which provides the following methods

Then another interface diskcache without method is defined, which simply inherits the disccacheware interface.

Basediskcache implements diskcache. This class is an abstract class that defines the following attributes of disk buffer:

1) The default cache size is 32K

2) The default compressed image format is PNG (as the first parameter of bitmap's compress method)

3) By default, the quality of the image after compression is 100, that is, the compression rate is 0, and compression is not performed (as the second parameter of compress)

It provides a set method for modifying the format and compression ratio of compressed pictures and modifying the cache size. The class also encapsulates the following three properties

Constructor

1) The constructor with only one parameter only initializes cachedir, does not use backup cache, and uses hashcodefilenamegenerator to generate the file name of the target file.

2) In addition to cachedir and hashcodefilenamegenerator, the constructor with two parameters can also initialize the backup cache

3) The constructor with three parameters requires that cachedir must be initialized and filennamegenerator must be initialized, otherwise an exception will be reported

get(String imageUri)

save(String imageUri,Bitmap bitmap)

Basedisccache has two extension classes: unlimited disccache that does not limit the cache size and limitedagedisccache that limits the cache time. Among them, unlimited disccache is very simple. It simply inherits basedisccache and does not extend basedisccache.

Limitedagedisccache this class implements the deletion of files loaded for more than a specified time in the cache: delete files from the cache when the following conditions are met: current system time - latest modification time of files > maxfileage

LimitedAgeDiscCache

This class provides two properties:

1. Maxfileage (long) sets the maximum timeout for loading. The change time is initialized by the constructor. Once initialized, it cannot be changed (set the maximum survival time of the file. When it exceeds this value, the file will be deleted)

2. loadingDates (Map<File, long>), which is an object of map type, key saves the image file to cache, and value saves the current time of calling the save method, and the data filled in loadingDates is implemented in the rememberUsage method below. The method is called in the two save methods of the class, first calling the save method of the parent class. Then call this method

The method to get data from the cache is get (string imageuri). This class overrides the basediscdache method. This method obtains the latest update time loadingdate of the image represented by imageuri from loadingdates, and then makes a difference between the current time and loadingdate. If the difference is greater than maxfileage, that is, after checking the maximum loading time, delete the file represented by the imageuri, Of course, if there is no imageuri in the map, the timeout problem will not be involved. At this time, put the image into the map. The specific implementation is as follows

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
分享
二维码
< <上一篇
下一篇>>