Skip to content

Commit 7c302c5

Browse files
committed
Merge pull request #63 from spotify/fix-options-class
Refactor the SPTPersistentCacheOptions class
2 parents f48cadf + f28675f commit 7c302c5

14 files changed

Lines changed: 434 additions & 165 deletions

File tree

README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,18 @@ For an example of this framework's usage, see the demo application `SPTPersisten
6363
### Creating the SPTPersistentCache
6464
It is best to use different caches for different types of data you want to store, and not just one big cache for your entire application. However, only create one `SPTPersistentCache` instance for each cache, otherwise you might encounter anomalies when the two different caches end up writing to the same file.
6565
```objc
66-
SPTPersistentCacheOptions *options = [[SPTPersistentCacheOptions alloc] initWithCachePath:cachePath
67-
identifier:@"com.spotify.demo.image.cache"
68-
defaultExpirationInterval:(60 * 60 * 24 * 30)
69-
garbageCollectorInterval:(NSUInteger)(1.5 * SPTPersistentCacheDefaultGCIntervalSec)
70-
debug:^(NSString *string) {
71-
NSLog(@"%@", string);
72-
}];
66+
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject stringByAppendingString:@"com.spotify.demo.image.cache"];
67+
68+
SPTPersistentCacheOptions *options = [SPTPersistentCacheOptions new];
69+
options.cachePath = cachePath;
70+
options.cacheIdentifier = @"com.spotify.demo.image.cache";
71+
options.defaultExpirationPeriod = 60 * 60 * 24 * 30; // 30 days
72+
options.garbageCollectionInterval = (NSUInteger)(1.5 * SPTPersistentCacheDefaultGCIntervalSec);
7373
options.sizeConstraintBytes = 1024 * 1024 * 10; // 10 MiB
74+
options.debugOutput = ^(NSString *string) {
75+
NSLog(@"%@", string);
76+
};
77+
7478
SPTPersistentCache *cache = [[SPTPersistentCache alloc] initWithOptions:options];
7579
```
7680

SPTPersistentCache.xcodeproj/project.pbxproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@
103103
696CD7841C4707E20071DD18 /* crc32iso3309.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = crc32iso3309.c; sourceTree = "<group>"; };
104104
696CD7851C4707E20071DD18 /* crc32iso3309.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = crc32iso3309.h; sourceTree = "<group>"; };
105105
696CD7861C4707E20071DD18 /* SPTPersistentCache.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPTPersistentCache.m; sourceTree = "<group>"; };
106-
696CD7871C4707E20071DD18 /* SPTPersistentCacheOptions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPTPersistentCacheOptions.m; sourceTree = "<group>"; };
107-
696CD7911C4707EA0071DD18 /* SPTPersistentCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPTPersistentCache.h; sourceTree = "<group>"; };
108-
696CD7931C4707EA0071DD18 /* SPTPersistentCacheOptions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPTPersistentCacheOptions.h; sourceTree = "<group>"; };
106+
696CD7871C4707E20071DD18 /* SPTPersistentCacheOptions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = SPTPersistentCacheOptions.m; sourceTree = "<group>"; };
107+
696CD7911C4707EA0071DD18 /* SPTPersistentCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = SPTPersistentCache.h; sourceTree = "<group>"; };
108+
696CD7931C4707EA0071DD18 /* SPTPersistentCacheOptions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = SPTPersistentCacheOptions.h; sourceTree = "<group>"; };
109109
696CD7941C4707EA0071DD18 /* SPTPersistentCacheHeader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPTPersistentCacheHeader.h; sourceTree = "<group>"; };
110110
698B70531C7538B000BDBFEA /* aad0e75ab0a6828d0a9b37a68198cc9d70d84850 */ = {isa = PBXFileReference; lastKnownFileType = file; path = aad0e75ab0a6828d0a9b37a68198cc9d70d84850; sourceTree = "<group>"; };
111111
698B70541C7538B000BDBFEA /* ab3d97d4d7b3df5417490aa726c5a49b9ee98038 */ = {isa = PBXFileReference; lastKnownFileType = file; path = ab3d97d4d7b3df5417490aa726c5a49b9ee98038; sourceTree = "<group>"; };

