EGOCache简介
EGOCache is a simple, thread-safe key value cache store. It has native support for NSString, UI/NSImage, and NSData, but can store anything that implements <NSCoding>. All cached items expire after the timeout, which by default, is one day.
翻译过来就是:EGOCache一个简单、线程安全的基于 key-value 的缓存框架,原生支持NSString、UI/NSImage、和NSData,也支持储存任何实现协议的类,可以设定缓存过期时间,默认是1天。
EGOCache只有一个类,EGOCache.h和EGOCache.m两个文件。用法也比较容易掌握,仔细研究一下EGOCache.h的方法,很快就可以上手。
EGOCache只提供了磁盘缓存,没有提供内存缓存。同时,也提供了清理缓存的方法:
复制代码- (void)clearCache;
EGOCache还提供了判断缓存是否存在的方法:
复制代码
- (BOOL)hasCacheForKey:(NSString* __nonnull)key;
通过Cocoapods直接加入项目
直接在你的项目的Podfile加入下面一行:
复制代码pod 'EGOCache'
然后执行:
复制代码
$ pod update
EGOCache用法
用EGOCache缓存NSString
存储:
复制代码NSString *saveString = @"把我保存起来吧";
[[EGOCache globalCache] setString:saveString forKey:[NSString stringWithFormat:@"EGOImageLoader-%lu", (unsigned long)[saveString hash]] withTimeoutInterval:24*60*60];
读取:
复制代码
NSString *getSaveString = [[EGOCache globalCache] stringForKey:[NSString stringWithFormat:@"EGOImageLoader-%lu", (unsigned long)[@"SaveString" hash]]];
是不是感觉跟NSDictionary很相似,确实,前面我们说了EGOCache是基于key-value 的缓存框架。
用EGOCache缓存UIImage
存储:
复制代码UIImage *saveImage = [UIImage imageNamed:@"iOSDevTip"];
[[EGOCache globalCache] setImage:saveImage forKey:[NSString stringWithFormat:@"EGOImageLoader-%lu", (unsigned long)[@"SaveImage" hash]] withTimeoutInterval:24*60*60];










