本地文件缓存

#import <Foundation/Foundation.h>


@interface NDCacheFileManger : NSObject


@property (nonatomic, strong) NSString *userPath;


+ (NDCacheFileManger *) shareCache;


//文件缓存

- (NSDictionary*) createDocumentsDirectoryFromUserId:(NSString *)userId;

- (void) cacheDataFromSource:(NSMutableArray *)sourceArr Path:(NSString *)filePath;


-(BOOL) createDirInCache:(NSString *)dirName;

-(BOOL) saveToFile:(NSString *)file inDirectory:(NSString *)directoryPath data:(NSDictionary *)dataObject;

- (NSDictionary *) readFromFile:(NSString *)file inDirectory:(NSString *)directoryPath;


@end

#import "NDCacheFileManger.h"


@interface NDCacheFileManger ()

@property (nonatomic, strong) NSString *cachePath;

@end


@implementation NDCacheFileManger

@synthesize userPath = _userPath;


+ (NDCacheFileManger *) shareCache

{

    static NDCacheFileManger *shareInstance = nil;

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        shareInstance = [[self alloc] init];

        shareInstance.cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];

    });

    return shareInstance;

}


- (void)setUserPath:(NSString *)userPath

{

    _userPath = userPath;

    [[NSUserDefaults standardUserDefaults] setObject:userPath forKey:kUserCachePath];

    [[NSUserDefaults standardUserDefaults] synchronize];

    

}

- (NSString *)userPath

{

     return [[NSUserDefaults standardUserDefaults] objectForKey:kUserCachePath];

}


//创建缓存文件夹

 -(BOOL) createDirInCache:(NSString *)dirName

 {

    NSString *imageDir =  [self.cachePath stringByAppendingPathComponent:dirName];

    BOOL isDir = NO;

    NSFileManager *fileManager = [NSFileManager defaultManager];

    BOOL existed = [fileManager fileExistsAtPath:imageDir isDirectory:&isDir];

    BOOL isCreated = NO;

    if (!(isDir == YES && existed == YES))

    {

        isCreated = [fileManager createDirectoryAtPath:imageDir withIntermediateDirectories:YES attributes:nil error:nil];

        self.userPath = imageDir;

    }

    if (existed) {

        isCreated = YES;

    }

     DLog(@"userPaht = %@",self.userPath);

     return isCreated;

}


-(BOOL) saveToFile:(NSString *)file inDirectory:(NSString *)directoryPath data:(NSDictionary *)dataObject

{

    BOOL isSaved = NO;

    BOOL isDir = NO;

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (directoryPath.length == 0) {

        directoryPath = kCustomerID;

    }

    NSString *userDirectory = [self.cachePath stringByAppendingPathComponent:directoryPath];

    BOOL existed = [fileManager fileExistsAtPath:userDirectory isDirectory:&isDir];

    if (isDir && existed) {

        NSString *filePath = [userDirectory stringByAppendingPathComponent:file];

        isSaved = [dataObject writeToFile:filePath atomically:YES];

        DLog(@"filePath = %@",filePath);

    }

    return isSaved;

}


- (NSDictionary *) readFromFile:(NSString *)file inDirectory:(NSString *)directoryPath

{

    BOOL isDir = NO;

    

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (directoryPath.length == 0) {

        directoryPath = kCustomerID;

    }

    

    NSString *userDirectory = [self.cachePath stringByAppendingPathComponent:directoryPath];

    BOOL existed = [fileManager fileExistsAtPath:userDirectory isDirectory:&isDir];

    if (isDir && existed) {

        NSString *filePath = [userDirectory stringByAppendingPathComponent:file];

        existed = [fileManager fileExistsAtPath:filePath];

        if (existed) {

            NSDictionary *array = [NSDictionary dictionaryWithContentsOfFile:filePath];

            return array;

        }else{

            return nil;

        }

    }else

    {

        return nil;

    }

}


#pragma mark - 自定义缓存文件

- (NSDictionary *)createDocumentsDirectoryFromUserId:(NSString *)userId{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];

    

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSString *fileName = [[NSString alloc]initWithFormat:@"%@.file",userId];

    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];

//    [fileManager removeItemAtPath:filePath error:nil];

    if(![fileManager fileExistsAtPath:filePath]){

        //文件不存在

        NSDictionary *dict = @{@"filePath":filePath,@"isCache":@"NO"};

        return dict;

    }else{

        NSDictionary *dict = @{@"filePath":filePath,@"isCache":@"YES"};

        return dict;

    }

    return nil;

}


- (void)cacheDataFromSource:(NSMutableArray *)sourceArr Path:(NSString *)filePath{

    NSData *data=[NSKeyedArchiver archivedDataWithRootObject:sourceArr];

    BOOL ret =  [data writeToFile:filePath atomically:YES];

    if (ret) {

        NSLog(@"缓存成功");

    }

}


@end




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值