MemCache for iOS
The prehistory of the appearance of the MemCache class is trivial. There is a project in development that most of the time is engaged in loading small amounts of data from the network. Mostly JSON data and small images. In each controller, an NSMutableDictionary was declared in which the results of the queries were saved. But with the increase in the number of controllers, two problems arose - code duplication and loss of caching results when calling popViewController.
Under the cut, the solution to these problems.
It was decided to refactor the project, which resulted in the birth of the MemCache singleton class and two categories, one for NSString and NSObject.
The MemCache class was supposed to perform caching for a short time (two minutes were enough in this project), independently clear the memory when MemoryWarning occurred, and also when the application switched to the background.
Link to the source at the end of the article, but for now I’ll tell you about the main stages of implementation.
In the init method, we subscribe to notifications about memory warnings and loss of activity.
Do not forget to unsubscribe from notifications in dealloc.
Since the cache can be cleared at any time by request or upon the occurrence of the events described above, when recording a new element, you need to check whether there is actually where to write. For each element, when added, we launch a deferred call to the method of removing it from the cache.
For ease of use, I used categories. Now to put an object in the cache, just write something like:
And you can get the object like this:
Link to the repository - github.com/eltiren/MemCahce-IOS
PS Compiled with ARC enabled.
Under the cut, the solution to these problems.
It was decided to refactor the project, which resulted in the birth of the MemCache singleton class and two categories, one for NSString and NSObject.
The MemCache class was supposed to perform caching for a short time (two minutes were enough in this project), independently clear the memory when MemoryWarning occurred, and also when the application switched to the background.
Link to the source at the end of the article, but for now I’ll tell you about the main stages of implementation.
In the init method, we subscribe to notifications about memory warnings and loss of activity.
- (id) init
{
self = [super init];
if (self != nil)
{
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:@selector(clearCache)
name:UIApplicationDidReceiveMemoryWarningNotification
object:nil];
[center addObserver:self
selector:@selector(clearCache)
name:UIApplicationWillResignActiveNotification
object:nil];
_lifeTime = 60.0f;
}
return self;
}
Do not forget to unsubscribe from notifications in dealloc.
- (void)dealloc
{
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center removeObserver:self
name:UIApplicationDidReceiveMemoryWarningNotification
object:nil];
[center removeObserver:self
name:UIApplicationWillResignActiveNotification
object:nil];
}
Since the cache can be cleared at any time by request or upon the occurrence of the events described above, when recording a new element, you need to check whether there is actually where to write. For each element, when added, we launch a deferred call to the method of removing it from the cache.
- (void)setValue:(id)value forKey:(NSString *)key
{
if (!_cache)
{
_cache = [[NSMutableDictionary alloc] init];
}
[_cache setValue:value forKey:key];
[NSObject cancelPreviousPerformRequestsWithTarget:_cache selector:@selector(removeObjectForKey:) object:key];
[_cache performSelector:@selector(removeObjectForKey:) withObject:key afterDelay:_lifeTime];
}
For ease of use, I used categories. Now to put an object in the cache, just write something like:
[jsonValue setMemCacheValueForKey:[[[connection originalRequest] URL] absoluteString]];
And you can get the object like this:
id val = [[url absoluteString] memCacheValue];
Link to the repository - github.com/eltiren/MemCahce-IOS
PS Compiled with ARC enabled.