MBThreadsafeCache Class Reference
Inherits from | NSObject |
---|---|
Declared in | MBThreadsafeCache.h |
Overview
This class implements in-memory object cache that can be safely shared among multiple threads simultaneously.
The cache provides an accessor/mutator interface similar to NSDictionary
and
NSMutableDictionary
, and supports keyed subscripting.
Note: Classes that wish to subclass MBThreadsafeCache
should refer
to the internal MBThreadsafeCache(ForSubclassEyesOnly)
methods
declared in the header file MBThreadsafeCache+Subclassing.h
.
Other Methods
– initWithExceptionProtection:ignoreMemoryWarnings:
Initializes a new MBThreadsafeCache
instance.
- (nonnull instancetype)initWithExceptionProtection:(BOOL)protect ignoreMemoryWarnings:(BOOL)ignore
Parameters
protect |
If |
---|---|
ignore |
If |
Declared In
MBThreadsafeCache.h
– initWithExceptionProtection:
Initializes a new MBThreadsafeCache
instance.
- (nonnull instancetype)initWithExceptionProtection:(BOOL)protect
Parameters
protect |
If |
---|
Declared In
MBThreadsafeCache.h
– init
The default initializer for MBThreadsafeCache
instances.
- (nonnull instancetype)init
Discussion
Equivalent to calling initWithExceptionProtection:NO ignoreMemoryWarnings:NO
.
Declared In
MBThreadsafeCache.h
Accessing cached items
– isKeyInCache:
Determines whether a given key has a corresponding value in the cache.
- (BOOL)isKeyInCache:(nonnull id)key
Parameters
key |
The key. |
---|
Return Value
YES
if there is a value for the given key in the cache; NO
otherwise.
Declared In
MBThreadsafeCache.h
– objectForKey:
Retrieves a cached object value given its key.
- (nullable id)objectForKey:(nonnull id)key
Parameters
key |
The key whose associated value is to be retrieved. |
---|
Return Value
The value associated with key
. May be nil
.
Declared In
MBThreadsafeCache.h
Modifying the cache
– setObject:forKey:
Sets a cached object value and associates it with the given key.
- (void)setObject:(nonnull id)obj forKey:(nonnull id)key
Parameters
obj |
The new cached value. |
---|---|
key |
The key whose associated value is to be set. |
Declared In
MBThreadsafeCache.h
– removeObjectForKey:
Removes from the cache the object associated with the given key.
- (void)removeObjectForKey:(nonnull id)key
Parameters
key |
The key whose associated value is to be removed. |
---|
Declared In
MBThreadsafeCache.h
Keyed subscripting support
– objectForKeyedSubscript:
Allows accessing a cached value using the Objective-C keyed subscripting notation.
- (nullable id)objectForKeyedSubscript:(nonnull id)key
Parameters
key |
The key whose associated value is to be retrieved. |
---|
Return Value
The value associated with key
. May be nil
.
Declared In
MBThreadsafeCache.h
– setObject:forKeyedSubscript:
Allows setting a cached value using the Objective-C keyed subscripting notation.
- (void)setObject:(nonnull id)obj forKeyedSubscript:(nonnull id)key
Parameters
obj |
The new cached value. |
---|---|
key |
The key whose associated value is to be set. |
Declared In
MBThreadsafeCache.h
Memory management
– clearMemoryCache
Empties the in-memory object cache.
- (void)clearMemoryCache
Discussion
Depending on how the cache instance was initialized, the memory cache may be cleared automatically in response to memory warnings.
Declared In
MBThreadsafeCache.h
– memoryWarning
This method is called automatically when a memory warning occurs, unless the cache was explicitly initialized to ignore memory warnings.
- (void)memoryWarning
Discussion
The default implementation simply calls clearMemoryCache
; however, subclasses
may override this to perform additional cleanup when a memory warning occurs.
Declared In
MBThreadsafeCache.h
Manipulating the cache lock
– lock
Locks the cache.
- (void)lock
Discussion
Used internally by the MBThreadsafeCache
implementation and subclasses.
Note: You will generally not need to call this directly unless you want to make multiple changes to the cache atomically.
Declared In
MBThreadsafeCache.h
– unlock
Unlocks the cache.
- (void)unlock
Discussion
Used internally by the MBThreadsafeCache
implementation and subclasses.
Note: You will generally not need to call this directly unless you want to make multiple changes to the cache atomically.
Declared In
MBThreadsafeCache.h