SDWebImage realization principle (b) caching policy

SDWebImage provides support for image cache, and this function is accomplished by the SDImageCache class. This class is responsible for handling memory cache and an optional disk cache. Where the disk cache write operation is asynchronous, so UI will not affect operation.

First, the memory cache

       Memory caching is carried out using NSCache target to achieve. NSCache is a collection of similar container. It stores key-value pairs, which is similar to the NSDictionary class. We usually use cache to store a temporary short-term use but create an expensive object. These objects can be reused to optimize performance, because their values ​​do not need to be recalculated. On the other hand, these objects are not critical for the program, will be discarded when memory is tight.

Second, the disk cache

       Disk cache processing is implemented using NSFileManager object. Picture storage location is located Cache folder. Further, SDImageCache defines a serial queue, asynchronously storing image.

Third, store pictures

      Placed in a cache memory, and if determines that the cache to the disk, then the disk cache to operate as a serial task into the processing queue. In iOS, it will first detect images in PNG or JPEG, and convert them to corresponding image data, and finally the data is written to disk (the file name is the MD5 digest string of key value after). Cache basic method of operation is -storeImage: recalculateFromImage: imageData: forKey: toDisk, in this method, the images stored in memory, if toDisk is YES, also saved to disk (go into a queue for asynchronous execution)

Fourth, the query Pictures

       1, first check memory cache, if found, is a direct callback;

       2, if memory does not, look for the disk. If found, it is placed in memory abouts, and callback.

Fifth, clear picture

      Pictures of disk cache cleanup operations can be divided into completely empty and clean-up section. Operation is completely empty directly to the cache folder is removed, part of the cleanup is to remove some files, according to some parameters we set, where the two main indicators: validity cached files and the maximum size of the cache space. Cache Expiration file can be set by maxCacheAge property, the default is 1 week. If the cache files for longer than the time value, it is removed. The maximum size of the cache space by maxCacheSize property to set if the total size of all cache file exceeds this size, a file was last modified in accordance with the reverse time, to each half of the recursive file to remove those premature, until the actual size of the cache is less than the maximum use of space we set.

to sum up

     The above analysis of image cache operation, of course, in addition to speak above a few operations, SDImageCache class also provides some helper methods. Such as access to the cache size, number of images in the cache to determine whether there is a key specified picture cache. Further, SDImageCache class provides an implementation of a single embodiment of the method, we can be treated as singletons.

 

 

Guess you like

Origin www.cnblogs.com/xiaonizicome/p/10929719.html