Skip to content

Commit b1c5f8d

Browse files
committed
Merge pull request #88 from vkovtash/feature/namespaces
Namespaces implementation
2 parents 97e512a + c60370a commit b1c5f8d

3 files changed

Lines changed: 57 additions & 8 deletions

File tree

FastImageCache/FICImageCache.h

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage);
2424
*/
2525
@interface FICImageCache : NSObject
2626

27+
/**
28+
The namespace of the image cache.
29+
30+
@discussion Namespace is responsible for isolation of dirrerent image cache instances on file system level. Namespace should be unique across application.
31+
*/
32+
33+
@property (readonly, nonatomic) NSString *nameSpace;
34+
2735
///----------------------------
2836
/// @name Managing the Delegate
2937
///----------------------------
@@ -36,6 +44,25 @@ typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage);
3644
*/
3745
@property (nonatomic, weak) id <FICImageCacheDelegate> delegate;
3846

47+
///---------------------------------------
48+
/// @name Creating Image Cache instances
49+
///---------------------------------------
50+
51+
/**
52+
Returns new image cache.
53+
54+
@return A new instance of `FICImageCache`.
55+
56+
@param nameSpace The namespace that uniquely identifies current image cahce entity. If no nameSpace given, default namespace will be used.
57+
58+
@note Fast Image Cache can either be used as a singleton for convenience or can exist as multiple instances.
59+
However, all instances of `FICImageCache` will make use same dispatch queue. To separate location on disk for storing image tables namespaces are used.
60+
61+
@see [FICImageCache dispatchQueue]
62+
*/
63+
64+
- (instancetype)initWithNameSpace:(NSString *)nameSpace;
65+
3966
///---------------------------------------
4067
/// @name Accessing the Shared Image Cache
4168
///---------------------------------------
@@ -45,8 +72,7 @@ typedef void (^FICImageRequestCompletionBlock)(UIImage *sourceImage);
4572
4673
@return A shared instance of `FICImageCache`.
4774
48-
@note Fast Image Cache can either be used as a singleton for convenience or can exist as multiple instances. However, all instances of `FICImageCache` will make use of
49-
shared resources, such as the same dispatch queue and the same location on disk for storing image tables.
75+
@note Shared instance always binded to default namespace.
5076
5177
@see [FICImageCache dispatchQueue]
5278
*/

FastImageCache/FICImageCache.m

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,17 @@ + (dispatch_queue_t)dispatchQueue {
7878
return __imageCacheDispatchQueue;
7979
}
8080

81-
- (id)init {
81+
- (instancetype)init {
82+
return [self initWithNameSpace:@"FICDefaultNamespace"];
83+
}
84+
85+
- (instancetype)initWithNameSpace:(NSString *)nameSpace {
8286
self = [super init];
83-
84-
if (self != nil) {
87+
if (self) {
8588
_formats = [[NSMutableDictionary alloc] init];
8689
_imageTables = [[NSMutableDictionary alloc] init];
8790
_requests = [[NSMutableDictionary alloc] init];
91+
_nameSpace = nameSpace;
8892
}
8993
return self;
9094
}
@@ -114,6 +118,9 @@ - (void)setFormats:(NSArray *)formats {
114118
// Remove any extraneous files in the image tables directory
115119
NSFileManager *fileManager = [NSFileManager defaultManager];
116120
NSString *directoryPath = [FICImageTable directoryPath];
121+
if (self.nameSpace) {
122+
directoryPath = [directoryPath stringByAppendingPathComponent:self.nameSpace];
123+
}
117124
NSArray *fileNames = [fileManager contentsOfDirectoryAtPath:directoryPath error:nil];
118125
for (NSString *fileName in fileNames) {
119126
if ([imageTableFiles containsObject:fileName] == NO) {

FastImageCache/FICImageTable.m

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,26 @@ @implementation FICImageTable
8181

8282
- (NSString *)tableFilePath {
8383
NSString *tableFilePath = [[_imageFormat name] stringByAppendingPathExtension:FICImageTableFileExtension];
84-
tableFilePath = [[FICImageTable directoryPath] stringByAppendingPathComponent:tableFilePath];
84+
tableFilePath = [[self directoryPath] stringByAppendingPathComponent:tableFilePath];
8585

8686
return tableFilePath;
8787
}
8888

8989
- (NSString *)metadataFilePath {
9090
NSString *metadataFilePath = [[_imageFormat name] stringByAppendingPathExtension:FICImageTableMetadataFileExtension];
91-
metadataFilePath = [[FICImageTable directoryPath] stringByAppendingPathComponent:metadataFilePath];
91+
metadataFilePath = [[self directoryPath] stringByAppendingPathComponent:metadataFilePath];
9292

9393
return metadataFilePath;
9494
}
9595

96+
- (NSString *) directoryPath {
97+
NSString *directoryPath = [FICImageTable directoryPath];
98+
if (self.imageCache.nameSpace) {
99+
directoryPath = [directoryPath stringByAppendingPathComponent:self.imageCache.nameSpace];
100+
}
101+
return directoryPath;
102+
}
103+
96104
#pragma mark - Class-Level Definitions
97105

98106
+ (int)pageSize {
@@ -167,7 +175,15 @@ - (instancetype)initWithFormat:(FICImageFormat *)imageFormat imageCache:(FICImag
167175

168176
[self _loadMetadata];
169177

178+
NSString *directoryPath = [self directoryPath];
179+
170180
NSFileManager *fileManager = [[NSFileManager alloc] init];
181+
182+
BOOL isDirectory;
183+
if (self.imageCache.nameSpace && ![fileManager fileExistsAtPath:directoryPath isDirectory:&isDirectory]) {
184+
[fileManager createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:nil error:nil];
185+
}
186+
171187
if ([fileManager fileExistsAtPath:_filePath] == NO) {
172188
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
173189
[attributes setValue:[_imageFormat protectionModeString] forKeyPath:NSFileProtectionKey];
@@ -686,7 +702,7 @@ - (void)saveMetadata {
686702
}
687703

688704
- (void)_loadMetadata {
689-
NSString *metadataFilePath = [[_filePath stringByDeletingPathExtension] stringByAppendingPathExtension:FICImageTableMetadataFileExtension];
705+
NSString *metadataFilePath = [self metadataFilePath];
690706
NSData *metadataData = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:metadataFilePath] options:NSDataReadingMappedAlways error:NULL];
691707
if (metadataData != nil) {
692708
NSDictionary *metadataDictionary = (NSDictionary *)[NSPropertyListSerialization propertyListWithData:metadataData options:0 format:NULL error:NULL];

0 commit comments

Comments
 (0)