SPTPersistentCacheDemo/SPTPersistentCacheDemo/MasterViewController.m

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,20 @@ - (void)viewDidLoad
3939
{
4040
[super viewDidLoad];
4141
self.objects = [NSMutableArray new];
42+
NSString *cacheIdentifier = @"com.spotify.demo.image.cache";
4243
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
4344
NSUserDomainMask,
44-
YES).firstObject stringByAppendingString:@"com.spotify.demo.image.cache"];
45+
YES).firstObject stringByAppendingString:cacheIdentifier];
4546

46-
SPTPersistentCacheOptions *options = [[SPTPersistentCacheOptions alloc] initWithCachePath:cachePath
47-
identifier:@"com.spotify.demo.image.cache"
48-
defaultExpirationInterval:(60 * 60 * 24 * 30)
49-
garbageCollectorInterval:(NSUInteger)(1.5 * SPTPersistentCacheDefaultGCIntervalSec)
50-
debug:^(NSString *string) { NSLog(@"%@", string); }];
47+
SPTPersistentCacheOptions *options = [SPTPersistentCacheOptions new];
48+
options.cachePath = cachePath;
49+
options.cacheIdentifier = cacheIdentifier;
50+
options.defaultExpirationPeriod = 60 * 60 * 24 * 30;
51+
options.garbageCollectionInterval = (NSUInteger)(1.5 * SPTPersistentCacheDefaultGCIntervalSec);
5152
options.sizeConstraintBytes = 1024 * 1024 * 10; // 10 MiB
53+
options.debugOutput = ^(NSString *string) {
54+
NSLog(@"%@ %@", @(__PRETTY_FUNCTION__), string);
55+
};
5256

5357
self.cache = [[SPTPersistentCache alloc] initWithOptions:options];
5458
self.navigationItem.leftBarButtonItem = self.editButtonItem;

