财神随手记账

HPCacheListManage.m 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //
  2. // HPCacheListManage.m
  3. // HPSlideSegmentView
  4. //
  5. // Created by 何鹏 on 17/7/14.
  6. // Copyright © 2017年 何鹏. All rights reserved.
  7. //
  8. #import "HPCacheListManage.h"
  9. @interface HPCacheListManage ()
  10. @property(nonatomic,strong) NSMutableDictionary *cacheDictionary;
  11. @property(nonatomic,strong) dispatch_queue_t cahceQueue;
  12. @property(nonatomic,strong) NSMutableArray *currentArray;
  13. @property(nonatomic,strong) NSLock *lock;
  14. @end
  15. @implementation HPCacheListManage
  16. -(instancetype)init
  17. {
  18. if (self=[super init]) {
  19. _lock = self.lock;
  20. _cahceQueue=dispatch_queue_create("hpSlideSegamentView.hepeng.slide", NULL);
  21. }
  22. return self;
  23. }
  24. -(void)addCacheWithLeft:(ObjcWithKeyStruct)left
  25. Centre:(ObjcWithKeyStruct)centre
  26. Right:(ObjcWithKeyStruct)right
  27. updateContent:(BOOL)update
  28. {
  29. [self.currentArray removeAllObjects];
  30. [self addObjeWithNumber:left.keyNum];
  31. [self addObjeWithNumber:centre.keyNum];
  32. [self addObjeWithNumber:right.keyNum];
  33. [self isCacheWithObj:left
  34. updateContent:update];
  35. [self isCacheWithObj:centre
  36. updateContent:update];
  37. [self isCacheWithObj:right
  38. updateContent:update];
  39. if ([_delegate respondsToSelector:@selector(hp_updateWihtLayotu)]) {
  40. [_delegate hp_updateWihtLayotu];
  41. }
  42. }
  43. -(void)isCacheWithObj:(ObjcWithKeyStruct)direction
  44. updateContent:(BOOL)update
  45. {
  46. if (direction.keyNum<0) {
  47. return;
  48. }
  49. if ([self isCahceWithKey:@(direction.keyNum)]) {
  50. if (update==YES) {
  51. id cacheValue = nil;
  52. if ([_delegate respondsToSelector:@selector(hp_notCahceCreat:pageIndex:)]) {
  53. cacheValue = [_delegate hp_notCahceCreat:[self cacheWithKey:@(direction.keyNum)] pageIndex:direction.keyNum];
  54. }
  55. if (cacheValue==nil) {
  56. return;
  57. }
  58. [self.cacheDictionary setObject:cacheValue forKey:@(direction.keyNum)];
  59. }
  60. if ([_delegate respondsToSelector:@selector(hp_cacheWithLayout:direction:page:)]) {
  61. [_delegate hp_cacheWithLayout:[self cacheWithKey:@(direction.keyNum)] direction:direction.directionType page:direction.keyNum];
  62. }
  63. }
  64. else
  65. {
  66. id cacheValue=nil;
  67. if ([_delegate respondsToSelector:@selector(hp_notCahceCreat:pageIndex:)]) {
  68. cacheValue = [_delegate hp_notCahceCreat:nil pageIndex:direction.keyNum];
  69. }
  70. if (cacheValue==nil) {
  71. return;
  72. }
  73. if (self.cacheDictionary.count>=self.cacheListMax) {
  74. [self deleWithAddObj:cacheValue
  75. keyNum:direction];
  76. }
  77. else
  78. {
  79. [self.cacheDictionary setObject:cacheValue forKey:@(direction.keyNum)];
  80. if ([_delegate respondsToSelector:@selector(hp_cacheWithLayout:direction:page:)]) {
  81. [_delegate hp_cacheWithLayout:[_cacheDictionary objectForKey:@(direction.keyNum)] direction:direction.directionType page:direction.keyNum];
  82. }
  83. }
  84. }
  85. }
  86. -(void)addObjeWithNumber:(NSInteger)number
  87. {
  88. if (number<0) {
  89. return;
  90. }
  91. [_currentArray addObject:@(number)];
  92. }
  93. -(void)deleWithAddObj:(id)cacheObj keyNum:(ObjcWithKeyStruct)direction
  94. {
  95. dispatch_async(self.cahceQueue, ^{
  96. [_lock lock];
  97. id selectObj=nil;
  98. NSNumber *numberMax=nil;
  99. NSUInteger maxNumber=0;
  100. NSArray *keys=_cacheDictionary.allKeys;
  101. for (int i=0; i<keys.count; i++) {
  102. if (![_currentArray containsObject:keys[i]]) {
  103. NSNumber *num=keys[i];
  104. NSUInteger number=[self abs:(num.unsignedIntegerValue-direction.keyNum)];
  105. if (number>maxNumber) {
  106. maxNumber=number;
  107. selectObj=[_cacheDictionary objectForKey:keys[i]];
  108. numberMax=keys[i];
  109. }
  110. }
  111. }
  112. [_lock unlock];
  113. if (selectObj!=nil && cacheObj!=nil && numberMax!=nil) {
  114. [_cacheDictionary removeObjectForKey:numberMax];
  115. if ([_delegate respondsToSelector:@selector(removeWithCacheObj:)]) {
  116. dispatch_async(dispatch_get_main_queue(), ^{
  117. [_delegate removeWithCacheObj:selectObj];
  118. });
  119. }
  120. [_cacheDictionary setObject:cacheObj forKey:@(direction.keyNum)];
  121. dispatch_async(dispatch_get_main_queue(), ^{
  122. if ([_delegate respondsToSelector:@selector(hp_cacheWithLayout:direction:page:)]) {
  123. [_delegate hp_cacheWithLayout:[_cacheDictionary objectForKey:@(direction.keyNum)] direction:direction.directionType page:direction.keyNum];
  124. }
  125. if ([_delegate respondsToSelector:@selector(hp_updateWihtLayotu)]) {
  126. [_delegate hp_updateWihtLayotu];
  127. }
  128. });
  129. }
  130. });
  131. }
  132. -(NSUInteger)abs:(NSInteger)number
  133. {
  134. if (number<0) {
  135. return number * -1;
  136. }
  137. return number;
  138. }
  139. -(id)cacheWithKey:(id)key
  140. {
  141. if (key==nil) {
  142. return nil;
  143. }
  144. id model=[self.cacheDictionary objectForKey:key];
  145. return model;
  146. }
  147. -(BOOL)isCahceWithKey:(id)key
  148. {
  149. if (key==nil) {
  150. return NO;
  151. }
  152. id model=[self.cacheDictionary objectForKey:key];
  153. if (model==nil) {
  154. return NO;
  155. }
  156. return YES;
  157. }
  158. #pragma mark 懒加载
  159. -(NSLock *)lock{
  160. if (_lock == nil) {
  161. _lock = [[NSLock alloc] init];
  162. }
  163. return _lock;
  164. }
  165. -(NSUInteger)cacheListMax
  166. {
  167. if (_cacheListMax<3) {
  168. return 3;
  169. }
  170. return _cacheListMax;
  171. }
  172. -(NSMutableDictionary *)cacheDictionary
  173. {
  174. if (_cacheDictionary==nil) {
  175. _cacheDictionary=[NSMutableDictionary dictionary];
  176. }
  177. return _cacheDictionary;
  178. }
  179. -(NSMutableArray *)currentArray
  180. {
  181. if (_currentArray==nil) {
  182. _currentArray=[NSMutableArray array];
  183. }
  184. return _currentArray;
  185. }
  186. @end