Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of age limit trimming #224

Merged
merged 6 commits into from
Jun 8, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- [performance] Reduce locking churn in cleanup methods. [#212](https://github.com/pinterest/PINCache/pull/212)
- [fix] Don't set file protection unless requested. [#220](https://github.com/pinterest/PINCache/pull/220)
- [new] Add ability to set an object level TTL: [#209](https://github.com/pinterest/PINCache/pull/209)
- [performance] Improve performance of age limit trimming: [#224](https://github.com/pinterest/PINCache/pull/224)

## 3.0.1 -- Beta 6
- [fix] Add some sane limits to the disk cache: [#201]https://github.com/pinterest/PINCache/pull/201
Expand Down
23 changes: 18 additions & 5 deletions Source/PINDiskCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -785,13 +785,24 @@ - (void)trimToAgeLimitRecursively
return;

NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceNow:-ageLimit];
[self trimDiskToDate:date];
[self trimToDateAsync:date completion:nil];

dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(ageLimit * NSEC_PER_SEC));
dispatch_after(time, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
[self.operationQueue scheduleOperation:^{
[self trimToAgeLimitRecursively];
} withPriority:PINOperationQueuePriorityLow];
// Ensure that ageLimit is the same as when we were scheduled, otherwise, we've been
// rescheduled (another dispatch_after was issued) and should cancel.
BOOL shouldReschedule = YES;
[self lock];
if (ageLimit != self->_ageLimit) {
shouldReschedule = NO;
}
[self unlock];

if (shouldReschedule) {
[self.operationQueue scheduleOperation:^{
[self trimToAgeLimitRecursively];
} withPriority:PINOperationQueuePriorityLow];
}
});
}

Expand Down Expand Up @@ -1506,7 +1517,9 @@ - (void)setAgeLimit:(NSTimeInterval)ageLimit
self->_ageLimit = ageLimit;
[self unlock];

[self trimToAgeLimitRecursively];
[self.operationQueue scheduleOperation:^{
[self trimToAgeLimitRecursively];
} withPriority:PINOperationQueuePriorityLow];
} withPriority:PINOperationQueuePriorityHigh];
}

Expand Down
17 changes: 14 additions & 3 deletions Source/PINMemoryCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,20 @@ - (void)trimToAgeLimitRecursively

dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(ageLimit * NSEC_PER_SEC));
dispatch_after(time, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
[self.operationQueue scheduleOperation:^{
[self trimToAgeLimitRecursively];
} withPriority:PINOperationQueuePriorityHigh];
// Ensure that ageLimit is the same as when we were scheduled, otherwise, we've been
// rescheduled (another dispatch_after was issued) and should cancel.
BOOL shouldReschedule = YES;
[self lock];
if (ageLimit != self->_ageLimit) {
shouldReschedule = NO;
}
[self unlock];

if (shouldReschedule) {
[self.operationQueue scheduleOperation:^{
[self trimToAgeLimitRecursively];
} withPriority:PINOperationQueuePriorityHigh];
}
});
}

Expand Down