Kache.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //
  2. // Kache.m
  3. // KacheDemo
  4. //
  5. // Created by jiajun on 7/25/12.
  6. // Copyright (c) 2012 __MyCompanyName__. All rights reserved.
  7. //
  8. #define KACHE_DEFAULT_POOL_NAME @"kache_default_pool_name_LKJHGFDWQSFASRTYUIOP:"
  9. #define KACHE_DEFAULT_QUEUE_NAME @"kache_default_queue_name_QWEDFVHUIOPPLMUYTRDX:"
  10. #import "KConfig.h"
  11. #import "KHolder.h"
  12. #import "KPool.h"
  13. #import "KQueue.h"
  14. #import "Kache.h"
  15. @interface Kache ()
  16. @property (strong, nonatomic) KHolder *holder;
  17. @property (strong, nonatomic) NSMutableDictionary *pools;
  18. @property (strong, nonatomic) NSMutableDictionary *queues;
  19. @end
  20. @implementation Kache
  21. @synthesize filetoken = _filetoken;
  22. @synthesize holder = _holder;
  23. @synthesize pools = _pools;
  24. @synthesize queues = _queues;
  25. #pragma mark static for default
  26. + (Kache *)instance {
  27. static Kache *obj= nil;
  28. #if defined(__IPHONE_5_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_5_0
  29. static dispatch_once_t once_token;
  30. dispatch_once(&once_token, ^{
  31. obj = [[Kache alloc] initWithFiletoken:@""];
  32. });
  33. #elif defined(__IPHONE_4_3) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_3
  34. if (nil == obj) {
  35. obj = [[Kache alloc] initWithFiletoken:@""];
  36. }
  37. #else
  38. #error Your SDK is too old ! Need at least 4.3.
  39. #endif
  40. return obj;
  41. }
  42. + (void)setValue:(id)value forKey:(NSString *)key expiredAfter:(NSInteger)duration {
  43. return [[Kache instance] setValue:value forKey:key expiredAfter:duration];
  44. }
  45. + (void)setValue:(id)value inDefaultPoolForKey:(NSString *)key expiredAfter:(NSInteger)duration {
  46. return [[Kache instance] setValue:value inPool:nil forKey:key expiredAfter:duration];
  47. }
  48. + (void)removeObjectForKey:(NSString *)key
  49. {
  50. return [[Kache instance] removeObjectForKey:key];
  51. }
  52. + (void)pushValue:(id)value {
  53. return [[Kache instance] pushValue:value toQueue:nil];
  54. }
  55. + (id)popValue {
  56. return [[Kache instance] popFromQueue:nil];
  57. }
  58. + (id)valueForKey:(NSString *)key {
  59. return [[Kache instance] valueForKey:key];
  60. }
  61. // Make sure the Pool exsists. Use newPoolWithName:size: to create a new Pool.
  62. + (void)setValue:(id)value inPool:(NSString *)name forKey:(NSString *)key expiredAfter:(NSInteger)duration {
  63. return [[Kache instance] setValue:value inPool:name forKey:key expiredAfter:duration];
  64. }
  65. // Make sure the Queue exsists. Use newQueueWithName:size: to create a new Queue.
  66. + (void)pushValue:(id)value toQueue:(NSString *)name {
  67. return [[Kache instance] pushValue:value toQueue:name];
  68. }
  69. + (id)popFromQueue:(NSString *)name {
  70. return [[Kache instance] popFromQueue:name];
  71. }
  72. + (void)newQueueWithName:(NSString *)name size:(NSInteger)size {
  73. return [[Kache instance] newQueueWithName:name size:size];
  74. }
  75. + (void)newPoolWithName:(NSString *)name size:(NSInteger)size {
  76. return [[Kache instance] newPoolWithName:name size:size];
  77. }
  78. + (void)save {
  79. return [[Kache instance] save];
  80. }
  81. + (void)load {
  82. return [[Kache instance] load];
  83. }
  84. #pragma mark - init
  85. - (id)init
  86. {
  87. self = [super init];
  88. if (self) {
  89. self.queues = [[NSMutableDictionary alloc] init];
  90. self.pools = [[NSMutableDictionary alloc] init];
  91. self.holder = [[KHolder alloc] initWithToken:@""];
  92. [self newPoolWithName:nil size:0];
  93. [self newQueueWithName:nil size:0];
  94. self.filetoken = @"";
  95. return self;
  96. }
  97. return nil;
  98. }
  99. // Spcify a FileToken, Kache will save the data into a different place.
  100. - (id)initWithFiletoken:(NSString *)aFiletoken
  101. {
  102. self = [super init];
  103. if (self) {
  104. self.queues = [[NSMutableDictionary alloc] init];
  105. self.pools = [[NSMutableDictionary alloc] init];
  106. self.holder = [[KHolder alloc] initWithToken:aFiletoken];
  107. [self newPoolWithName:nil size:0];
  108. [self newQueueWithName:nil size:0];
  109. self.filetoken = aFiletoken;
  110. return self;
  111. }
  112. return nil;
  113. }
  114. #pragma mark - public
  115. - (void)setValue:(id)value forKey:(NSString *)key expiredAfter:(NSInteger)duration
  116. {
  117. [self.holder setValue:value forKey:key expiredAfter:duration];
  118. }
  119. // Make sure the Pool exsists. Use newPoolWithName:size: to create a new Pool.
  120. - (void)setValue:(id)value inPool:(NSString *)name forKey:(NSString *)key expiredAfter:(NSInteger)duration
  121. {
  122. if (nil == name || 0 >= [name length]) {
  123. name = KACHE_DEFAULT_POOL_NAME;
  124. }
  125. if ([[self.pools allKeys] containsObject:name]) {
  126. KPool *pool = [self.pools objectForKey:name];
  127. [pool setValue:value forKey:key expiredAfter:duration];
  128. }
  129. }
  130. - (void)removeObjectForKey:(NSString *)key
  131. {
  132. for (NSString *name in [self.pools allKeys]) {
  133. KPool *pool = [self.pools valueForKey:name];
  134. [pool removeObjectForKey:key];
  135. }
  136. [self.holder removeObjectForKey:key];
  137. }
  138. // Make sure the Queue exsists. Use newQueueWithName:size: to create a new Queue.
  139. - (void)pushValue:(id)value toQueue:(NSString *)name
  140. {
  141. if (nil == name || 0 >= [name length]) {
  142. name = KACHE_DEFAULT_QUEUE_NAME;
  143. }
  144. if ([[self.queues allKeys] containsObject:name]) {
  145. KQueue *queue = [self.queues objectForKey:name];
  146. [queue push:value];
  147. }
  148. }
  149. - (id)popFromQueue:(NSString *)name
  150. {
  151. if (nil == name || 0 >= [name length]) {
  152. name = KACHE_DEFAULT_QUEUE_NAME;
  153. }
  154. if ([[self.queues allKeys] containsObject:name]) {
  155. KQueue *queue = [self.queues objectForKey:name];
  156. return [queue pop];
  157. }
  158. return nil;
  159. }
  160. - (id)valueForKey:(NSString *)key
  161. {
  162. return [self.holder valueForKey:key];
  163. }
  164. - (void)newQueueWithName:(NSString *)name size:(NSInteger)size
  165. {
  166. if (nil == name || 0 >= [name length]) {
  167. name = KACHE_DEFAULT_QUEUE_NAME;
  168. }
  169. if (! [[self.queues allKeys] containsObject:name]) {
  170. KQueue *queue = [[KQueue alloc] initWithHolder:self.holder];
  171. if (0 < size) {
  172. queue.size = size;
  173. }
  174. queue.name = name;
  175. [self.queues setValue:queue
  176. forKey:name];
  177. }
  178. }
  179. - (void)newPoolWithName:(NSString *)name size:(NSInteger)size;
  180. {
  181. if (nil == name || 0 >= [name length]) {
  182. name = KACHE_DEFAULT_POOL_NAME;
  183. }
  184. if (! [[self.pools allKeys] containsObject:name]) {
  185. KPool *pool = [[KPool alloc] initWithHolder:self.holder];
  186. if (0 < size) {
  187. pool.size = size;
  188. }
  189. pool.name = name;
  190. [self.pools setValue:pool
  191. forKey:name];
  192. }
  193. }
  194. - (void)save {
  195. NSMutableArray *queueArray = [[NSMutableArray alloc] init];
  196. NSMutableArray *poolArray = [[NSMutableArray alloc] init];
  197. for (KQueue *queue in [self.queues objectEnumerator]) {
  198. [queueArray addObject:[queue serialize]];
  199. }
  200. for (KPool *pool in [self.pools objectEnumerator]) {
  201. [poolArray addObject:[pool serialize]];
  202. }
  203. NSDictionary *kacheDict = [[NSDictionary alloc] initWithObjectsAndKeys:
  204. [self.holder serialize], @"holder",
  205. queueArray, @"queues",
  206. poolArray, @"pools",
  207. nil];
  208. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
  209. NSString *libDirectory = [paths objectAtIndex:0];
  210. NSString *path = @"Caches/KACHE_STORAGE_FILE_QERFCVBJKOL:";
  211. if (self.filetoken) {
  212. path = [path stringByAppendingPathExtension:self.filetoken];
  213. }
  214. NSString *filePath = [libDirectory stringByAppendingPathComponent:path];
  215. NSData *d = [NSKeyedArchiver archivedDataWithRootObject:kacheDict];
  216. [d writeToFile:filePath atomically:YES];
  217. }
  218. - (void)load {
  219. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
  220. NSString *libDirectory = [paths objectAtIndex:0];
  221. NSString *path = @"Caches/KACHE_STORAGE_FILE_QERFCVBJKOL:";
  222. if (self.filetoken) {
  223. path = [path stringByAppendingPathExtension:self.filetoken];
  224. }
  225. NSString *filePath = [libDirectory stringByAppendingPathComponent:path];
  226. NSData *d = [NSData dataWithContentsOfFile:filePath];
  227. if (d && 0 < d.length) {
  228. NSDictionary *kacheDict = [NSDictionary dictionaryWithDictionary:
  229. [NSKeyedUnarchiver unarchiveObjectWithData:d]];
  230. if (kacheDict && 0 < [kacheDict count]) {
  231. [self.holder unserializeFrom:[kacheDict objectForKey:@"holder"]];
  232. for (NSDictionary *queueDict in [kacheDict objectForKey:@"queues"]) {
  233. KQueue *queue = [[KQueue alloc] initWithHolder:self.holder];
  234. [queue unserializeFrom:queueDict];
  235. [self.queues setValue:queue forKey:[queueDict objectForKey:@"name"]];
  236. }
  237. for (NSDictionary *poolDict in [kacheDict objectForKey:@"pools"]) {
  238. KPool *pool = [[KPool alloc] initWithHolder:self.holder];
  239. [pool unserializeFrom:poolDict];
  240. [self.pools setValue:pool forKey:[poolDict objectForKey:@"name"]];
  241. }
  242. }
  243. }
  244. }
  245. @end