IOS data cache and YYCache implementation analysis, ios data cache yycache

Source: Internet
Author: User

IOS data cache and YYCache implementation analysis, ios data cache yycache
1. What is cache?

Cache means cache.

The cache on the computer is a high-speed cache, which is defined in the course of computer composition. It exists between the master memory and the CPU, and is mainly used to solve the problem that the speed of CPU processing data is far greater than the speed of reading the master memory data.

Cache is also available on mobile phones. The main function is to save some temporary files generated by software to avoid repeated requests to the server for the same data every time, which wastes user traffic and affects the APP response speed.

 

2. cache implementation

There are two methods for cell phone cache: memory cache and hard disk cache.

Each time the client requests data, it first checks whether the memory cache has data. If no data exists, it checks the hard disk and still does not request the server.

After the request is successful, store the data in the memory and hard disk.

It seems easy, but some strategies need to be considered for implementation:

1. As with the previous problems encountered when learning cache, the memory is limited. What algorithm is used to clear the memory?

2. There are two hard disk cache Methods: one is to store files and the other is to store databases, how can we achieve high efficiency?

 

3. Various cache Libraries

Currently there are many cache libraries, and the official NSCache

Common open-source cache libraries SDWebImage and FastImageCache

Common closed-source cache library NSURLCache and Facebook FBDiskCache

Here, the author of YYCache reviews various cache libraries and the Implementation ideas of the above cache libraries are worth learning:

Http://blog.ibireme.com/2015/10/26/yycache

4. Implementation of YYCache

The YYCache code is clear and annotated in detail, so it is worth learning and thinking.

1. Memory Cache (YYMemoryCache)

The storage unit is _ yy1_mapnode. Besides the key and value, it also stores the address _ prev, _ next of its front and back nodes.

The entire implementation is based on the _ yy1_map, which is a two-way linked list. In addition to the dictionary _ dic, it also stores the header and tail nodes. the function is simple: Insert new data into the head of the linked list, move the accessed data nodes to the header, and remove the tail nodes when the memory is insufficient. in this way, the elimination algorithm is implemented.

Because the memory access speed is very fast and the lock takes less time, the OSSpinLockLock with the fastest speed is used.

2. Hard disk cache (YYDiskCache)

Files and databases work together.

There is a parameter inlineThreshold. The default value is 20 KB, which is smaller than the value of the parameter in the database and greater than the value in the database. This improves the efficiency.

Key: path, value: cache is stored in NSMapTable. cache is obtained based on path and a series of set, get, and remove operations are performed.

The underlying layer is YYKVStorage, which can directly read and write sqlite and the file system.

When the memory limit is exceeded, select key, filename, size from manifest order by last_access_time desc limit? 1

Data that is not commonly used recently is deleted in chronological order.

Hard Disk access takes a long time. If you use the OSSpinLockLock, the CPU usage will be excessively high. Therefore, use dispatch_semaphore_wait.

 

I have carefully studied the entire source code. The general idea is to understand it. I am very fond of some tips for handling the details, but I still feel that I have a problem with understanding it in some places, if you are interested, study and continue learning and communication.

Related Article

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.