Skip to content

Instantly share code, notes, and snippets.

@maltzsama
Created May 29, 2013 20:02
Show Gist options
  • Select an option

  • Save maltzsama/5673391 to your computer and use it in GitHub Desktop.

Select an option

Save maltzsama/5673391 to your computer and use it in GitHub Desktop.
- (void)trimDiskToDate:(NSDate *)trimDate
{
NSArray *keysSortedByDate = [_dates keysSortedByValueUsingSelector:@selector(compare:)];
for (NSString *key in keysSortedByDate) { // oldest files first
NSDate *accessDate = [_dates objectForKey:key];
if (!accessDate)
continue;
if ([accessDate compare:trimDate] == NSOrderedAscending) { // older than trim date
[self removeFileAndExecuteBlocksForKey:key];
} else {
break;
}
}
}
- (void)trimToAgeLimitRecursively
{
if (_ageLimit == 0.0)
return;
NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceNow:-_ageLimit];
[self trimDiskToDate:date];
__weak TMDiskCache *weakSelf = self;
dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_ageLimit * NSEC_PER_SEC));
dispatch_after(time, _queue, ^(void) {
TMDiskCache *strongSelf = weakSelf;
[strongSelf trimToAgeLimitRecursively];
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment