-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathSPTPersistentCacheGarbageCollector.m
More file actions
147 lines (119 loc) · 4.62 KB
/
SPTPersistentCacheGarbageCollector.m
File metadata and controls
147 lines (119 loc) · 4.62 KB
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
/*
* Copyright (c) 2018 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 "SPTPersistentCacheGarbageCollector.h"
#import "SPTPersistentCacheDebugUtilities.h"
#import "SPTPersistentCache+Private.h"
static BOOL SPTPersistentCacheGarbageCollectorSchedulerIsInMainQueue(void);
static const NSTimeInterval SPTPersistentCacheGarbageCollectorSchedulerTimerTolerance = 300;
@interface SPTPersistentCacheGarbageCollector ()
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, copy) SPTPersistentCacheOptions *options;
@end
@implementation SPTPersistentCacheGarbageCollector
#pragma mark - Initializer
- (instancetype)initWithCache:(SPTPersistentCache *)cache
options:(SPTPersistentCacheOptions *)options
queue:(NSOperationQueue *)queue
{
self = [super init];
if (self) {
_options = [options copy];
_cache = cache;
_queue = queue;
}
return self;
}
- (void)dealloc
{
/**
* Intentionally Left Blank
*
* Our timer should be invalidated by unscheduling this garbage collector
* on the -dealloc method of the object owning the reference.
*/
}
#pragma mark -
- (void)enqueueGarbageCollection
{
__weak __typeof(self) const weakSelf = self;
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
// We want to shadow `self` in this case.
_Pragma("clang diagnostic push");
_Pragma("clang diagnostic ignored \"-Wshadow\"");
__typeof(weakSelf) const self = weakSelf;
_Pragma("clang diagnostic pop");
SPTPersistentCache * const cache = self.cache;
[cache runRegularGC];
[cache pruneBySize];
}];
operation.queuePriority = self.options.garbageCollectionPriority;
operation.qualityOfService = self.options.garbageCollectionQualityOfService;
[self.queue addOperation:operation];
}
- (void)garbageCollectionTimerFired:(NSTimer *)timer
{
[self enqueueGarbageCollection];
}
- (void)schedule
{
if (!SPTPersistentCacheGarbageCollectorSchedulerIsInMainQueue()) {
dispatch_async(dispatch_get_main_queue(), ^{
[self schedule];
});
return;
}
SPTPersistentCacheSafeDebugCallback([NSString stringWithFormat:@"runGarbageCollector:%@", self.timer],
self.options.debugOutput);
if (self.isGarbageCollectionScheduled) {
return;
}
self.timer = [NSTimer timerWithTimeInterval:self.options.garbageCollectionInterval
target:self
selector:@selector(garbageCollectionTimerFired:)
userInfo:nil
repeats:YES];
self.timer.tolerance = SPTPersistentCacheGarbageCollectorSchedulerTimerTolerance;
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
}
- (void)unschedule
{
if (!SPTPersistentCacheGarbageCollectorSchedulerIsInMainQueue()) {
dispatch_async(dispatch_get_main_queue(), ^{
[self unschedule];
});
return;
}
SPTPersistentCacheSafeDebugCallback([NSString stringWithFormat:@"stopGarbageCollector:%@", self.timer],
self.options.debugOutput);
[self.timer invalidate];
self.timer = nil;
}
- (BOOL)isGarbageCollectionScheduled
{
return (self.timer != nil);
}
@end
static BOOL SPTPersistentCacheGarbageCollectorSchedulerIsInMainQueue(void)
{
NSString *currentQueueLabelString = [NSString stringWithUTF8String:dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL)];
NSString *mainQueueLabelString = [NSString stringWithUTF8String:dispatch_queue_get_label(dispatch_get_main_queue())];
return [currentQueueLabelString isEqualToString:mainQueueLabelString];
}