Kache.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 dispatch_once_t once_token;
  28. static Kache *obj= nil;
  29. dispatch_once(&once_token, ^{
  30. obj = [[Kache alloc] init];
  31. });
  32. return obj;
  33. }
  34. + (void)setValue:(id)value forKey:(NSString *)key expiredAfter:(NSInteger)duration {
  35. return [[Kache instance] setValue:value forKey:key expiredAfter:duration];
  36. }
  37. + (void)setValue:(id)value inDefaultPoolForKey:(NSString *)key expiredAfter:(NSInteger)duration {
  38. return [[Kache instance] setValue:value inPool:nil forKey:key expiredAfter:duration];
  39. }
  40. + (void)pushValue:(id)value {
  41. return [[Kache instance] pushValue:value toQueue:nil];
  42. }
  43. + (id)popValue {
  44. return [[Kache instance] popFromQueue:nil];
  45. }
  46. + (id)valueForKey:(NSString *)key {
  47. return [[Kache instance] valueForKey:key];
  48. }
  49. // Make sure the Pool exsists. Use newPoolWithName:size: to create a new Pool.
  50. + (void)setValue:(id)value inPool:(NSString *)name forKey:(NSString *)key expiredAfter:(NSInteger)duration {
  51. return [[Kache instance] setValue:value inPool:name forKey:key expiredAfter:duration];
  52. }
  53. // Make sure the Queue exsists. Use newQueueWithName:size: to create a new Queue.
  54. + (void)pushValue:(id)value toQueue:(NSString *)name {
  55. return [[Kache instance] pushValue:value toQueue:name];
  56. }
  57. + (id)popFromQueue:(NSString *)name {
  58. return [[Kache instance] popFromQueue:name];
  59. }
  60. + (void)newQueueWithName:(NSString *)name size:(NSInteger)size {
  61. return [[Kache instance] newQueueWithName:name size:size];
  62. }
  63. + (void)newPoolWithName:(NSString *)name size:(NSInteger)size {
  64. return [[Kache instance] newPoolWithName:name size:size];
  65. }
  66. + (void)saveToStorage {
  67. return [[Kache instance] save];
  68. }
  69. + (void)loadFromStorage {
  70. return [[Kache instance] load];
  71. }
  72. #pragma mark - init
  73. - (id)init
  74. {
  75. return [self initWithFiletoken:@""];
  76. }
  77. // Spcify a FileToken, Kache will save the data into a different place.
  78. - (id)initWithFiletoken:(NSString *)aFiletoken
  79. {
  80. self = [super init];
  81. if (self) {
  82. self.queues = [[NSMutableDictionary alloc] init];
  83. self.pools = [[NSMutableDictionary alloc] init];
  84. self.holder = [[KHolder alloc] init];
  85. [self newPoolWithName:nil size:0];
  86. [self newQueueWithName:nil size:0];
  87. self.filetoken = aFiletoken;
  88. return self;
  89. }
  90. return nil;
  91. }
  92. #pragma mark - public
  93. - (void)setValue:(id)value forKey:(NSString *)key expiredAfter:(NSInteger)duration
  94. {
  95. [self.holder setValue:value forKey:key expiredAfter:duration];
  96. }
  97. // Make sure the Pool exsists. Use newPoolWithName:size: to create a new Pool.
  98. - (void)setValue:(id)value inPool:(NSString *)name forKey:(NSString *)key expiredAfter:(NSInteger)duration
  99. {
  100. if (nil == name || 0 >= [name length]) {
  101. name = KACHE_DEFAULT_POOL_NAME;
  102. }
  103. if ([[self.pools allKeys] containsObject:name]) {
  104. KPool *pool = [self.pools objectForKey:name];
  105. [pool setValue:value forKey:key expiredAfter:duration];
  106. }
  107. }
  108. // Make sure the Queue exsists. Use newQueueWithName:size: to create a new Queue.
  109. - (void)pushValue:(id)value toQueue:(NSString *)name
  110. {
  111. if (nil == name || 0 >= [name length]) {
  112. name = KACHE_DEFAULT_QUEUE_NAME;
  113. }
  114. if ([[self.queues allKeys] containsObject:name]) {
  115. KQueue *queue = [self.queues objectForKey:name];
  116. [queue push:value];
  117. }
  118. }
  119. - (id)popFromQueue:(NSString *)name
  120. {
  121. if (nil == name || 0 >= [name length]) {
  122. name = KACHE_DEFAULT_QUEUE_NAME;
  123. }
  124. if ([[self.queues allKeys] containsObject:name]) {
  125. KQueue *queue = [self.queues objectForKey:name];
  126. return [queue pop];
  127. }
  128. return nil;
  129. }
  130. - (id)valueForKey:(NSString *)key
  131. {
  132. return [self.holder valueForKey:key];
  133. }
  134. - (void)newQueueWithName:(NSString *)name size:(NSInteger)size
  135. {
  136. if (nil == name || 0 >= [name length]) {
  137. name = KACHE_DEFAULT_QUEUE_NAME;
  138. }
  139. if (! [[self.queues allKeys] containsObject:name]) {
  140. KQueue *queue = [[KQueue alloc] initWithHolder:self.holder];
  141. if (0 < size) {
  142. queue.size = size;
  143. }
  144. queue.name = name;
  145. [self.queues setValue:queue
  146. forKey:name];
  147. }
  148. }
  149. - (void)newPoolWithName:(NSString *)name size:(NSInteger)size;
  150. {
  151. if (nil == name || 0 >= [name length]) {
  152. name = KACHE_DEFAULT_POOL_NAME;
  153. }
  154. if (! [[self.pools allKeys] containsObject:name]) {
  155. KPool *pool = [[KPool alloc] initWithHolder:self.holder];
  156. if (0 < size) {
  157. pool.size = size;
  158. }
  159. pool.name = name;
  160. [self.pools setValue:pool
  161. forKey:name];
  162. }
  163. }
  164. - (void)save {
  165. NSMutableArray *queueArray = [[NSMutableArray alloc] init];
  166. NSMutableArray *poolArray = [[NSMutableArray alloc] init];
  167. for (KQueue *queue in [self.queues objectEnumerator]) {
  168. [queueArray addObject:[queue serialize]];
  169. }
  170. for (KPool *pool in [self.pools objectEnumerator]) {
  171. [poolArray addObject:[pool serialize]];
  172. }
  173. NSDictionary *kacheDict = [[NSDictionary alloc] initWithObjectsAndKeys:
  174. [self.holder serialize], @"holder",
  175. queueArray, @"queues",
  176. poolArray, @"pools",
  177. nil];
  178. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
  179. NSString *libDirectory = [paths objectAtIndex:0];
  180. NSString *path = @"Caches/KACHE_STORAGE_FILE_QERFCVBJKOL:";
  181. if (self.filetoken) {
  182. path = [path stringByAppendingString:self.filetoken];
  183. }
  184. NSString *filePath = [libDirectory stringByAppendingPathComponent:path];
  185. NSData *d = [NSKeyedArchiver archivedDataWithRootObject:kacheDict];
  186. [d writeToFile:filePath atomically:YES];
  187. }
  188. - (void)load {
  189. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
  190. NSString *libDirectory = [paths objectAtIndex:0];
  191. NSString *path = @"Caches/KACHE_STORAGE_FILE_QERFCVBJKOL:";
  192. if (self.filetoken) {
  193. path = [path stringByAppendingString:self.filetoken];
  194. }
  195. NSString *filePath = [libDirectory stringByAppendingPathComponent:path];
  196. NSData *d = [NSData dataWithContentsOfFile:filePath];
  197. NSDictionary *kacheDict = [NSDictionary dictionaryWithDictionary:
  198. [NSKeyedUnarchiver unarchiveObjectWithData:d]];
  199. if (kacheDict && 0 < [kacheDict count]) {
  200. [self.holder unserializeFrom:[kacheDict objectForKey:@"holder"]];
  201. for (NSDictionary *queueDict in [kacheDict objectForKey:@"queues"]) {
  202. KQueue *queue = [[KQueue alloc] initWithHolder:self.holder];
  203. [queue unserializeFrom:queueDict];
  204. [self.queues setValue:queue forKey:[queueDict objectForKey:@"name"]];
  205. }
  206. for (NSDictionary *poolDict in [kacheDict objectForKey:@"pools"]) {
  207. KPool *pool = [[KPool alloc] initWithHolder:self.holder];
  208. [pool unserializeFrom:poolDict];
  209. [self.pools setValue:pool forKey:[poolDict objectForKey:@"name"]];
  210. }
  211. }
  212. }
  213. @end