IOSSwift&Objective-C

源码阅读——YYCache

2019-01-30  本文已影响16人  Bestmer

前言

缓存在iOS开发中很常用,大到网络请求的缓存,小到各种属性的缓存。比如用户发送朋友圈时,写了很多内容,因为某些操作导致APP crash,之前编辑的内容都不在了,造成非常不好的体验。之所以阅读YYCache,一是作者的编码风格非常值得学习,文档读起来和苹果官方风格一样;二是,想用这样的方式学习作者的编程思想,不断提升自己的能力。


知识储备

双向链表


OSSPinkLock(自旋锁)

OSSpinLock lock = OS_SPINLOCK_INIT;
OSSpinLockLock(&lock);
...
OSSpinLockUnlock(&lock);

pthread_mutex(互斥锁)

pthread_mutexattr_t attr;  
pthread_mutexattr_init(&attr);  
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_NORMAL);  // 定义锁的属性

pthread_mutex_t mutex;  
pthread_mutex_init(&mutex, &attr) // 创建锁

pthread_mutex_lock(&mutex); // 申请锁  
    // 临界区
pthread_mutex_unlock(&mutex); // 释放锁 

static inline


__unsafe_unretained


NSHashTable

NSHashTable *hashTable = [NSHashTable hashTableWithOptions:NSPointerFunctionsCopyIn]; 
[hashTable addObject:@"hello"]; 
[hashTable addObject:@10]; 
[hashTable addObject:@"world"]; 
[hashTable removeObject:@"world"]; 
NSLog(@"Members: %@", [hashTable allObjects]); 

NSMapTable

NSMapTable *mapTable = [NSMapTable mapTableWithKeyOptions:NSMapTableStrongMemory valueOptions:NSMapTableWeakMemory]; 
[mapTable setObject:delegate forKey:@"foo"]; 
NSLog(@"Keys: %@", [[mapTable keyEnumerator] allObjects]); 

NSPointerArray

总结


YYCache总体结构

image

YYMemoryCache


链表节点的结构图

"区块链"
// 有新数据了插入链表头部
_YYLinkedMapNode *node = CFDictionaryGetValue(_lru->_dic, (__bridge const void *)(key));
NSTimeInterval now = CACurrentMediaTime();
if (node) {
    _lru->_totalCost -= node->_cost;
    _lru->_totalCost += cost;
    node->_cost = cost;
    node->_time = now;
    node->_value = object;
    [_lru bringNodeToHead:node];
} else {
    node = [_YYLinkedMapNode new];
    node->_cost = cost;
    node->_time = now;
    node->_key = key;
    node->_value = object;
    [_lru insertNodeAtHead:node];
}
// 访问过的数据,移到头部
pthread_mutex_lock(&_lock);
_YYLinkedMapNode *node = CFDictionaryGetValue(_lru->_dic, (__bridge const void *)(key));
if (node) {
    node->_time = CACurrentMediaTime();
    [_lru bringNodeToHead:node];
}
pthread_mutex_unlock(&_lock);
// 内存紧张时把尾部的结点移除
if (_lru->_totalCost > costLimit) {
    _YYLinkedMapNode *node = [_lru removeTailNode];
    if (node) [holder addObject:node];
}
// 通过设置autoTrimInterval属性去完成每隔一定时间
// 去检查countLimit,costLimit是否达到了最大限制,
// 并做相应的操作。
- (void)_trimRecursively {
    __weak typeof(self) _self = self;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_autoTrimInterval * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
        __strong typeof(_self) self = _self;
        if (!self) return;
        [self _trimInBackground];
        //递归的调用
        [self _trimRecursively];
    });
}

- (void)_trimInBackground {
    dispatch_async(_queue, ^{
        //检查是否达到设置的最大消耗,并做相应的处理
        [self _trimToCost:self->_costLimit];
        //检查是否达到该缓存设置的最大持有对象数,并做相应的处理
        [self _trimToCount:self->_countLimit];
        //当前的时间和链表最后的节点时间的差值是否大于设定的_ageLimit值,移除大于该值得节点
        [self _trimToAge:self->_ageLimit];
    });
}

YYDiskCache

/**
 If the object's data size (in bytes) is larger than this value, then object will
 be stored as a file, otherwise the object will be stored in sqlite.
 
 0 means all objects will be stored as separated files, NSUIntegerMax means all
 objects will be stored in sqlite. 
 
 The default value is 20480 (20KB).
 */
 
YYKVStorageType type;
if (threshold == 0) {
    type = YYKVStorageTypeFile;
} else if (threshold == NSUIntegerMax) {
    type = YYKVStorageTypeSQLite;
} else {
    type = YYKVStorageTypeMixed;
}
- (instancetype)initWithPath:(NSString *)path
             inlineThreshold:(NSUInteger)threshold {
    // 判断是否可以成功初始化,省略
    ...
     
    // 先从 NSMapTable 单例中根据 path 获取 YYDiskCache 实例,如果获取到就直接返回该实例
    YYDiskCache *globalCache = _YYDiskCacheGetGlobal(path);
    if (globalCache) return globalCache;
     
    // 没有获取到则初始化一个 YYDiskCache 实例
    // 要想初始化一个 YYDiskCache 首先要初始化一个 YYKVStorage
    YYKVStorage *kv = [[YYKVStorage alloc] initWithPath:path type:type];
    if (!kv) return nil;
     
    // 根据刚才得到的 kv 和 path 入参初始化一个 YYDiskCache 实例,代码太长省略
    ...
    
    // 开启递归清理,会根据 _autoTrimInterval 对 YYDiskCache trim
    [self _trimRecursively];
    // 向 NSMapTable 单例注册新生成的 YYDiskCache 实例
    _YYDiskCacheSetGlobal(self);
     
    // App 生命周期通知相关代码,省略
    ...
    return self;
}

- (void)setObject:(id<NSCoding>)object forKey:(NSString *)key {
    if (!key) return;
    if (!object) {
        [self removeObjectForKey:key];
        return;
    }
    //runtime 取extended_data_key的value
    NSData *extendedData = [YYDiskCache getExtendedDataFromObject:object];
    NSData *value = nil;
    if (_customArchiveBlock) {
        //block返回
        value = _customArchiveBlock(object);
    } else {
        @try {
            //序列化
            value = [NSKeyedArchiver archivedDataWithRootObject:object];
        }
        @catch (NSException *exception) {
            // nothing to do...
        }
    }
    if (!value) return;
    NSString *filename = nil;
    if (_kv.type != YYKVStorageTypeSQLite) {
        //长度判断这个储存方式,value.length当大于_inlineThreshold则文件储存
        if (value.length > _inlineThreshold) {
            //将key 进行md5加密
            filename = [self _filenameForKey:key];
        }
    }
    
    Lock();
    [_kv saveItemWithKey:key value:value filename:filename extendedData:extendedData];
    Unlock();
}

YYDiskCache使用dispatch_semaphore作为锁的原因


最后

YYCache内部的设计思想非常精妙,对于性能上的优化也是一点一点积累出来的,比如作者对于锁的选择,异步释放缓存对象,使用 NSMapTable 单例管理的 YYDiskCache,甚至使用 CoreFoundation 来换取微乎其微的性能提升,这一切都是通过细节的追求积少成多换来的。

上一篇下一篇

猜你喜欢

热点阅读