123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- /**
- `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 "AWSTMDiskCache.h"
- #import "AWSTMMemoryCache.h"
-
- @class AWSTMCache;
-
- typedef void (^AWSTMCacheBlock)(AWSTMCache *cache);
- typedef void (^AWSTMCacheObjectBlock)(AWSTMCache *cache, NSString *key, id object);
-
- @interface AWSTMCache : 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) AWSTMDiskCache *diskCache;
-
- /**
- The underlying memory cache, see <TMMemoryCache> for additional configuration and trimming options.
- */
- @property (readonly) AWSTMMemoryCache *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:(AWSTMCacheObjectBlock)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:(AWSTMCacheObjectBlock)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:(AWSTMCacheObjectBlock)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:(AWSTMCacheBlock)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:(AWSTMCacheBlock)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
|