本地文件缓存

来源:互联网 发布:淘宝卖家开通花呗要求 编辑:程序博客网 时间:2024/06/07 12:05

#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
原创粉丝点击