Skip to content

Commit 3a1bb8b

Browse files
committed
Merge pull request #720 from ParsePlatform/nlutsenko.commandcache.perf
Improve performance of adding commands to PFCommandCache.
2 parents a9665d6 + 695b243 commit 3a1bb8b

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

Parse/Internal/PFCommandCache.m

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -227,25 +227,34 @@ - (BFTask *)_cleanupDiskCacheWithRequiredFreeSize:(NSUInteger)requiredSize {
227227
return [BFTask taskFromExecutor:[BFExecutor defaultExecutor] withBlock:^id{
228228
NSUInteger size = requiredSize;
229229

230-
NSMutableDictionary *commandSizes = [NSMutableDictionary dictionary];
230+
NSMutableDictionary<NSString *, NSNumber *> *commandSizes = [NSMutableDictionary dictionary];
231231

232232
[[PFMultiProcessFileLockController sharedController] beginLockedContentAccessForFileAtPath:self.diskCachePath];
233-
NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:self.diskCachePath];
234-
235-
NSString *identifier = nil;
236-
while ((identifier = [enumerator nextObject])) {
237-
NSNumber *fileSize = enumerator.fileAttributes[NSFileSize];
238-
if (fileSize) {
239-
commandSizes[identifier] = fileSize;
240-
size += fileSize.unsignedIntegerValue;
233+
234+
NSDictionary *directoryAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:self.diskCachePath error:nil];
235+
if ([directoryAttributes[NSFileSize] unsignedLongLongValue] > self.diskCacheSize) {
236+
NSDirectoryEnumerator<NSURL *> *enumerator = [[NSFileManager defaultManager] enumeratorAtURL:[NSURL fileURLWithPath:self.diskCachePath]
237+
includingPropertiesForKeys:@[ NSURLFileSizeKey ]
238+
options:NSDirectoryEnumerationSkipsSubdirectoryDescendants
239+
errorHandler:nil];
240+
NSURL *fileURL = nil;
241+
while ((fileURL = [enumerator nextObject])) {
242+
NSNumber *fileSize = nil;
243+
if (![fileURL getResourceValue:&fileSize forKey:NSURLFileSizeKey error:nil]) {
244+
continue;
245+
}
246+
if (fileSize) {
247+
commandSizes[fileURL.path.lastPathComponent] = fileSize;
248+
size += fileSize.unsignedIntegerValue;
249+
}
241250
}
242251
}
243252

244253
[[PFMultiProcessFileLockController sharedController] endLockedContentAccessForFileAtPath:self.diskCachePath];
245254

246255
if (size > self.diskCacheSize) {
247256
// Get identifiers and sort them to remove oldest commands first
248-
NSArray *identifiers = [commandSizes.allKeys sortedArrayUsingSelector:@selector(compare:)];
257+
NSArray<NSString *> *identifiers = [commandSizes.allKeys sortedArrayUsingSelector:@selector(compare:)];
249258
for (NSString *identifier in identifiers) @autoreleasepool {
250259
[self _removeFileForCommandWithIdentifier:identifier];
251260
size -= [commandSizes[identifier] unsignedIntegerValue];

0 commit comments

Comments
 (0)