forked from soffes/SAMCache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSAMCache.h
More file actions
executable file
·232 lines (154 loc) · 5.29 KB
/
SAMCache.h
File metadata and controls
executable file
·232 lines (154 loc) · 5.29 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//
// SAMCache.h
// SAMCache
//
// Created by Sam Soffes on 10/31/11.
// Copyright (c) 2011-2014 Sam Soffes. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface SAMCache : NSObject
///-----------------
/// @name Properties
///-----------------
/**
The name of the cache.
*/
@property (nonatomic, readonly) NSString *name;
/**
The directory of the on-disk cache.
*/
@property (nonatomic, readonly) NSString *directory;
///-------------------------------
/// @name Getting the Shared Cache
///-------------------------------
/**
Shared cache suitable for all of your caching needs.
@return A shared cache.
*/
+ (SAMCache *)sharedCache;
///-------------------
/// @name Initializing
///-------------------
/**
Initialize a separate cache from the shared cache. It may be handy to make a separate cache from the shared cache in
case you need to call `removeAllObjects` or change the location.
The on-disk cache will be stored at `~/Library/Caches/com.samsoffes.samcache/NAME/`.
@param name A string to identify the cache.
@return A new cache.
*/
- (instancetype)initWithName:(NSString *)name;
/**
Initialize a separate cache from the shared cache. It may be handy to make a separate cache from the shared cache in
case you need to call `removeAllObjects` or change the location.
@param name A string to identify the cache.
@param directory A path to the on-disk cache directory. It will be created if it does not exist. If you pass `nil` it
will default to `~/Library/Caches/com.samsoffes.samcache/NAME/`.
@return A new cache.
*/
- (instancetype)initWithName:(NSString *)name directory:(NSString *)directory;
///-----------------------------
/// @name Getting a Cached Value
///-----------------------------
/**
Synchronously get an object from the cache.
@param key The key of the object.
@return The object for the given key or `nil` if it does not exist.
*/
- (id)objectForKey:(NSString *)key;
/**
Asynchronously get an object from the cache.
@param key The key of the object.
@param block A block called on an arbitrary queue with the requested object or `nil` if it does not exist.
*/
- (void)objectForKey:(NSString *)key usingBlock:(void (^)(id <NSCopying> object))block;
/**
Synchronously check if an object exists in the cache without retriving it.
@param key The key of the object.
@return A boolean specifying if the object exists or not.
*/
- (BOOL)objectExistsForKey:(NSString *)key;
///----------------------------------------
/// @name Adding and Removing Cached Values
///----------------------------------------
/**
Synchronously set an object in the cache for a given key.
@param object The object to store in the cache.
@param key The key of the object.
*/
- (void)setObject:(id <NSCoding>)object forKey:(NSString *)key;
/**
Remove an object from the cache.
@param key The key of the object.
*/
- (void)removeObjectForKey:(NSString *)key;
/**
Remove all objects from the cache.
*/
- (void)removeAllObjects;
///-------------------------------
/// @name Accessing the Disk Cache
///-------------------------------
/**
Returns the path to the object on disk associated with a given key.
@param key An object identifying the value.
@return Path to object on disk or `nil` if no object exists for the given `key`.
*/
- (NSString *)pathForKey:(NSString *)key;
///-------------------
/// @name Subscripting
///-------------------
/**
Synchronously get an object from the cache.
@param key The key of the object.
@return The object for the given key or `nil` if it does not exist.
This method behaves the same as `objectForKey:`.
*/
- (id)objectForKeyedSubscript:(NSString *)key;
/**
Synchronously set an object in the cache for a given key.
@param object The object to store in the cache.
@param key The key of the object.
This method behaves the same as `setObject:forKey:`.
*/
- (void)setObject:(id <NSCoding>)object forKeyedSubscript:(NSString *)key;
@end
#if TARGET_OS_IPHONE
#import <UIKit/UIImage.h>
@interface SAMCache (UIImageAdditions)
/**
Returns the path to the raw image on disk associated with a given key.
@param key An object identifying the value.
@return Path to object on disk or `nil` if no object exists for the given `key`.
*/
- (NSString *)imagePathForKey:(NSString *)key;
/**
Synchronously get an image from the cache.
@param key The key of the image.
@return The image for the given key or `nil` if it does not exist.
*/
- (UIImage *)imageForKey:(NSString *)key;
/**
Asynchronously get an image from the cache.
@param key The key of the image.
@param block A block called on an arbitrary queue with the requested image or `nil` if it does not exist.
*/
- (void)imageForKey:(NSString *)key usingBlock:(void (^)(UIImage *image))block;
/**
Synchronously store a PNG representation of an image in the cache for a given key.
@param image The image to store in the cache.
@param key The key of the image.
*/
- (void)setImage:(UIImage *)image forKey:(NSString *)key;
/**
Synchronously check if an image exists in the cache without retriving it.
@param key The key of the image.
@return A boolean specifying if the image exists or not.
*/
- (BOOL)imageExistsForKey:(NSString *)key;
/**
Remove an image from the cache.
@param key The key of the image.
*/
- (void)removeImageForKey:(NSString *)key;
@end
#endif