YYCache是一个优秀的缓存框架,其文件结构如下:
YYCache的文件结构分为四个,YYCAche、YYDiskCache、YYKVStorage、YYMemoryCache
YYCAche使用以及介绍:
初始化方法:
- (nullable instancetype)initWithName:(NSString *)name;
- (nullable instancetype)initWithPath:(NSString *)path;
+ (nullable instancetype)cacheWithName:(NSString *)name;
+ (nullable instancetype)cacheWithPath:(NSString *)path;
属性:
@property (copy, readonly) NSString *name;
@property (strong, readonly) YYMemoryCache *memoryCache;
@property (strong, readonly) YYDiskCache *diskCache;
缓存操作方法:
- (BOOL)containsObjectForKey:(NSString *)key;
- (void)containsObjectForKey:(NSString *)key withBlock:(nullable void(^)(NSString *key, BOOL contains))block;
- (nullable id<NSCoding>)objectForKey:(NSString *)key;
- (void)objectForKey:(NSString *)key withBlock:(nullable void(^)(NSString *key, id<NSCoding> object))block;
- (void)setObject:(nullable id<NSCoding>)object forKey:(NSString *)key;
- (void)setObject:(nullable id<NSCoding>)object forKey:(NSString *)key withBlock:(nullable void(^)(void))block;
- (void)removeObjectForKey:(NSString *)key;
- (void)removeObjectForKey:(NSString *)key withBlock:(nullable void(^)(NSString *key))block;
- (void)removeAllObjects;
- (void)removeAllObjectsWithBlock:(void(^)(void))block;
- (void)removeAllObjectsWithProgressBlock:(nullable void(^)(int removedCount, int totalCount))progress
endBlock:(nullable void(^)(BOOL error))end;
源文件中的一些方法实现初探:
- (instancetype)initWithName:(NSString *)name {
if (name.length == 0) return nil;
NSString *cacheFolder = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
NSString *path = [cacheFolder stringByAppendingPathComponent:name];
return [self initWithPath:path];
}
- (instancetype)initWithPath:(NSString *)path {
if (path.length == 0) return nil;
YYDiskCache *diskCache = [[YYDiskCache alloc] initWithPath:path];
if (!diskCache) return nil;
NSString *name = [path lastPathComponent];
YYMemoryCache *memoryCache = [YYMemoryCache new];
memoryCache.name = name;
self = [super init];
_name = name;
_diskCache = diskCache;
_memoryCache = memoryCache;
return self;
}
初始化方法中在创建缓存时同时创建了YYDiskCache与YYMemoryCache
- (BOOL)containsObjectForKey:(NSString *)key {
return [_memoryCache containsObjectForKey:key] || [_diskCache containsObjectForKey:key];
}
判断是否具有每个缓存时对 YYDiskCache与 YYMemoryCache同时进行判断- (id<NSCoding>)objectForKey:(NSString *)key {
id<NSCoding> object = [_memoryCache objectForKey:key];
if (!object) {
object = [_diskCache objectForKey:key];
if (object) {
[_memoryCache setObject:object forKey:key];
}
}
return object;
}
读取缓存时先从YYMemoryCache中去读取数据,如果YYMemoryCache中不存在时就从YYDiskCache读取数据,并把数据存放在YYMemoryCache中
由以上方法可以确定,YYCache的操作同时是对YYDiskCache与YYMemoryCache的操作
具体使用方法如下:
1、同步缓存操作
func userYYCache() -> Void {
let cacheName:String = "TongCache"
let cache:YYCache = YYCache.init(name: cacheName)!
let cacheKey:String = "TongCacheKey"
print("开始进行同步存储数据")
cache.setObject("data" as NSCoding, forKey: cacheKey)
sleep(5)
print("同步存储数据结束")
print("开始同步判断是否含有缓存")
let isContent:Bool = cache.containsObject(forKey: cacheKey)
sleep(5)
print("判断缓存是否存在结束")
print("开始读取缓存数据")
let data:String = cache.object(forKey: cacheKey) as! String
sleep(5)
print("读取缓存数据结束")
}
执行结果如下:
开始进行同步存储数据
同步存储数据结束
开始同步判断是否含有缓存
判断缓存是否存在结束
开始读取缓存数据
读取缓存数据结束
block块进行数据操作
func userCacheForAsyn() -> Void {
print("开始进行缓存操作")
let cacheName:String = "YiCache"
let cache:YYCache = YYCache.init(name: cacheName)!
let cacheKey:String = "YiCacheKey"
print("开始进行异步存储数据")
cache.setObject("data" as NSCoding, forKey: cacheKey) {
print("====")
}
sleep(5)
print("异步存储数据结束")
print("开始同步判断是否含有缓存")
cache.containsObject(forKey: cacheKey) { (name, icContent) in
print("name:\(name), icContent:\(icContent)")
}
sleep(3)
print("判断缓存是否存在结束")
print("开始读取缓存数据")
cache.object(forKey: cacheKey) { (name, nSCoding) in
print("name:\(name), coding:\(nSCoding)")
}
sleep(7)
print("读取缓存数据结束")
print("结束缓存操作")
}
执行结果
开始进行缓存操作
开始进行同步存储数据
数据缓存完毕
同步存储数据结束
开始同步判断是否含有缓存
name:YiCacheKey, icContent:true
判断缓存是否存在结束
开始读取缓存数据
name:YiCacheKey, coding:data
读取缓存数据结束