硬盘缓存(YYDiskCache)
采用的是文件和数据库相互配合的方式.
有一个参数inlineThreshold,默认20KB,小于它存数据库,大于它存文件.能获得效率的提高.
key:path,value:cache存储在NSMapTable里.根据path获得cache,进行一系列的set,get,remove操作
更底层的是YYKVStorage,它能直接对sqlite和文件系统进行读写.
每次内存超过限制时,select key, filename, size from manifest order by last_access_time desc limit ?1
会根据时间排序来删除最近不常用的数据.
硬盘访问的时间比较长,如果用OSSpinLockLock锁会造成CPU消耗过大,所以用的dispatch_semaphore_wait来做.
========================================================================
NSString *basePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) firstObject];
basePath = [basePath stringByAppendingPathComponent:@"FileCacheBenchmarkLarge"];
// 写入数据
//YYKVStorage能直接对sqlite和文件系统进行读写
YYKVStorage *yykvFile = [[YYKVStorage alloc] initWithPath:[basePath stringByAppendingPathComponent:@"yykvFile"] type:YYKVStorageTypeFile];
[yykvFile saveItemWithKey:keys[i] value:[NSKeyedArchiver archivedDataWithRootObject:values[i]] filename:keys[i] extendedData:nil];
========================================================================
YYKVStorage *yykvSQLite = [[YYKVStorage alloc] initWithPath:[basePath stringByAppendingPathComponent:@"yykvSQLite"] type:YYKVStorageTypeSQLite];
[yykvSQLite saveItemWithKey:keys[i] value:[NSKeyedArchiver archivedDataWithRootObject:values[i]]];
========================================================================
//YYDiskCache硬盘缓存 封装了YYKVStorage 有一个参数inlineThreshold,默认20KB,小于它存数据库,大于它存文件.能获得效率的提高.key:path,value:cache存储在NSMapTable里.根据path获得cache,进行一系列的set,get,remove操作
YYDiskCache *yy = [[YYDiskCache alloc] initWithPath:[basePath stringByAppendingPathComponent:@"yy"]];
yy.customArchiveBlock = ^(id object) {return object;};
yy.customUnarchiveBlock = ^(NSData *object) {returnobject;};
[yy setObject:values[i] forKey:keys[i]];
========================================================================
// 读数据
NSString *basePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) firstObject];
basePath = [basePath stringByAppendingPathComponent:@"FileCacheBenchmarkLarge"];
========================================================================
YYKVStorage *yykvFile = [[YYKVStorage alloc] initWithPath:[basePath stringByAppendingPathComponent:@"yykvFile"] type:YYKVStorageTypeFile];
for (int i = 0; i < count; i++) {
YYKVStorageItem *item = [yykvFile getItemForKey:keys[i]];
NSData *value = item.value;
if (!value) printf("error!");
}
========================================================================
YYKVStorage *yykvSQLite = [[YYKVStorage alloc] initWithPath:[basePath stringByAppendingPathComponent:@"yykvSQLite"] type:YYKVStorageTypeSQLite];
for (int i = 0; i < count; i++) {
YYKVStorageItem *item = [yykvSQLite getItemForKey:keys[i]];
NSData *value = item.value;
if (!value) printf("error!");
}
========================================================================
YYDiskCache *yy = [[YYDiskCache alloc] initWithPath:[basePath stringByAppendingPathComponent:@"yy"]];
yy.customArchiveBlock = ^(id object) {return object;};
yy.customUnarchiveBlock = ^(NSData *object) {return object;};
for (int i = 0; i < count; i++) {
NSData *value = (id)[yy objectForKey:keys[i]];
if (!value) printf("error!");
}