SPTPersistentCacheFramework/SPTPersistentCacheFramework.xcodeproj/project.pbxproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@
7878
C45526B31C77DCCC008D5570 /* SPTPersistentCacheTypeUtilities.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPTPersistentCacheTypeUtilities.m; sourceTree = "<group>"; };
7979
C4EA65041C7A547000A6091A /* SPTPersistentCacheDebugUtilities.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPTPersistentCacheDebugUtilities.h; sourceTree = "<group>"; };
8080
C4EA65051C7A547000A6091A /* SPTPersistentCacheDebugUtilities.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPTPersistentCacheDebugUtilities.m; sourceTree = "<group>"; };
81-
DD1D23791C77857900D0477A /* SPTPersistentCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPTPersistentCache.h; sourceTree = "<group>"; };
81+
DD1D23791C77857900D0477A /* SPTPersistentCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = SPTPersistentCache.h; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
8282
DD1D237A1C77857900D0477A /* SPTPersistentCacheHeader.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPTPersistentCacheHeader.h; sourceTree = "<group>"; };
83-
DD1D237B1C77857900D0477A /* SPTPersistentCacheOptions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPTPersistentCacheOptions.h; sourceTree = "<group>"; };
83+
DD1D237B1C77857900D0477A /* SPTPersistentCacheOptions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = SPTPersistentCacheOptions.h; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
8484
DD1D237C1C77857900D0477A /* SPTPersistentCacheRecord.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPTPersistentCacheRecord.h; sourceTree = "<group>"; };
8585
DD1D237D1C77857900D0477A /* SPTPersistentCacheResponse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPTPersistentCacheResponse.h; sourceTree = "<group>"; };
8686
DD1D238C1C7785A900D0477A /* NSError+SPTPersistentCacheDomainErrors.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSError+SPTPersistentCacheDomainErrors.h"; sourceTree = "<group>"; };
@@ -92,7 +92,7 @@
9292
DD1D23941C7785A900D0477A /* SPTPersistentCacheFileManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SPTPersistentCacheFileManager.h; sourceTree = "<group>"; };
9393
DD1D23951C7785A900D0477A /* SPTPersistentCacheFileManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPTPersistentCacheFileManager.m; sourceTree = "<group>"; };
9494
DD1D23961C7785A900D0477A /* SPTPersistentCacheHeader.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPTPersistentCacheHeader.m; sourceTree = "<group>"; };
95-
DD1D23971C7785A900D0477A /* SPTPersistentCacheOptions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPTPersistentCacheOptions.m; sourceTree = "<group>"; };
95+
DD1D23971C7785A900D0477A /* SPTPersistentCacheOptions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = SPTPersistentCacheOptions.m; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objc; };
9696
DD1D23981C7785A900D0477A /* SPTPersistentCacheRecord.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPTPersistentCacheRecord.m; sourceTree = "<group>"; };
9797
DD1D23991C7785A900D0477A /* SPTPersistentCacheRecord+Private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "SPTPersistentCacheRecord+Private.h"; sourceTree = "<group>"; };
9898
DD1D239A1C7785A900D0477A /* SPTPersistentCacheResponse.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPTPersistentCacheResponse.m; sourceTree = "<group>"; };

Sources/SPTPersistentCache.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ - (BOOL)isDataExpiredWithHeader:(SPTPersistentCacheRecordHeader *)header
757757
assert(header != nil);
758758
uint64_t ttl = header->ttl;
759759
uint64_t current = spt_uint64rint(self.currentDateTimeInterval);
760-
int64_t threshold = (int64_t)((ttl > 0) ? ttl : self.options.defaultExpirationPeriodSec);
760+
int64_t threshold = (int64_t)((ttl > 0) ? ttl : self.options.defaultExpirationPeriod);
761761

762762
if (ttl > SPTPersistentCacheTTLUpperBoundInSec) {
763763
[self debugOutput:@"PersistentDataCache: WARNING: TTL seems too big: %llu > %llu sec", ttl, SPTPersistentCacheTTLUpperBoundInSec];

Sources/SPTPersistentCacheFileManager.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ - (NSString *)subDirectoryPathForKey:(NSString *)key
8181
// make folder tree: xx/ zx/ xy/ yz/ etc.
8282
NSString *subDir = self.options.cachePath;
8383

84-
if (self.options.folderSeparationEnabled &&
85-
[key length] >= SPTPersistentCacheFileManagerSubDirNameLength) {
84+
if (self.options.useDirectorySeparation && key.length >= SPTPersistentCacheFileManagerSubDirNameLength) {
8685
NSString *subDirectoryName = [key substringToIndex:SPTPersistentCacheFileManagerSubDirNameLength];
8786
subDir = [self.options.cachePath stringByAppendingPathComponent:subDirectoryName];
8887
}

Sources/SPTPersistentCacheGarbageCollector.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
@interface SPTPersistentCacheGarbageCollector ()
3030
@property (nonatomic, strong) NSTimer *timer;
31-
@property (nonatomic, strong) SPTPersistentCacheOptions *options;
31+
@property (nonatomic, copy) SPTPersistentCacheOptions *options;
3232
@end
3333

3434

@@ -42,7 +42,7 @@ - (instancetype)initWithCache:(SPTPersistentCache *)cache
4242
{
4343
self = [super init];
4444
if (self) {
45-
_options = options;
45+
_options = [options copy];
4646
_cache = cache;
4747
_queue = queue;
4848
}
@@ -95,7 +95,7 @@ - (void)schedule
9595
return;
9696
}
9797

98-
self.timer = [NSTimer timerWithTimeInterval:self.options.gcIntervalSec
98+
self.timer = [NSTimer timerWithTimeInterval:self.options.garbageCollectionInterval
9999
target:self
100100
selector:@selector(enqueueGarbageCollection:)
101101
userInfo:nil

0 commit comments

Comments
 (0)