-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCacheFile.m
More file actions
120 lines (98 loc) · 3.12 KB
/
CacheFile.m
File metadata and controls
120 lines (98 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
// CacheFile.m
// injoy
//
// Created by tao hans on 13-3-22.
// Copyright (c) 2013年 injoy365.net. All rights reserved.
//
#import "CacheFile.h"
@implementation CacheFile
// 缓存文件 设置
+(BOOL)cache_file_set:(NSString*)fileName contents:(NSString*)contents
{
NSError *err;
NSString *path = [self cache_file_path:fileName];
NSFileManager *fm=[NSFileManager defaultManager];
if ([fm fileExistsAtPath:path]) {
[fm removeItemAtPath:path error:&err];
}
return [fm createFileAtPath:path contents:[contents dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
}
// 缓存文件 设置 NSData
+(BOOL)cache_file_set_nsdata:(NSString*)fileName contents:(NSData*)contents
{
NSError *err;
NSString *path = [self cache_file_path:fileName];
NSFileManager *fm=[NSFileManager defaultManager];
if ([fm fileExistsAtPath:path]) {
[fm removeItemAtPath:path error:&err];
}
return [fm createFileAtPath:path contents:contents attributes:nil];
}
// 缓存文件 读取
+(NSString*)cache_file_get:(NSString*)fileName
{
NSString *path = [self cache_file_path:fileName];
NSFileManager *fm=[NSFileManager defaultManager];
if ([fm fileExistsAtPath:path]) {
return [[NSString alloc] initWithData:[fm contentsAtPath:path] encoding:NSUTF8StringEncoding];
}
return @"";
}
// 缓存文件 读取 NSData
+(NSData*)cache_file_get_nsdata:(NSString*)fileName
{
NSString *path = [self cache_file_path:fileName];
NSFileManager *fm=[NSFileManager defaultManager];
if ([fm fileExistsAtPath:path]) {
return [fm contentsAtPath:path];
}
return nil;
}
// 缓存文件 是否过期
// expiredSecond 过期多少秒
+(BOOL)cache_file_expired:(NSString*)fileName expiredSecond:(NSInteger)expiredSecond
{
NSString *path = [self cache_file_path:fileName];
NSFileManager *fm=[NSFileManager defaultManager];
if (![fm fileExistsAtPath:path]) {
return YES;
}
NSError *err;
NSDictionary *fileAttributes = [fm attributesOfFileSystemForPath:path error:&err];
NSDate *fileModDate = [fileAttributes objectForKey:NSFileModificationDate];
if ([[NSDate date] timeIntervalSinceDate:fileModDate] > expiredSecond) {
return YES;
}
else{
return NO;
}
return YES;
}
// 缓存文件 是否存在
+(BOOL)cache_file_exists:(NSString*)fileName
{
NSString *path = [self cache_file_path:fileName];
NSFileManager *fm=[NSFileManager defaultManager];
return [fm fileExistsAtPath:path];
}
// 缓存文件 删除
+(BOOL)cache_file_delete:(NSString*)fileName
{
NSString *path = [self cache_file_path:fileName];
NSFileManager *fm=[NSFileManager defaultManager];
if ([fm fileExistsAtPath:path]) {
NSError *err;
return [fm removeItemAtPath:path error:&err];
}
return YES;
}
// 缓存文件 路径
+(NSString*)cache_file_path:(NSString*) fileName
{
NSString* path=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
path=[path stringByAppendingString:@"/"];
path=[path stringByAppendingString:fileName];
return path;
}
@end