本文分析YYDiskCache
->YYKVStorage
实现过程:
YYDiskCache
对YYKVStorage
一层封装,缓存方式:数据库+文件,下面先分析主要实现类YYKVStorage
,再分析表层类YYDiskCache
.
YYKVStorage.h方法结构图
YYKVStorage.h方法解释
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
#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 |
YYKVStorage.m方法结构图
私有方法:
公开方法:
YYKVStorage.m方法实现
下面分别拎出添加、删除、查找各个主要的方法来讲解
1.添加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
// 添加缓存 - |