YYDiskCacheYYCache组件中负责磁盘缓存的。

涉及知识点:

由于YYDiskCache是对YYKVStorage的封装调用

YYKVStorage

3种存储方式

typedef NS_ENUM(NSUInteger, YYKVStorageType) {
    /// 文件形式存储
    YYKVStorageTypeFile = 0,
    /// sqlite 的 blob type.
    YYKVStorageTypeSQLite = 1,
    /// 根据情况选择文件还是SQLite
    YYKVStorageTypeMixed = 2,
};

本地文件路径

/path/
  /manifest.sqlite
  /manifest.sqlite-shm
  /manifest.sqlite-wal
  /data/
       /e10adc3949ba59abbe56e057f20f883e
       /e10adc3949ba59abbe56e057f20f883e
  /trash/
        /unused_file_or_folder

YYKVStorage会根据存储方式进行数据的增删改查;

YYKVStorage 不是线程安全的

数据库

初始化

create table if not exists manifest (
key                 text,
filename            text,
size                integer,
inline_data         blob,
modification_time   integer,
last_access_time    integer,
extended_data       blob,
primary key(key)
); 
create index if not exists last_access_time_idx on manifest(last_access_time);

日志模式采用wal方式;sqlite3.7版本之后支持;

检查点

// 在合适的时机,将 `sqlite-wal` 文件合并到 `sqlite` 文件。因为`sqlite-wal`文件过大会影响性能
- (void)_dbCheckpoint {
    if (![self _dbCheck]) return;
    sqlite3_wal_checkpoint(_db, NULL);
}

YYKVStorageItem