Interpretation of YYCache source code (1)

YYCache

  YYCache is an IOS caching tool designed by ibireme in 2015.
  Generally speaking, a cache includes memory cache and disk cache. In the design of disk cache, YYCache uses database sqlite mapping and file system mapping for storage, and a doubly linked list structure is designed in memory to store data nodes. Both memory and disk data elimination strategies are implemented using the lru algorithm. At the same time, in terms of storage, YYCache respectively limits the number of objects, storage capacity, object life cycle, memory warning, and the state when the program exits to the background. At the same time, the lock structure and semaphore are adopted to ensure thread synchronization and safety.

insert image description here

Figure 1: YYCache class diagram

  As shown in Figure 1, the structure of YYCache mainly includes three parts, namely the disk cache part, the memory cache part, and the YYCache main class. The disk cache part includes two classes, YYDiskCache and YYKVStorage. Access and delete operations are implemented in YYKVStorage, including the access and deletion of database sqlite and files. The file size threshold determines whether to store in a file or in a database. By default 20k. YYDiskCache mainly implements access operation methods, regular checks (according to three variables: number of objects, object storage capacity, object storage time, and remaining free disk space) and eliminates the earliest accessed value in the cache (LRU algorithm). YYMemoryCache implements the cache logic in memory, mainly maintains a linked list, implements the method of moving any node to the head node, inserting at the head node, and deleting at the tail node, and implements the LRU algorithm. YYCache is associated with YYDiskCache and YYMemoryCache, and adds a value to the memory and disk at the same time, and the query data will first query the variables in the memory. At the same time, YYCache will regularly check the memory and disk, and perform the elimination operation according to the judgment.

YYDiskCache

YYKVStorage

YYMemoryCache

YYCache

To be continued Weiyang's Study Review the Past and Learn the New

Guess you like

Origin blog.csdn.net/Ambrosedream/article/details/119211083