iOS各种机制和底层实现原理iOS开发笔记将来跳槽用

iOS利用沙盒机制做数据缓存

2016-06-30  本文已影响1560人  iOS俱哥

沙盒简述:
每一个APP都有一个存储空间,就是沙盒。
APP之间不能相互通信。
沙盒根目录结构:Documents、Library、temp。

ZJDataCache.h文件代码
#import <Foundation/Foundation.h>
/*
缓存:临时保存数据的一种形式
通过接口请求数据,将数据在本地保存一份(将数据保存到本地文件).当在一段时间内重新访问当前界面时,不必从接口请求数据,而是将本地的数据拿来使用.这个过程叫做对数据的缓存.

 作用:为用户节省大量的流量,提高效率,提高用户体验.
 缺点:不能拿到实时的数据
 */
@interface ZJDataCache : NSObject
//创建单例对象
+(ZJDataCache *)sharedCache;
//存数据
-(BOOL)saveDataWithData:(NSData *)data andStringName:(NSString *)name;
//取数据
-(NSData *)getDataWithStringName:(NSString *)name;
@end

ZJDataCache.m文件
#import "ZJDataCache.h"
#import "NSString+Hashing.h"
@interface ZJDataCache ()
@property (nonatomic,assign) NSTimeInterval invaliteTime;//有效时间
@end
@implementation ZJDataCache
//创建单例对象
static ZJDataCache cache = nil;
/
*
* @author zhengju, 16-06-30 17:06:27
*
* @brief 单例创建缓存对象
*
* @return 单例对象
/
+(ZJDataCache )sharedCache{
@synchronized(self){
if (!cache) {
cache = [[[ZJDataCache class] alloc]init];
}
}
return cache;
}
+(instancetype)allocWithZone:(struct _NSZone )zone{
@synchronized(self){
if (!cache) {
cache = [super allocWithZone:zone];
}
}
return cache;
}
/

* @author zhengju, 16-06-30 17:06:49
*
* @brief 初始化的时候返设置过期时间
*
* @return 对象
/
-(id)init{
if (self = [super init]) {
_invaliteTime = 60
60;//以秒为单位
}
return self;
}
/
*
* @author zhengju, 16-06-30 17:06:43
*
* @brief 存数据
*
* @param data 缓存Data数据
* @param name 段路径,一般用请求数据的短URL来传值
*
* @return 是否保存数据成功
*/
-(BOOL)saveDataWithData:(NSData *)data andStringName:(NSString *)name{
//获取路径
NSString *path = [NSString stringWithFormat:@"%@/Documents/Cache/",NSHomeDirectory()];//沙盒路径
NSFileManager manager = [NSFileManager defaultManager];
BOOL isSuc = [manager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
if (!isSuc) {
NSLog(@"创建失败");
return NO;
}
//先将文件名字进行加密处理
//MD5:一种加密方式,通过MD5加密会得到一个16进制的32位的文件(固定长度)
name = [name MD5Hash];
//获取的完整路径
NSString allPath = [NSString stringWithFormat:@"%@%@",path,name];
BOOL isWriteSuc = [data writeToFile:allPath atomically:YES];//写文件
return isWriteSuc;
}
/

* @author zhengju, 16-06-30 17:06:44
*
* @brief 根据路径查找Data数据
*
* @param name 段路径
*
* @return 返回缓存的在段路径下的Data数据
*/
-(NSData *)getDataWithStringName:(NSString *)name{
NSString *tempName = [name MD5Hash];
NSString *path = [NSString stringWithFormat:@"%@/Documents/Cache/%@",NSHomeDirectory(),tempName];
// NSLog(@"--path----->>%@",path);
//判断文件是否存在
NSFileManager *manage = [NSFileManager defaultManager];
if (![manage fileExistsAtPath:path]) {
NSLog(@"文件不存在");
return nil;
}
//判断数据是否过期
NSTimeInterval invalitTime = [[NSDate date] timeIntervalSinceDate:[self getLastModefityDateWithFile:path]];
if (invalitTime >= _invaliteTime) {
return nil;
}
//取数据
NSData *data = [NSData dataWithContentsOfFile:path];
return data;
}
//获取最后修改文件的日期
-(NSDate *)getLastModefityDateWithFile:(NSString *)path{
NSFileManager *manager = [NSFileManager defaultManager];
NSDictionary dic = [manager attributesOfItemAtPath:path error:nil];
/

NSFileCreationDate = "2015-08-10 03:38:15 +0000";
NSFileExtensionHidden = 0;
NSFileGroupOwnerAccountID = 20;
NSFileGroupOwnerAccountName = staff;
NSFileModificationDate = "2015-08-10 03:38:15 +0000";
NSFileOwnerAccountID = 501;
NSFilePosixPermissions = 420;
NSFileReferenceCount = 1;
NSFileSize = 17090;
NSFileSystemFileNumber = 9204533;
NSFileSystemNumber = 16777217;
NSFileType = NSFileTypeRegular;
*/
return dic[NSFileModificationDate];
}
@end

本文参考: iOS沙盒目录结构解析

欢迎转载,转载请注明出处。
github下载地址:https://github.com/zhengju/DataCache

上一篇 下一篇

猜你喜欢

热点阅读