forked from TumblrArchive/TMCache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TMCache.h
177 lines (135 loc) · 6.43 KB
/
TMCache.h
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
`TMCache` is a thread safe key/value store designed for persisting temporary objects that are expensive to
reproduce, such as downloaded data or the results of slow processing. It is comprised of two self-similar
stores, one in memory (<TMMemoryCache>) and one on disk (<TMDiskCache>).
`TMCache` itself actually does very little; its main function is providing a front end for a common use case:
a small, fast memory cache that asynchronously persists itself to a large, slow disk cache. When objects are
removed from the memory cache in response to an "apocalyptic" event they remain in the disk cache and are
repopulated in memory the next time they are accessed. `TMCache` also does the tedious work of creating a
dispatch group to wait for both caches to finish their operations without blocking each other.
The parallel caches are accessible as public properties (<memoryCache> and <diskCache>) and can be manipulated
separately if necessary. See the docs for <TMMemoryCache> and <TMDiskCache> for more details.
*/
#import <Foundation/Foundation.h>
#import "TMDiskCache.h"
#import "TMMemoryCache.h"
@class TMCache;
typedef void (^TMCacheBlock)(TMCache *cache);
typedef void (^TMCacheObjectBlock)(TMCache *cache, NSString *key, id object);
@interface TMCache : NSObject
#pragma mark -
/// @name Core
/**
The name of this cache, used to create the <diskCache> and also appearing in stack traces.
*/
@property (readonly) NSString *name;
/**
A concurrent queue on which blocks passed to the asynchronous access methods are run.
*/
@property (readonly) dispatch_queue_t queue;
/**
Synchronously retrieves the total byte count of the <diskCache> on the shared disk queue.
*/
@property (readonly) NSUInteger diskByteCount;
/**
The underlying disk cache, see <TMDiskCache> for additional configuration and trimming options.
*/
@property (readonly) TMDiskCache *diskCache;
/**
The underlying memory cache, see <TMMemoryCache> for additional configuration and trimming options.
*/
@property (readonly) TMMemoryCache *memoryCache;
#pragma mark -
/// @name Initialization
/**
A shared cache.
@result The shared singleton cache instance.
*/
+ (instancetype)sharedCache;
/**
Multiple instances with the same name are allowed and can safely access
the same data on disk thanks to the magic of seriality. Also used to create the <diskCache>.
@see name
@param name The name of the cache.
@result A new cache with the specified name.
*/
- (instancetype)initWithName:(NSString *)name;
/**
The designated initializer. Multiple instances with the same name are allowed and can safely access
the same data on disk thanks to the magic of seriality. Also used to create the <diskCache>.
@see name
@param name The name of the cache.
@param rootPath The path of the cache on disk.
@result A new cache with the specified name.
*/
- (instancetype)initWithName:(NSString *)name rootPath:(NSString *)rootPath;
#pragma mark -
/// @name Asynchronous Methods
/**
Retrieves the object for the specified key. This method returns immediately and executes the passed
block after the object is available, potentially in parallel with other blocks on the <queue>.
@param key The key associated with the requested object.
@param block A block to be executed concurrently when the object is available.
*/
- (void)objectForKey:(NSString *)key block:(TMCacheObjectBlock)block;
/**
Stores an object in the cache for the specified key. This method returns immediately and executes the
passed block after the object has been stored, potentially in parallel with other blocks on the <queue>.
@param object An object to store in the cache.
@param key A key to associate with the object. This string will be copied.
@param block A block to be executed concurrently after the object has been stored, or nil.
*/
- (void)setObject:(id <NSCoding>)object forKey:(NSString *)key block:(TMCacheObjectBlock)block;
/**
Removes the object for the specified key. This method returns immediately and executes the passed
block after the object has been removed, potentially in parallel with other blocks on the <queue>.
@param key The key associated with the object to be removed.
@param block A block to be executed concurrently after the object has been removed, or nil.
*/
- (void)removeObjectForKey:(NSString *)key block:(TMCacheObjectBlock)block;
/**
Removes all objects from the cache that have not been used since the specified date. This method returns immediately and
executes the passed block after the cache has been trimmed, potentially in parallel with other blocks on the <queue>.
@param date Objects that haven't been accessed since this date are removed from the cache.
@param block A block to be executed concurrently after the cache has been trimmed, or nil.
*/
- (void)trimToDate:(NSDate *)date block:(TMCacheBlock)block;
/**
Removes all objects from the cache.This method returns immediately and executes the passed block after the
cache has been cleared, potentially in parallel with other blocks on the <queue>.
@param block A block to be executed concurrently after the cache has been cleared, or nil.
*/
- (void)removeAllObjects:(TMCacheBlock)block;
#pragma mark -
/// @name Synchronous Methods
/**
Retrieves the object for the specified key. This method blocks the calling thread until the object is available.
@see objectForKey:block:
@param key The key associated with the object.
@result The object for the specified key.
*/
- (id)objectForKey:(NSString *)key;
/**
Stores an object in the cache for the specified key. This method blocks the calling thread until the object has been set.
@see setObject:forKey:block:
@param object An object to store in the cache.
@param key A key to associate with the object. This string will be copied.
*/
- (void)setObject:(id <NSCoding>)object forKey:(NSString *)key;
/**
Removes the object for the specified key. This method blocks the calling thread until the object
has been removed.
@param key The key associated with the object to be removed.
*/
- (void)removeObjectForKey:(NSString *)key;
/**
Removes all objects from the cache that have not been used since the specified date.
This method blocks the calling thread until the cache has been trimmed.
@param date Objects that haven't been accessed since this date are removed from the cache.
*/
- (void)trimToDate:(NSDate *)date;
/**
Removes all objects from the cache. This method blocks the calling thread until the cache has been cleared.
*/
- (void)removeAllObjects;
@end