// 写缓存
+ (void)writeLocalCacheData:(NSData *)data withKey:(NSString *)key {
// 设置存储路径
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]
stringByAppendingPathComponent:key];
// 判读缓存数据是否存在
if ([[NSFileManager defaultManager] fileExistsAtPath:cachesPath]) {
// 删除旧的缓存数据
[[NSFileManager defaultManager] removeItemAtPath:cachesPath error:nil];
}
// 存储新的缓存数据
[data writeToFile:cachesPath atomically:YES];
}
// 读缓存
+ (NSData *)readLocalCacheDataWithKey:(NSString *)key {
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]
stringByAppendingPathComponent:key];
// 判读缓存数据是否存在
if ([[NSFileManager defaultManager] fileExistsAtPath:cachesPath]) {
// 读取缓存数据
return [NSData dataWithContentsOfFile:cachesPath];
}
return nil;
}
// 删缓存
+ (void)deleteLocalCacheDataWithKey:(NSString *)key {
NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0]
stringByAppendingPathComponent:key];
// 判读缓存数据是否存在
if ([[NSFileManager defaultManager] fileExistsAtPath:cachesPath]) {
// 删除缓存数据
[[NSFileManager defaultManager] removeItemAtPath:cachesPath error:nil];
}
}