Skip to content

Commit 792f172

Browse files
committed
Expose minimumFreeDiskSpaceFraction
Defaulting to require 10% of free disk space does not make sense for some apps. For example, on a huge disk, say 64GB, there is no reason to require 6GB free space for a 30MB cache.
1 parent 4062328 commit 792f172

4 files changed

Lines changed: 39 additions & 3 deletions

File tree

Sources/SPTPersistentCacheFileManager.m

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
#import "SPTPersistentCacheDebugUtilities.h"
2323
#import "SPTPersistentCacheOptions.h"
2424

25-
static const double SPTPersistentCacheFileManagerMinFreeDiskSpace = 0.1;
26-
2725
const NSUInteger SPTPersistentCacheFileManagerSubDirNameLength = 2;
2826

2927
@implementation SPTPersistentCacheFileManager
@@ -179,7 +177,7 @@ - (SPTPersistentCacheDiskSize)optimizedDiskSizeForCacheSize:(SPTPersistentCacheD
179177
SPTPersistentCacheDiskSize totalSpace = fileSystemSize.longLongValue;
180178
SPTPersistentCacheDiskSize freeSpace = fileSystemFreeSpace.longLongValue + currentCacheSize;
181179
SPTPersistentCacheDiskSize proposedCacheSize = freeSpace - llrint(totalSpace *
182-
SPTPersistentCacheFileManagerMinFreeDiskSpace);
180+
self.options.minimumFreeDiskSpaceFraction);
183181

184182
tempCacheSize = MAX(0, proposedCacheSize);
185183

Sources/SPTPersistentCacheOptions.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
const NSUInteger SPTPersistentCacheDefaultExpirationTimeSec = 10 * 60;
2828
const NSUInteger SPTPersistentCacheDefaultGCIntervalSec = 6 * 60 + 3;
2929
const NSUInteger SPTPersistentCacheDefaultCacheSizeInBytes = 0; // unbounded
30+
const double SPTPersistentCacheDefaultMinFreeDiskSpaceFraction = 0.1; // 10% of total disk size
3031

3132
const NSUInteger SPTPersistentCacheMinimumGCIntervalLimit = 60;
3233
const NSUInteger SPTPersistentCacheMinimumExpirationLimit = 60;
@@ -55,6 +56,7 @@ - (instancetype)init
5556
_garbageCollectionInterval = SPTPersistentCacheDefaultGCIntervalSec;
5657
_defaultExpirationPeriod = SPTPersistentCacheDefaultExpirationTimeSec;
5758
_sizeConstraintBytes = SPTPersistentCacheDefaultCacheSizeInBytes;
59+
_minimumFreeDiskSpaceFraction = SPTPersistentCacheDefaultMinFreeDiskSpaceFraction;
5860
_maxConcurrentOperations = NSOperationQueueDefaultMaxConcurrentOperationCount;
5961
_writePriority = NSOperationQueuePriorityNormal;
6062
_writeQualityOfService = NSQualityOfServiceDefault;
@@ -117,6 +119,7 @@ - (id)copyWithZone:(NSZone *)zone
117119
copy.garbageCollectionInterval = self.garbageCollectionInterval;
118120
copy.defaultExpirationPeriod = self.defaultExpirationPeriod;
119121
copy.sizeConstraintBytes = self.sizeConstraintBytes;
122+
copy.minimumFreeDiskSpaceFraction = self.minimumFreeDiskSpaceFraction;
120123

121124
copy.debugOutput = self.debugOutput;
122125
copy.timingCallback = self.timingCallback;

Tests/SPTPersistentCacheFileManagerTests.m

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ - (void)setUp
6969
SPTPersistentCacheOptions *options = [SPTPersistentCacheOptions new];
7070
options.cachePath = SPTPersistentCacheFileManagerTestsCachePath;
7171
options.cacheIdentifier = @"test";
72+
options.sizeConstraintBytes = (SPTPersistentCacheDiskSize)1024 * 1024 * 1024 * 3; // 3 GiB
7273
self.options = options;
7374

7475
self.cacheFileManager = [[SPTPersistentCacheFileManagerForTests alloc] initWithOptions:self.options];
@@ -168,6 +169,33 @@ - (void)testOptimizedDiskSizeForCacheSizeSmall
168169
XCTAssertEqual(optimizedSize, (SPTPersistentCacheDiskSize)0);
169170
}
170171

172+
- (void)testMinimumFreeDiskSpaceFraction
173+
{
174+
const SPTPersistentCacheDiskSize diskSize = (SPTPersistentCacheDiskSize)1024 * 1024 * 1024 * 16; // 16GiB
175+
const SPTPersistentCacheDiskSize freeSpace = (SPTPersistentCacheDiskSize)1024 * 1024 * 1024 * 8; // 8GiB
176+
NSFileManagerMock *fileManager = [NSFileManagerMock new];
177+
fileManager.mock_attributesOfFileSystemForPaths = @{SPTPersistentCacheFileManagerTestsCachePath: @{NSFileSystemSize: @(diskSize),
178+
NSFileSystemFreeSize: @(freeSpace)}};
179+
self.cacheFileManager.test_fileManager = fileManager;
180+
181+
self.cacheFileManager.options.minimumFreeDiskSpaceFraction = 1.0;
182+
SPTPersistentCacheDiskSize optimizedSize = [self.cacheFileManager optimizedDiskSizeForCacheSize:0];
183+
XCTAssertEqual(optimizedSize, (SPTPersistentCacheDiskSize)0);
184+
185+
self.cacheFileManager.options.minimumFreeDiskSpaceFraction = 0.0;
186+
optimizedSize = [self.cacheFileManager optimizedDiskSizeForCacheSize:0];
187+
XCTAssertEqual(optimizedSize, (SPTPersistentCacheDiskSize)self.options.sizeConstraintBytes);
188+
189+
self.cacheFileManager.options.minimumFreeDiskSpaceFraction = 0.5;
190+
optimizedSize = [self.cacheFileManager optimizedDiskSizeForCacheSize:0];
191+
XCTAssertEqual(optimizedSize, (SPTPersistentCacheDiskSize)0);
192+
193+
const SPTPersistentCacheDiskSize twoGiB = (SPTPersistentCacheDiskSize)1024 * 1024 * 1024 * 2;
194+
self.cacheFileManager.options.minimumFreeDiskSpaceFraction = (freeSpace - twoGiB) / (double)diskSize;
195+
optimizedSize = [self.cacheFileManager optimizedDiskSizeForCacheSize:0];
196+
XCTAssertEqual(optimizedSize, twoGiB);
197+
}
198+
171199
- (void)testRemoveAllDataButKeysWithoutKeys
172200
{
173201
NSString *keyOne = @"AA";

include/SPTPersistentCache/SPTPersistentCacheOptions.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,13 @@ FOUNDATION_EXPORT const NSUInteger SPTPersistentCacheMinimumExpirationLimit;
178178
* @note Defaults to `0` (unbounded).
179179
*/
180180
@property (nonatomic, assign) NSUInteger sizeConstraintBytes;
181+
/**
182+
* The minimum fraction of free disk space required for caching. If there is less disk space
183+
* available than this, the cache will be purged during garbage collection until the treshold
184+
* is met. This could mean that the whole cache is evicted if the device is low on space.
185+
* @note Defaults to `0.1`, 10% of the total disk size.
186+
*/
187+
@property (nonatomic, assign) double minimumFreeDiskSpaceFraction;
181188
/**
182189
* The queue priority for garbage collection. Defaults to NSOperationQueuePriorityLow.
183190
*/

0 commit comments

Comments
 (0)