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)ignoreParameters
protect |
If |
|---|---|
ignore |
If |
Declared In
MBThreadsafeCache.h
– initWithExceptionProtection:
Initializes a new MBThreadsafeCache instance.
- (nonnull instancetype)initWithExceptionProtection:(BOOL)protectParameters
protect |
If |
|---|
Declared In
MBThreadsafeCache.h
– init
The default initializer for MBThreadsafeCache instances.
- (nonnull instancetype)initDiscussion
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)keyParameters
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)keyParameters
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)keyParameters
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)keyParameters
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)keyParameters
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)keyParameters
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)clearMemoryCacheDiscussion
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)memoryWarningDiscussion
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)lockDiscussion
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)unlockDiscussion
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