-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEKFileOnDiskCache.m
154 lines (128 loc) · 4.02 KB
/
EKFileOnDiskCache.m
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
// EKFilesOnDiskCache.m
// EKFilesManager
//
// Created by Evgeniy Kirpichenko on 10/31/12.
// Copyright (c) 2012 Evgeniy Kirpichenko. All rights reserved.
//
#import "EKFileOnDiskCache.h"
#import "NSString+Hash.h"
static NSString * const kDefaultSubdirectory = @"FilesCache";
static EKFileOnDiskCache *kFilesOnDiskCache = nil;
@interface EKFileOnDiskCache ()
@property (nonatomic,copy) NSString *cachePath;
@end
@implementation EKFileOnDiskCache
#pragma mark -
#pragma mark life cycle
+ (id) currentCache
{
return kFilesOnDiskCache;
}
+ (void) setCurrentCache:(id<EKFileCache>) cache
{
if (cache != kFilesOnDiskCache) {
[kFilesOnDiskCache release];
kFilesOnDiskCache = [cache retain];
}
}
- (id) initWithCachePath:(NSString *)cachePath
{
if (self = [super init]) {
[self setCachePath:cachePath];
if (![[NSFileManager defaultManager] fileExistsAtPath:cachePath]) {
[[NSFileManager defaultManager] createDirectoryAtPath:cachePath
withIntermediateDirectories:YES
attributes:nil
error:nil];
}
}
return self;
}
- (id) init
{
return [self initWithCachePath:[[self class] pathForCachesDirectory]];
}
- (void) dealloc
{
if (self == kFilesOnDiskCache) {
kFilesOnDiskCache = nil;
}
[self setCachePath:nil];
[super dealloc];
}
#pragma mark -
#pragma mark paths
+ (NSString *) pathForCachesDirectory
{
return [self pathForDirectory:NSCachesDirectory];
}
+ (NSString *)pathForDocumentsDirectory
{
return [self pathForDirectory:NSDocumentDirectory];
}
- (NSString *) filePathForKey:(NSString *)key
{
NSString *keyhash = [NSString stringWithFormat:@"%@.%@",[key md5],[key pathExtension]];
return [[self cachePath] stringByAppendingPathComponent:keyhash];
}
#pragma mark -
#pragma mark store files
- (void) cacheFileData:(NSData *) fileData forKey:(NSString *) key
{
NSString *filePath = [self filePathForKey:key];
[[NSFileManager defaultManager] createFileAtPath:filePath contents:fileData attributes:nil];
[self addSkipBackupAttributeToFileAtPath:filePath];
}
- (NSData *) cachedFileDataForKey:(NSString *) key
{
NSString *filePath = [self filePathForKey:key];
return [NSData dataWithContentsOfFile:filePath];
}
- (BOOL) hasCachedFileForKey:(NSString *) key
{
NSString *filePath = [self filePathForKey:key];
return [[NSFileManager defaultManager] fileExistsAtPath:filePath];
}
#pragma mark -
#pragma mark clean cache
- (void) cleanCache
{
NSError *error = nil;
NSString *cacheDirectory = [self cachePath];
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:cacheDirectory
error:&error];
if (error == nil && [files count] > 0) {
for (NSString *filename in files) {
NSString *filePath = [cacheDirectory stringByAppendingPathComponent:filename];
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
}
}
}
- (void) deleteFileForKey:(NSString *) key
{
[[NSFileManager defaultManager] removeItemAtPath:[self filePathForKey:key]
error:nil];
}
#pragma mark -
#pragma mark helpers
+ (NSString *)pathForDirectory:(NSSearchPathDirectory)directory
{
NSArray* caches = NSSearchPathForDirectoriesInDomains(directory, NSUserDomainMask, YES);
return [caches lastObject];
}
- (void)addSkipBackupAttributeToFileAtPath:(NSString *)filePath
{
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
NSError *error;
NSURL *fileURL = [NSURL URLWithString:filePath];
BOOL success = [fileURL setResourceValue:@(YES) forKey:NSURLIsExcludedFromBackupKey
error:&error];
if (!success)
{
NSLog(@"Error excluding %@ from backup %@", fileURL, error);
}
}
}
@end