Tmcache is the caching system (Github:https://github.com/tumblr/tmcache) used by Tumblr, which consists of two parts: the disk cache and the memory cache. (The maintenance is now discontinued)
Characteristics:
1. Supported by GCD
2. Thread Safety
3. If a memory warning is received or the app enters the background, the memory cache will be cleaned up. Disk caching requires manual cleanup or setting time/size Limits
4. Ability to cache any object that supports nscoding (most importantly, uiimage), accessed via key
Tmcache is composed of three classes, Tmcache,tmdiskcache and Tmmemorycache. Tmcache encapsulates Tmdiskcache and Tmmemorycache, which does not natively cache the work.
Tmcache:
Tmcache has a concurrent queue, and basically all Access objects are performed in this queue and can be asynchronously or synchronously accessed (the Async method is the principal, and the synchronization method is asynchronous to synchronous by the semaphore).
Asynchronous Fetch Object Method
-Tmcache first in the Tmmemorycache, if no more in the Tmdiskcache to take
-Each time the block is added to the queue, the self is captured and the self is determined in the block, and if Self is released elsewhere for some reason during block, the Block remains self and is prone to memory leaks. Because of the use of __weak).
-After the object is fetched in the Tmdiskcache, the object is immediately saved to the Tmmemorycache so that the next time you can get it directly from the Tmmemorycache
-(void) Objectforkey: (NSString *) key block: (Tmcacheobjectblock) block{if (!key | |!block) return; __weak Tmcache *weakself = self; Dispatch_async (_queue, ^{tmcache *strongself = weakself; if (!strongself) return; __weak Tmcache *weakself = strongself; [Strongself->_memorycache objectforkey:key block:^ (Tmmemorycache *cache, nsstring *key, id object) {Tmcache *strongself = weakself; if (!strongself) return; if (object) {[Strongself->_diskcache fileurlforkey:key block:^ (Tmdiskcache *cache, nsstring *key, id &L T Nscoding> object, Nsurl *fileurl) {//update the access time on disk}]; __weak Tmcache *weakself = strongself; Dispatch_async (Strongself->_queue, ^{tmcache *strongself = weakself; if (strongself) Block (strongself, key, object); }); } else {__weak tmcache *weakself = strongself; [Strongself->_diskcache objectforkey:key block:^ (Tmdiskcache *cache, nsstring *key, id <NSCoding> object, Nsurl *fileurl) {Tmcache *strongself = weakself; if (!strongself) return; [Strongself->_memorycache setobject:object Forkey:key Block:nil]; __weak Tmcache *weakself = strongself; Dispatch_async (Strongself->_queue, ^{tmcache *strongself = weakself; if (strongself) block (strongself, key, object); }); }]; } }]; });}
Methods for asynchronously saving an object:
-Save to disk and memory at the same time, using Dispatch_group_create ();d ispatch_group_enter (group);d Ispatch_group_leave (group) and
Dispatch_group_notify (Group,queue,block) keeps two operations synchronized (after two operations are complete, it is only a success)
-(void) SetObject: (ID <NSCoding>) object forkey: (NSString *) key block: (Tmcacheobjectblock) block{if (!key | |!obje CT) return; dispatch_group_t Group = nil; Tmmemorycacheobjectblock memblock = nil; Tmdiskcacheobjectblock Diskblock = nil; if (block) {group = Dispatch_group_create (); Dispatch_group_enter (group); Dispatch_group_enter (group); Memblock = ^ (Tmmemorycache *cache, nsstring *key, id object) {dispatch_group_leave (group); }; Diskblock = ^ (Tmdiskcache *cache, nsstring *key, id <NSCoding> object, nsurl *fileurl) {dispatch_group_l Eave (group); }; } [_memorycache setobject:object forkey:key Block:memblock]; [_diskcache setobject:object Forkey:key Block:diskblock]; if (group) {__weak Tmcache *weakself = self; Dispatch_group_notify (Group, _queue, ^{tmcache *strongself = weakself; if (strongself) Block (strongself, key, object); }); #if! OS_OBJECT_USE_OBJC dispatch_release (group); #endif}}
To synchronize a saved object:
-Use dispatch_semaphore_t to synchronize the above async methods.
-(void) SetObject: (ID <NSCoding>) object forkey: (NSString *) key{ if (!object | |!key) return; dispatch_semaphore_t semaphore = dispatch_semaphore_create (0); [Self setobject:object forkey:key block:^ (Tmcache *cache, nsstring *key, id object) { dispatch_semaphore_signal (semaphore); }]; Dispatch_semaphore_wait (semaphore, dispatch_time_forever); #if! OS_OBJECT_USE_OBJC Dispatch_release (semaphore); #endif}
Tmdiskcache
-With a serialed queue, basic all access operations are performed in this queue, and because of the serial queue, all threads are secured.
-When initializing, scan the cache folder and use dictionary to record the modified date of each cached object file (date of last visit), size, and update at a later time when the cache is stored and cached (in the future as the basis for deleting the cache)
-Take the disk file URL (after processing) as key, use Nskeyedunarchiver for access (each fetch, update the last access date)
-owned Will/didaddobjectblock,will/didremoveobjectblock,will/didremoveallobjectsblock are called before and after the relevant operation
Tmmemorycache:
-With a parallel (concurrent) queue, save objects, and delete objects, use Dispatch_barrier_async to ensure thread safety
(Dispatch_barrier_async, the function is to check if there are other blocks in the parallel queue that are being executed when the barrier block is started, and if so they are executed first and do not perform other blocks that have not yet been executed, Then execute barrier block,barrier block after execution, and then execute the other block)
-Didreceivememorywarningblock,didenterbackgroundblock with memory warning and background processing
-Store key and cache objects using dictionary
iOS Dev-cache (how is Tmcache cached?) )