YYCache源码分析(三)
本文分析YYDiskCache
->YYKVStorage
实现过程:
YYDiskCache
对YYKVStorage
一层封装,缓存方式:数据库+文件,下面先分析主要实现类YYKVStorage
,再分析表层类YYDiskCache
.
YYKVStorage.h方法结构图
YYKVStorage.h方法解释
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
// 用YYKVStorageItem保存缓存相关参数
@interface YYKVStorageItem : NSObject
// 缓存键值
@property (nonatomic, strong) NSString *key;
// 缓存对象
@property (nonatomic, strong) NSData *value;
// 缓存文件名
@property (nullable, nonatomic, strong) NSString *filename;
// 缓存大小
@property (nonatomic) int size;
// 修改时间
@property (nonatomic) int modTime;
// 最后使用时间
@property (nonatomic) int accessTime;
// 扩展数据
@property (nullable, nonatomic, strong) NSData *extendedData;
@end
// 可以指定缓存类型
typedef NS_ENUM(NSUInteger, YYKVStorageType) {
// 文件缓存(filename != null)
YYKVStorageTypeFile = 0,
// 数据库缓存
YYKVStorageTypeSQLite = 1,
// 如果filename != null,则value用文件缓存,缓存的其他参数用数据库缓存;如果filename == null,则用数据库缓存
YYKVStorageTypeMixed = 2,
};
// 缓存操作实现
@interface YYKVStorage : NSObject
#pragma mark - Attribute
// 缓存路径
@property (nonatomic, readonly) NSString *path;
// 缓存方式
@property (nonatomic, readonly) YYKVStorageType type;
// 是否要打开错误日志
@property (nonatomic) BOOL errorLogsEnabled;
#pragma mark - Initializer
// 这两个方法不能使用,因为实例化对象时要有初始化path、type
- (instancetype)init UNAVAILABLE_ATTRIBUTE;
+ (instancetype)new UNAVAILABLE_ATTRIBUTE;
/**
* 实例化对象
*
* @param path 缓存路径
* @param type 缓存方式
*/
- (nullable instancetype)initWithPath:(NSString *)path type:(YYKVStorageType)type NS_DESIGNATED_INITIALIZER;
#pragma mark - Save Items
/**
* 添加缓存
*
* @param item 把缓存数据封装到YYKVStorageItem对象
*/
- (BOOL)saveItem:(YYKVStorageItem *)item;
/**
* 添加缓存
*
* @param key 缓存键值
* @param value 缓存对象
*/
- (BOOL)saveItemWithKey:(NSString *)key value:(NSData *)value;
/**
* 添加缓存
*
* @param key 缓存键值
* @param value 缓存对象
* @param filename 缓存文件名称
* filename != null
* 则用文件缓存value,并把`key`,`filename`,`extendedData`写入数据库
* filename == null
* 缓存方式type:YYKVStorageTypeFile 不进行缓存
* 缓存方式type:YYKVStorageTypeSQLite || YYKVStorageTypeMixed 数据库缓存
* @param extendedData 缓存拓展数据
*/
- (BOOL)saveItemWithKey:(NSString *)key
value:(NSData *)value
filename:(nullable NSString *)filename
extendedData:(nullable NSData *)extendedData;
#pragma mark - Remove Items
/**
* 删除缓存
*/
- (BOOL)removeItemForKey:(NSString *)key;
- (BOOL)removeItemForKeys:(NSArray<NSString *> *)keys;
/**
* 删除所有内存开销大于size的缓存
*/
- (BOOL)removeItemsLargerThanSize:(int)size;
/**
* 删除所有时间比time小的缓存
*/
- (BOOL)removeItemsEarlierThanTime:(int)time;
/**
* 减小缓存占的容量开销,使总缓存的容量开销值不大于maxSize(删除原则:LRU 最久未使用的缓存将先删除)
*/
- (BOOL)removeItemsToFitSize:(int)maxSize;
/**
* 减小总缓存数量,使总缓存数量不大于maxCount(删除原则:LRU 最久未使用的缓存将先删除)
*/
- (BOOL)removeItemsToFitCount:(int)maxCount;
/**
* 清空所有缓存
*/
- (BOOL)removeAllItems;
- (void)removeAllItemsWithProgressBlock:(nullable void(^)(int removedCount, int totalCount))progress
endBlock:(nullable void(^)(BOOL error))end;
#pragma mark - Get Items
/**
* 读取缓存
*/
- (nullable YYKVStorageItem *)getItemForKey:(NSString *)key;
- (nullable YYKVStorageItem *)getItemInfoForKey:(NSString *)key;
- (nullable NSData *)getItemValueForKey:(NSString *)key;
- (nullable NSArray<YYKVStorageItem *> *)getItemForKeys:(NSArray<NSString *> *)keys;
- (nullable NSArray<YYKVStorageItem *> *)getItemInfoForKeys:(NSArray<NSString *> *)keys;
- (nullable NSDictionary<NSString *, NSData *> *)getItemValueForKeys:(NSArray<NSString *> *)keys;
#pragma mark - Get Storage Status
/**
* 判断当前key是否有对应的缓存
*/
- (BOOL)itemExistsForKey:(NSString *)key;
/**
* 获取缓存总数量
*/
- (int)getItemsCount;
/**
* 获取缓存总内存开销
*/
- (int)getItemsSize;
@end
NS_ASSUME_NONNULL_END
Typically, write data to sqlite is faster than extern file, but
reading performance is dependent on data size. In my test (on iPhone 6 64G),
read data from extern file is faster than from sqlite when the data is larger
than 20KB.
- If you want to store large number of small datas (such as contacts cache),
use YYKVStorageTypeSQLite to get better performance. - If you want to store large files (such as image cache),
use YYKVStorageTypeFile to get better performance. - You can use YYKVStorageTypeMixed and choice your storage type for each item.
通常情况下,数据写入SQLite比外部文件更快;但读取性能依赖于数据大小。在我的测试中(iPhone 6 64G),当数据大小超过20KB时,从外部文件读取数据的速度比从SQLite数据。
Multiple instances with the same path will make the storage unstable.
具有相同路径的多个实例将使存储不稳定。
Save an item or update the item with 'key' if it already exists.
- If the
type
is YYKVStorageTypeFile, then the item.filename should not be empty. - If the
type
is YYKVStorageTypeSQLite, then the item.filename will be ignored. - If the
type
is YYKVStorageTypeMixed, then the item.value will be saved to file
system if the item.filename is not empty, otherwise it will be saved to sqlite.
根据key保存一个item,如果它已经存在,就更新。key-value pair 键值对;
YYKVStorageTypeFile,item.filename不能为空;YYKVStorageTypeSQLite,item.filename会被忽略;YYKVStorageTypeMixed 如果filename != null,则value用文件缓存,缓存的其他参数用数据库缓存;如果filename == null,则用数据库缓存;
YYKVStorage.m方法结构图
私有方法:
公开方法:
/*
File:
/path/
/manifest.sqlite
/manifest.sqlite-shm
/manifest.sqlite-wal
/data/
/e10adc3949ba59abbe56e057f20f883e
/e10adc3949ba59abbe56e057f20f883e
/trash/
/unused_file_or_folder
SQL:
// 建表语句
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);
*/
SQLite3 索引的简单使用
http://hustcat.github.io/
http://www.blogfshare.com/
iOS-SQLite3和FMDB使用
SQLLite (三):sqlite3_prepare_v2,sqlite3_step
YYKVStorage.m方法实现
下面分别拎出添加、删除、查找各个主要的方法来讲解