TMMemoryCache.m 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. #import "TMMemoryCache.h"
  2. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0
  3. #import <UIKit/UIKit.h>
  4. #endif
  5. NSString * const TMMemoryCachePrefix = @"com.tumblr.TMMemoryCache";
  6. @interface TMMemoryCache ()
  7. #if OS_OBJECT_USE_OBJC
  8. @property (strong, nonatomic) dispatch_queue_t queue;
  9. #else
  10. @property (assign, nonatomic) dispatch_queue_t queue;
  11. #endif
  12. @property (strong, nonatomic) NSMutableDictionary *dictionary;
  13. @property (strong, nonatomic) NSMutableDictionary *dates;
  14. @property (strong, nonatomic) NSMutableDictionary *costs;
  15. @end
  16. @implementation TMMemoryCache
  17. @synthesize ageLimit = _ageLimit;
  18. @synthesize costLimit = _costLimit;
  19. @synthesize totalCost = _totalCost;
  20. @synthesize willAddObjectBlock = _willAddObjectBlock;
  21. @synthesize willRemoveObjectBlock = _willRemoveObjectBlock;
  22. @synthesize willRemoveAllObjectsBlock = _willRemoveAllObjectsBlock;
  23. @synthesize didAddObjectBlock = _didAddObjectBlock;
  24. @synthesize didRemoveObjectBlock = _didRemoveObjectBlock;
  25. @synthesize didRemoveAllObjectsBlock = _didRemoveAllObjectsBlock;
  26. @synthesize didReceiveMemoryWarningBlock = _didReceiveMemoryWarningBlock;
  27. @synthesize didEnterBackgroundBlock = _didEnterBackgroundBlock;
  28. #pragma mark - Initialization -
  29. - (void)dealloc
  30. {
  31. [[NSNotificationCenter defaultCenter] removeObserver:self];
  32. #if !OS_OBJECT_USE_OBJC
  33. dispatch_release(_queue);
  34. _queue = nil;
  35. #endif
  36. }
  37. - (id)init
  38. {
  39. if (self = [super init]) {
  40. NSString *queueName = [[NSString alloc] initWithFormat:@"%@.%p", TMMemoryCachePrefix, self];
  41. _queue = dispatch_queue_create([queueName UTF8String], DISPATCH_QUEUE_CONCURRENT);
  42. _dictionary = [[NSMutableDictionary alloc] init];
  43. _dates = [[NSMutableDictionary alloc] init];
  44. _costs = [[NSMutableDictionary alloc] init];
  45. _willAddObjectBlock = nil;
  46. _willRemoveObjectBlock = nil;
  47. _willRemoveAllObjectsBlock = nil;
  48. _didAddObjectBlock = nil;
  49. _didRemoveObjectBlock = nil;
  50. _didRemoveAllObjectsBlock = nil;
  51. _didReceiveMemoryWarningBlock = nil;
  52. _didEnterBackgroundBlock = nil;
  53. _ageLimit = 0.0;
  54. _costLimit = 0;
  55. _totalCost = 0;
  56. _removeAllObjectsOnMemoryWarning = YES;
  57. _removeAllObjectsOnEnteringBackground = YES;
  58. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0
  59. [[NSNotificationCenter defaultCenter] addObserver:self
  60. selector:@selector(handleMemoryWarning)
  61. name:UIApplicationDidReceiveMemoryWarningNotification
  62. object:nil];
  63. [[NSNotificationCenter defaultCenter] addObserver:self
  64. selector:@selector(handleApplicationBackgrounding)
  65. name:UIApplicationDidEnterBackgroundNotification
  66. object:nil];
  67. #endif
  68. }
  69. return self;
  70. }
  71. + (instancetype)sharedCache
  72. {
  73. static id cache;
  74. static dispatch_once_t predicate;
  75. dispatch_once(&predicate, ^{
  76. cache = [[self alloc] init];
  77. });
  78. return cache;
  79. }
  80. #pragma mark - Private Methods -
  81. - (void)handleMemoryWarning
  82. {
  83. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0
  84. if (self.removeAllObjectsOnMemoryWarning)
  85. [self removeAllObjects:nil];
  86. __weak TMMemoryCache *weakSelf = self;
  87. dispatch_async(_queue, ^{
  88. TMMemoryCache *strongSelf = weakSelf;
  89. if (!strongSelf)
  90. return;
  91. if (strongSelf->_didReceiveMemoryWarningBlock)
  92. strongSelf->_didReceiveMemoryWarningBlock(strongSelf);
  93. });
  94. #endif
  95. }
  96. - (void)handleApplicationBackgrounding
  97. {
  98. #if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_4_0
  99. if (self.removeAllObjectsOnEnteringBackground)
  100. [self removeAllObjects:nil];
  101. __weak TMMemoryCache *weakSelf = self;
  102. dispatch_async(_queue, ^{
  103. TMMemoryCache *strongSelf = weakSelf;
  104. if (!strongSelf)
  105. return;
  106. if (strongSelf->_didEnterBackgroundBlock)
  107. strongSelf->_didEnterBackgroundBlock(strongSelf);
  108. });
  109. #endif
  110. }
  111. - (void)removeObjectAndExecuteBlocksForKey:(NSString *)key
  112. {
  113. id object = [_dictionary objectForKey:key];
  114. NSNumber *cost = [_costs objectForKey:key];
  115. if (_willRemoveObjectBlock)
  116. _willRemoveObjectBlock(self, key, object);
  117. if (cost)
  118. _totalCost -= [cost unsignedIntegerValue];
  119. [_dictionary removeObjectForKey:key];
  120. [_dates removeObjectForKey:key];
  121. [_costs removeObjectForKey:key];
  122. if (_didRemoveObjectBlock)
  123. _didRemoveObjectBlock(self, key, nil);
  124. }
  125. - (void)trimMemoryToDate:(NSDate *)trimDate
  126. {
  127. NSArray *keysSortedByDate = [_dates keysSortedByValueUsingSelector:@selector(compare:)];
  128. for (NSString *key in keysSortedByDate) { // oldest objects first
  129. NSDate *accessDate = [_dates objectForKey:key];
  130. if (!accessDate)
  131. continue;
  132. if ([accessDate compare:trimDate] == NSOrderedAscending) { // older than trim date
  133. [self removeObjectAndExecuteBlocksForKey:key];
  134. } else {
  135. break;
  136. }
  137. }
  138. }
  139. - (void)trimToCostLimit:(NSUInteger)limit
  140. {
  141. if (_totalCost <= limit)
  142. return;
  143. NSArray *keysSortedByCost = [_costs keysSortedByValueUsingSelector:@selector(compare:)];
  144. for (NSString *key in [keysSortedByCost reverseObjectEnumerator]) { // costliest objects first
  145. [self removeObjectAndExecuteBlocksForKey:key];
  146. if (_totalCost <= limit)
  147. break;
  148. }
  149. }
  150. - (void)trimToCostLimitByDate:(NSUInteger)limit
  151. {
  152. if (_totalCost <= limit)
  153. return;
  154. NSArray *keysSortedByDate = [_dates keysSortedByValueUsingSelector:@selector(compare:)];
  155. for (NSString *key in keysSortedByDate) { // oldest objects first
  156. [self removeObjectAndExecuteBlocksForKey:key];
  157. if (_totalCost <= limit)
  158. break;
  159. }
  160. }
  161. - (void)trimToAgeLimitRecursively
  162. {
  163. if (_ageLimit == 0.0)
  164. return;
  165. NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceNow:-_ageLimit];
  166. [self trimMemoryToDate:date];
  167. __weak TMMemoryCache *weakSelf = self;
  168. dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(_ageLimit * NSEC_PER_SEC));
  169. dispatch_after(time, _queue, ^(void){
  170. TMMemoryCache *strongSelf = weakSelf;
  171. if (!strongSelf)
  172. return;
  173. __weak TMMemoryCache *weakSelf = strongSelf;
  174. dispatch_barrier_async(strongSelf->_queue, ^{
  175. TMMemoryCache *strongSelf = weakSelf;
  176. [strongSelf trimToAgeLimitRecursively];
  177. });
  178. });
  179. }
  180. #pragma mark - Public Asynchronous Methods -
  181. - (void)objectForKey:(NSString *)key block:(TMMemoryCacheObjectBlock)block
  182. {
  183. NSDate *now = [[NSDate alloc] init];
  184. if (!key || !block)
  185. return;
  186. __weak TMMemoryCache *weakSelf = self;
  187. dispatch_async(_queue, ^{
  188. TMMemoryCache *strongSelf = weakSelf;
  189. if (!strongSelf)
  190. return;
  191. id object = [strongSelf->_dictionary objectForKey:key];
  192. if (object) {
  193. __weak TMMemoryCache *weakSelf = strongSelf;
  194. dispatch_barrier_async(strongSelf->_queue, ^{
  195. TMMemoryCache *strongSelf = weakSelf;
  196. if (strongSelf)
  197. [strongSelf->_dates setObject:now forKey:key];
  198. });
  199. }
  200. block(strongSelf, key, object);
  201. });
  202. }
  203. - (void)setObject:(id)object forKey:(NSString *)key block:(TMMemoryCacheObjectBlock)block
  204. {
  205. [self setObject:object forKey:key withCost:0 block:block];
  206. }
  207. - (void)setObject:(id)object forKey:(NSString *)key withCost:(NSUInteger)cost block:(TMMemoryCacheObjectBlock)block
  208. {
  209. NSDate *now = [[NSDate alloc] init];
  210. if (!key || !object)
  211. return;
  212. __weak TMMemoryCache *weakSelf = self;
  213. dispatch_barrier_async(_queue, ^{
  214. TMMemoryCache *strongSelf = weakSelf;
  215. if (!strongSelf)
  216. return;
  217. if (strongSelf->_willAddObjectBlock)
  218. strongSelf->_willAddObjectBlock(strongSelf, key, object);
  219. [strongSelf->_dictionary setObject:object forKey:key];
  220. [strongSelf->_dates setObject:now forKey:key];
  221. [strongSelf->_costs setObject:@(cost) forKey:key];
  222. _totalCost += cost;
  223. if (strongSelf->_didAddObjectBlock)
  224. strongSelf->_didAddObjectBlock(strongSelf, key, object);
  225. if (strongSelf->_costLimit > 0)
  226. [strongSelf trimToCostByDate:strongSelf->_costLimit block:nil];
  227. if (block) {
  228. __weak TMMemoryCache *weakSelf = strongSelf;
  229. dispatch_async(strongSelf->_queue, ^{
  230. TMMemoryCache *strongSelf = weakSelf;
  231. if (strongSelf)
  232. block(strongSelf, key, object);
  233. });
  234. }
  235. });
  236. }
  237. - (void)removeObjectForKey:(NSString *)key block:(TMMemoryCacheObjectBlock)block
  238. {
  239. if (!key)
  240. return;
  241. __weak TMMemoryCache *weakSelf = self;
  242. dispatch_barrier_async(_queue, ^{
  243. TMMemoryCache *strongSelf = weakSelf;
  244. if (!strongSelf)
  245. return;
  246. [strongSelf removeObjectAndExecuteBlocksForKey:key];
  247. if (block) {
  248. __weak TMMemoryCache *weakSelf = strongSelf;
  249. dispatch_async(strongSelf->_queue, ^{
  250. TMMemoryCache *strongSelf = weakSelf;
  251. if (strongSelf)
  252. block(strongSelf, key, nil);
  253. });
  254. }
  255. });
  256. }
  257. - (void)trimToDate:(NSDate *)trimDate block:(TMMemoryCacheBlock)block
  258. {
  259. if (!trimDate)
  260. return;
  261. if ([trimDate isEqualToDate:[NSDate distantPast]]) {
  262. [self removeAllObjects:block];
  263. return;
  264. }
  265. __weak TMMemoryCache *weakSelf = self;
  266. dispatch_barrier_async(_queue, ^{
  267. TMMemoryCache *strongSelf = weakSelf;
  268. if (!strongSelf)
  269. return;
  270. [strongSelf trimMemoryToDate:trimDate];
  271. if (block) {
  272. __weak TMMemoryCache *weakSelf = strongSelf;
  273. dispatch_async(strongSelf->_queue, ^{
  274. TMMemoryCache *strongSelf = weakSelf;
  275. if (strongSelf)
  276. block(strongSelf);
  277. });
  278. }
  279. });
  280. }
  281. - (void)trimToCost:(NSUInteger)cost block:(TMMemoryCacheBlock)block
  282. {
  283. __weak TMMemoryCache *weakSelf = self;
  284. dispatch_barrier_async(_queue, ^{
  285. TMMemoryCache *strongSelf = weakSelf;
  286. if (!strongSelf)
  287. return;
  288. [strongSelf trimToCostLimit:cost];
  289. if (block) {
  290. __weak TMMemoryCache *weakSelf = strongSelf;
  291. dispatch_async(strongSelf->_queue, ^{
  292. TMMemoryCache *strongSelf = weakSelf;
  293. if (strongSelf)
  294. block(strongSelf);
  295. });
  296. }
  297. });
  298. }
  299. - (void)trimToCostByDate:(NSUInteger)cost block:(TMMemoryCacheBlock)block
  300. {
  301. __weak TMMemoryCache *weakSelf = self;
  302. dispatch_barrier_async(_queue, ^{
  303. TMMemoryCache *strongSelf = weakSelf;
  304. if (!strongSelf)
  305. return;
  306. [strongSelf trimToCostLimitByDate:cost];
  307. if (block) {
  308. __weak TMMemoryCache *weakSelf = strongSelf;
  309. dispatch_async(strongSelf->_queue, ^{
  310. TMMemoryCache *strongSelf = weakSelf;
  311. if (strongSelf)
  312. block(strongSelf);
  313. });
  314. }
  315. });
  316. }
  317. - (void)removeAllObjects:(TMMemoryCacheBlock)block
  318. {
  319. __weak TMMemoryCache *weakSelf = self;
  320. dispatch_barrier_async(_queue, ^{
  321. TMMemoryCache *strongSelf = weakSelf;
  322. if (!strongSelf)
  323. return;
  324. if (strongSelf->_willRemoveAllObjectsBlock)
  325. strongSelf->_willRemoveAllObjectsBlock(strongSelf);
  326. [strongSelf->_dictionary removeAllObjects];
  327. [strongSelf->_dates removeAllObjects];
  328. [strongSelf->_costs removeAllObjects];
  329. strongSelf->_totalCost = 0;
  330. if (strongSelf->_didRemoveAllObjectsBlock)
  331. strongSelf->_didRemoveAllObjectsBlock(strongSelf);
  332. if (block) {
  333. __weak TMMemoryCache *weakSelf = strongSelf;
  334. dispatch_async(strongSelf->_queue, ^{
  335. TMMemoryCache *strongSelf = weakSelf;
  336. if (strongSelf)
  337. block(strongSelf);
  338. });
  339. }
  340. });
  341. }
  342. - (void)enumerateObjectsWithBlock:(TMMemoryCacheObjectBlock)block completionBlock:(TMMemoryCacheBlock)completionBlock
  343. {
  344. if (!block)
  345. return;
  346. __weak TMMemoryCache *weakSelf = self;
  347. dispatch_barrier_async(_queue, ^{
  348. TMMemoryCache *strongSelf = weakSelf;
  349. if (!strongSelf)
  350. return;
  351. NSArray *keysSortedByDate = [strongSelf->_dates keysSortedByValueUsingSelector:@selector(compare:)];
  352. for (NSString *key in keysSortedByDate) {
  353. block(strongSelf, key, [strongSelf->_dictionary objectForKey:key]);
  354. }
  355. if (completionBlock) {
  356. __weak TMMemoryCache *weakSelf = strongSelf;
  357. dispatch_async(strongSelf->_queue, ^{
  358. TMMemoryCache *strongSelf = weakSelf;
  359. if (strongSelf)
  360. completionBlock(strongSelf);
  361. });
  362. }
  363. });
  364. }
  365. #pragma mark - Public Synchronous Methods -
  366. - (id)objectForKey:(NSString *)key
  367. {
  368. if (!key)
  369. return nil;
  370. __block id objectForKey = nil;
  371. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  372. [self objectForKey:key block:^(TMMemoryCache *cache, NSString *key, id object) {
  373. objectForKey = object;
  374. dispatch_semaphore_signal(semaphore);
  375. }];
  376. dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
  377. #if !OS_OBJECT_USE_OBJC
  378. dispatch_release(semaphore);
  379. #endif
  380. return objectForKey;
  381. }
  382. - (void)setObject:(id)object forKey:(NSString *)key
  383. {
  384. [self setObject:object forKey:key withCost:0];
  385. }
  386. - (void)setObject:(id)object forKey:(NSString *)key withCost:(NSUInteger)cost
  387. {
  388. if (!object || !key)
  389. return;
  390. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  391. [self setObject:object forKey:key withCost:cost block:^(TMMemoryCache *cache, NSString *key, id object) {
  392. dispatch_semaphore_signal(semaphore);
  393. }];
  394. dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
  395. #if !OS_OBJECT_USE_OBJC
  396. dispatch_release(semaphore);
  397. #endif
  398. }
  399. - (void)removeObjectForKey:(NSString *)key
  400. {
  401. if (!key)
  402. return;
  403. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  404. [self removeObjectForKey:key block:^(TMMemoryCache *cache, NSString *key, id object) {
  405. dispatch_semaphore_signal(semaphore);
  406. }];
  407. dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
  408. #if !OS_OBJECT_USE_OBJC
  409. dispatch_release(semaphore);
  410. #endif
  411. }
  412. - (void)trimToDate:(NSDate *)date
  413. {
  414. if (!date)
  415. return;
  416. if ([date isEqualToDate:[NSDate distantPast]]) {
  417. [self removeAllObjects];
  418. return;
  419. }
  420. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  421. [self trimToDate:date block:^(TMMemoryCache *cache) {
  422. dispatch_semaphore_signal(semaphore);
  423. }];
  424. dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
  425. #if !OS_OBJECT_USE_OBJC
  426. dispatch_release(semaphore);
  427. #endif
  428. }
  429. - (void)trimToCost:(NSUInteger)cost
  430. {
  431. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  432. [self trimToCost:cost block:^(TMMemoryCache *cache) {
  433. dispatch_semaphore_signal(semaphore);
  434. }];
  435. dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
  436. #if !OS_OBJECT_USE_OBJC
  437. dispatch_release(semaphore);
  438. #endif
  439. }
  440. - (void)trimToCostByDate:(NSUInteger)cost
  441. {
  442. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  443. [self trimToCostByDate:cost block:^(TMMemoryCache *cache) {
  444. dispatch_semaphore_signal(semaphore);
  445. }];
  446. dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
  447. #if !OS_OBJECT_USE_OBJC
  448. dispatch_release(semaphore);
  449. #endif
  450. }
  451. - (void)removeAllObjects
  452. {
  453. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  454. [self removeAllObjects:^(TMMemoryCache *cache) {
  455. dispatch_semaphore_signal(semaphore);
  456. }];
  457. dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
  458. #if !OS_OBJECT_USE_OBJC
  459. dispatch_release(semaphore);
  460. #endif
  461. }
  462. - (void)enumerateObjectsWithBlock:(TMMemoryCacheObjectBlock)block
  463. {
  464. if (!block)
  465. return;
  466. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  467. [self enumerateObjectsWithBlock:block completionBlock:^(TMMemoryCache *cache) {
  468. dispatch_semaphore_signal(semaphore);
  469. }];
  470. dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
  471. #if !OS_OBJECT_USE_OBJC
  472. dispatch_release(semaphore);
  473. #endif
  474. }
  475. #pragma mark - Public Thread Safe Accessors -
  476. - (TMMemoryCacheObjectBlock)willAddObjectBlock
  477. {
  478. __block TMMemoryCacheObjectBlock block = nil;
  479. dispatch_sync(_queue, ^{
  480. block = self->_willAddObjectBlock;
  481. });
  482. return block;
  483. }
  484. - (void)setWillAddObjectBlock:(TMMemoryCacheObjectBlock)block
  485. {
  486. __weak TMMemoryCache *weakSelf = self;
  487. dispatch_barrier_async(_queue, ^{
  488. TMMemoryCache *strongSelf = weakSelf;
  489. if (!strongSelf)
  490. return;
  491. strongSelf->_willAddObjectBlock = [block copy];
  492. });
  493. }
  494. - (TMMemoryCacheObjectBlock)willRemoveObjectBlock
  495. {
  496. __block TMMemoryCacheObjectBlock block = nil;
  497. dispatch_sync(_queue, ^{
  498. block = _willRemoveObjectBlock;
  499. });
  500. return block;
  501. }
  502. - (void)setWillRemoveObjectBlock:(TMMemoryCacheObjectBlock)block
  503. {
  504. __weak TMMemoryCache *weakSelf = self;
  505. dispatch_barrier_async(_queue, ^{
  506. TMMemoryCache *strongSelf = weakSelf;
  507. if (!strongSelf)
  508. return;
  509. strongSelf->_willRemoveObjectBlock = [block copy];
  510. });
  511. }
  512. - (TMMemoryCacheBlock)willRemoveAllObjectsBlock
  513. {
  514. __block TMMemoryCacheBlock block = nil;
  515. dispatch_sync(_queue, ^{
  516. block = _willRemoveAllObjectsBlock;
  517. });
  518. return block;
  519. }
  520. - (void)setWillRemoveAllObjectsBlock:(TMMemoryCacheBlock)block
  521. {
  522. __weak TMMemoryCache *weakSelf = self;
  523. dispatch_barrier_async(_queue, ^{
  524. TMMemoryCache *strongSelf = weakSelf;
  525. if (!strongSelf)
  526. return;
  527. strongSelf->_willRemoveAllObjectsBlock = [block copy];
  528. });
  529. }
  530. - (TMMemoryCacheObjectBlock)didAddObjectBlock
  531. {
  532. __block TMMemoryCacheObjectBlock block = nil;
  533. dispatch_sync(_queue, ^{
  534. block = _didAddObjectBlock;
  535. });
  536. return block;
  537. }
  538. - (void)setDidAddObjectBlock:(TMMemoryCacheObjectBlock)block
  539. {
  540. __weak TMMemoryCache *weakSelf = self;
  541. dispatch_barrier_async(_queue, ^{
  542. TMMemoryCache *strongSelf = weakSelf;
  543. if (!strongSelf)
  544. return;
  545. strongSelf->_didAddObjectBlock = [block copy];
  546. });
  547. }
  548. - (TMMemoryCacheObjectBlock)didRemoveObjectBlock
  549. {
  550. __block TMMemoryCacheObjectBlock block = nil;
  551. dispatch_sync(_queue, ^{
  552. block = _didRemoveObjectBlock;
  553. });
  554. return block;
  555. }
  556. - (void)setDidRemoveObjectBlock:(TMMemoryCacheObjectBlock)block
  557. {
  558. __weak TMMemoryCache *weakSelf = self;
  559. dispatch_barrier_async(_queue, ^{
  560. TMMemoryCache *strongSelf = weakSelf;
  561. if (!strongSelf)
  562. return;
  563. strongSelf->_didRemoveObjectBlock = [block copy];
  564. });
  565. }
  566. - (TMMemoryCacheBlock)didRemoveAllObjectsBlock
  567. {
  568. __block TMMemoryCacheBlock block = nil;
  569. dispatch_sync(_queue, ^{
  570. block = _didRemoveAllObjectsBlock;
  571. });
  572. return block;
  573. }
  574. - (void)setDidRemoveAllObjectsBlock:(TMMemoryCacheBlock)block
  575. {
  576. __weak TMMemoryCache *weakSelf = self;
  577. dispatch_barrier_async(_queue, ^{
  578. TMMemoryCache *strongSelf = weakSelf;
  579. if (!strongSelf)
  580. return;
  581. strongSelf->_didRemoveAllObjectsBlock = [block copy];
  582. });
  583. }
  584. - (TMMemoryCacheBlock)didReceiveMemoryWarningBlock
  585. {
  586. __block TMMemoryCacheBlock block = nil;
  587. dispatch_sync(_queue, ^{
  588. block = _didReceiveMemoryWarningBlock;
  589. });
  590. return block;
  591. }
  592. - (void)setDidReceiveMemoryWarningBlock:(TMMemoryCacheBlock)block
  593. {
  594. __weak TMMemoryCache *weakSelf = self;
  595. dispatch_barrier_async(_queue, ^{
  596. TMMemoryCache *strongSelf = weakSelf;
  597. if (!strongSelf)
  598. return;
  599. strongSelf->_didReceiveMemoryWarningBlock = [block copy];
  600. });
  601. }
  602. - (TMMemoryCacheBlock)didEnterBackgroundBlock
  603. {
  604. __block TMMemoryCacheBlock block = nil;
  605. dispatch_sync(_queue, ^{
  606. block = _didEnterBackgroundBlock;
  607. });
  608. return block;
  609. }
  610. - (void)setDidEnterBackgroundBlock:(TMMemoryCacheBlock)block
  611. {
  612. __weak TMMemoryCache *weakSelf = self;
  613. dispatch_barrier_async(_queue, ^{
  614. TMMemoryCache *strongSelf = weakSelf;
  615. if (!strongSelf)
  616. return;
  617. strongSelf->_didEnterBackgroundBlock = [block copy];
  618. });
  619. }
  620. - (NSTimeInterval)ageLimit
  621. {
  622. __block NSTimeInterval ageLimit = 0.0;
  623. dispatch_sync(_queue, ^{
  624. ageLimit = _ageLimit;
  625. });
  626. return ageLimit;
  627. }
  628. - (void)setAgeLimit:(NSTimeInterval)ageLimit
  629. {
  630. __weak TMMemoryCache *weakSelf = self;
  631. dispatch_barrier_async(_queue, ^{
  632. TMMemoryCache *strongSelf = weakSelf;
  633. if (!strongSelf)
  634. return;
  635. strongSelf->_ageLimit = ageLimit;
  636. [strongSelf trimToAgeLimitRecursively];
  637. });
  638. }
  639. - (NSUInteger)costLimit
  640. {
  641. __block NSUInteger costLimit = 0;
  642. dispatch_sync(_queue, ^{
  643. costLimit = _costLimit;
  644. });
  645. return costLimit;
  646. }
  647. - (void)setCostLimit:(NSUInteger)costLimit
  648. {
  649. __weak TMMemoryCache *weakSelf = self;
  650. dispatch_barrier_async(_queue, ^{
  651. TMMemoryCache *strongSelf = weakSelf;
  652. if (!strongSelf)
  653. return;
  654. strongSelf->_costLimit = costLimit;
  655. if (costLimit > 0)
  656. [strongSelf trimToCostLimitByDate:costLimit];
  657. });
  658. }
  659. - (NSUInteger)totalCost
  660. {
  661. __block NSUInteger cost = 0;
  662. dispatch_sync(_queue, ^{
  663. cost = _totalCost;
  664. });
  665. return cost;
  666. }
  667. @end