-
Notifications
You must be signed in to change notification settings - Fork 77
/
SPTPersistentCacheOptions.m
193 lines (149 loc) · 6.8 KB
/
SPTPersistentCacheOptions.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* Copyright (c) 2016 Spotify AB.
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#import "SPTPersistentCacheOptions.h"
#import "SPTPersistentCacheObjectDescription.h"
#import "SPTPersistentCacheDebugUtilities.h"
#pragma mark - Constants
const NSUInteger SPTPersistentCacheDefaultExpirationTimeSec = 10 * 60;
const NSUInteger SPTPersistentCacheDefaultGCIntervalSec = 6 * 60 + 3;
const NSUInteger SPTPersistentCacheDefaultCacheSizeInBytes = 0; // unbounded
const NSUInteger SPTPersistentCacheMinimumGCIntervalLimit = 60;
const NSUInteger SPTPersistentCacheMinimumExpirationLimit = 60;
#pragma mark Helper Functions
static NSUInteger SPTGuardedPropertyValue(NSUInteger proposedValue, NSUInteger minimumValue, SEL propertySelector, SPTPersistentCacheDebugCallback debugCallback);
#pragma mark - SPTPersistentCacheOptions Implementation
@implementation SPTPersistentCacheOptions
#pragma mark Object Life Cycle
- (instancetype)init
{
self = [super init];
if (self) {
_cachePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"/com.spotify.temppersistent.image.cache"];
_cacheIdentifier = @"persistent.cache";
_useDirectorySeparation = YES;
_garbageCollectionInterval = SPTPersistentCacheDefaultGCIntervalSec;
_defaultExpirationPeriod = SPTPersistentCacheDefaultExpirationTimeSec;
_sizeConstraintBytes = SPTPersistentCacheDefaultCacheSizeInBytes;
}
return self;
}
#pragma mark Queue Management Options
- (NSString *)identifierForQueue
{
return [NSString stringWithFormat:@"%@.queue.%lu.%lu.%p",
self.cacheIdentifier,
(unsigned long)self.garbageCollectionInterval,
(unsigned long)self.defaultExpirationPeriod,
(void *)self];
}
#pragma mark Garbage Collection Options
- (void)setGarbageCollectionInterval:(NSUInteger)garbageCollectionInterval
{
if (_garbageCollectionInterval == garbageCollectionInterval) {
return;
}
_garbageCollectionInterval = SPTGuardedPropertyValue(garbageCollectionInterval,
SPTPersistentCacheMinimumGCIntervalLimit,
@selector(garbageCollectionInterval),
self.debugOutput);
}
- (void)setDefaultExpirationPeriod:(NSUInteger)defaultExpirationPeriod
{
if (_defaultExpirationPeriod == defaultExpirationPeriod) {
return;
}
_defaultExpirationPeriod = SPTGuardedPropertyValue(defaultExpirationPeriod,
SPTPersistentCacheMinimumExpirationLimit,
@selector(defaultExpirationPeriod),
self.debugOutput);
}
#pragma mark NSCopying
- (id)copyWithZone:(NSZone *)zone
{
SPTPersistentCacheOptions * const copy = [[self.class allocWithZone:zone] init];
copy.cacheIdentifier = self.cacheIdentifier;
copy.cachePath = self.cachePath;
copy.useDirectorySeparation = self.useDirectorySeparation;
copy.garbageCollectionInterval = self.garbageCollectionInterval;
copy.defaultExpirationPeriod = self.defaultExpirationPeriod;
copy.sizeConstraintBytes = self.sizeConstraintBytes;
copy.debugOutput = self.debugOutput;
return copy;
}
#pragma mark Describing an Object
- (NSString *)description
{
return SPTPersistentCacheObjectDescription(self, self.cacheIdentifier, @"cache-identifier");
}
- (NSString *)debugDescription
{
return SPTPersistentCacheObjectDescription(self,
self.cacheIdentifier, @"cache-identifier",
self.cachePath, @"cache-path",
self.identifierForQueue, @"identifier-for-queue",
@(self.useDirectorySeparation), @"use-directory-separation",
@(self.garbageCollectionInterval), @"garbage-collection-interval",
@(self.defaultExpirationPeriod), @"default-expiration-period",
@(self.sizeConstraintBytes), @"size-constraint-bytes");
}
@end
@implementation SPTPersistentCacheOptions (Deprectated)
- (instancetype)initWithCachePath:(NSString *)cachePath
identifier:(NSString *)cacheIdentifier
defaultExpirationInterval:(NSUInteger)defaultExpirationInterval
garbageCollectorInterval:(NSUInteger)garbageCollectorInterval
debug:(nullable SPTPersistentCacheDebugCallback)debugCallback
{
self = [self init];
if (self) {
_cachePath = [cachePath copy];
_cacheIdentifier = [cacheIdentifier copy];
_defaultExpirationPeriod = SPTGuardedPropertyValue(defaultExpirationInterval, SPTPersistentCacheMinimumExpirationLimit, @selector(defaultExpirationPeriod), debugCallback);
_garbageCollectionInterval = SPTGuardedPropertyValue(garbageCollectorInterval, SPTPersistentCacheMinimumGCIntervalLimit, @selector(garbageCollectionInterval), debugCallback);
_debugOutput = [debugCallback copy];
}
return self;
}
- (BOOL)folderSeparationEnabled
{
return self.useDirectorySeparation;
}
- (void)setFolderSeparationEnabled:(BOOL)folderSeparationEnabled
{
self.useDirectorySeparation = folderSeparationEnabled;
}
- (NSUInteger)gcIntervalSec
{
return self.garbageCollectionInterval;
}
- (NSUInteger)defaultExpirationPeriodSec
{
return self.defaultExpirationPeriod;
}
@end
static NSUInteger SPTGuardedPropertyValue(NSUInteger proposedValue, NSUInteger minimumValue, SEL propertySelector, SPTPersistentCacheDebugCallback debugCallback)
{
if (proposedValue >= minimumValue) {
return proposedValue;
}
SPTPersistentCacheSafeDebugCallback([NSString stringWithFormat:@"PersistentDataCache: Forcing \"%@\" to %lu seconds", NSStringFromSelector(propertySelector), (unsigned long)SPTPersistentCacheMinimumExpirationLimit], debugCallback);
return minimumValue;
}