KCHMainViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //
  2. // KCHMainViewController.m
  3. // KacheDemo
  4. //
  5. // Created by jiajun on 7/25/12.
  6. // Copyright (c) 2012 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "KCHMainViewController.h"
  9. #import "KCH.h"
  10. @interface KCHMainViewController ()
  11. @property (strong, nonatomic) Kache *kache;
  12. @property (strong, nonatomic) UILabel *banner;
  13. @property (strong, nonatomic) UILabel *body;
  14. @property (strong, nonatomic) UIScrollView *bodyBackground;
  15. @property (assign, nonatomic) NSInteger offset;
  16. @property (assign, nonatomic) NSInteger timmer;
  17. @property (assign, nonatomic) NSInteger counter;
  18. @end
  19. @implementation KCHMainViewController
  20. @synthesize kache = _kache;
  21. @synthesize banner = _banner;
  22. @synthesize body = _body;
  23. @synthesize bodyBackground = _bodyBackground;
  24. @synthesize offset = _offset;
  25. @synthesize timmer = _timmer;
  26. @synthesize counter = _counter;
  27. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  28. {
  29. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  30. if (self) {
  31. self.kache = [[Kache alloc] initWithFiletoken:@"demo"];
  32. self.timmer = 0;
  33. self.offset = 0;
  34. }
  35. return self;
  36. }
  37. - (void)viewDidLoad
  38. {
  39. [super viewDidLoad];
  40. self.view.backgroundColor = [UIColor whiteColor];
  41. self.banner = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 10.0f, 320.0f, 20.0f)];
  42. self.banner.backgroundColor = [UIColor clearColor];
  43. self.banner.textAlignment = NSTextAlignmentCenter;
  44. self.banner.font = [UIFont boldSystemFontOfSize:18];
  45. [self.view addSubview:self.banner];
  46. self.body = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 300.0f, 410.0f)];
  47. self.body.backgroundColor = [UIColor clearColor];
  48. self.body.numberOfLines = 0;
  49. self.body.lineBreakMode = NSLineBreakByCharWrapping;
  50. self.body.textAlignment = NSTextAlignmentLeft;
  51. self.body.text = @"";
  52. [self.body sizeToFit];
  53. self.body.frame = CGRectMake(0.0f, 0.0f, 300.0f, self.body.bounds.size.height);
  54. [self countDown];
  55. [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(countDown) userInfo:nil repeats:YES];
  56. self.bodyBackground = [[UIScrollView alloc] initWithFrame:CGRectMake(10.0f, 40.0f, 320.0f, 410.0f)];
  57. self.bodyBackground.contentSize = CGSizeMake(300.0f, self.body.bounds.size.height);
  58. [self.bodyBackground addSubview:self.body];
  59. [self.view addSubview:self.bodyBackground];
  60. NSInteger delay = 1.0f;
  61. // Begin Test.
  62. [self printLog:@"===== Test Begin ====="];
  63. [self performSelector:@selector(simpleTest) withObject:nil afterDelay:delay]; delay += 12;
  64. [self performSelector:@selector(queueTest) withObject:nil afterDelay:delay]; delay += 13;
  65. [self performSelector:@selector(poolTest) withObject:nil afterDelay:delay]; delay += 8;
  66. [self performSelector:@selector(saveTest) withObject:nil afterDelay:delay]; delay += 7;
  67. [self performSelector:@selector(loadTest) withObject:nil afterDelay:delay]; delay += 5;
  68. [self performSelector:@selector(printLog:) withObject:@"===== All Test Done =====" afterDelay:delay];
  69. }
  70. - (void)simpleTest
  71. {
  72. [self printLog:@"====== Begin Simple Test. ======"];
  73. // Set Simple Cache.
  74. [self printLog:@"Set 12 Simple Cache Value:\nkey_0 ~ key_11."];
  75. for (int i = 0; i < 12; i ++) {
  76. // After (i+3) seconds, the value will be expired.
  77. [self.kache setValue:[NSString stringWithFormat:@"ValueWithLifeDuration-%d-AndOffset-%d", i + 3, i]
  78. forKey:[NSString stringWithFormat:@"key_%d", i]
  79. expiredAfter:i + 3];
  80. }
  81. [self performSelector:@selector(performPrintValue:) withObject:@"key_1" afterDelay:1.0f];
  82. [self performSelector:@selector(performPrintValue:) withObject:@"key_5" afterDelay:2.0f];
  83. [self performSelector:@selector(printLog:)
  84. withObject:@"Waiting..."
  85. afterDelay:3.0f];
  86. [self performSelector:@selector(printLog:)
  87. withObject:@"After 5 seconds.\nObject: \"key_1\" has been expired."
  88. afterDelay:4.0f];
  89. [self performSelector:@selector(performPrintValue:) withObject:@"key_1" afterDelay:5.0f];
  90. [self performSelector:@selector(printLog:)
  91. withObject:@"After 10 seconds.\nObject: \"key_5\" has been expired."
  92. afterDelay:9.0f];
  93. [self performSelector:@selector(performPrintValue:) withObject:@"key_5" afterDelay:10.0f];
  94. }
  95. - (void)queueTest
  96. {
  97. [self printLog:@"====== Begin Queue Test. ======"];
  98. // Set a Queue with Default Size 10.
  99. [self printLog:@"Push 10 values to the default queue."];
  100. [self.kache newQueueWithName:@"for_test_queue" size:10];
  101. for (int i = 0; i < 10; i ++) {
  102. [self.kache pushValue:[NSString stringWithFormat:@"QueueValue-%d", i] toQueue:@"for_test_queue"]; // Default Queue.
  103. }
  104. for (int i = 0; i < 11; i ++) {
  105. [self performSelector:@selector(printLog:)
  106. withObject:[NSString stringWithFormat:@"Pop a Value:\n\"%@\"", [self.kache popFromQueue:@"for_test_queue"]]
  107. afterDelay:i + 1];
  108. }
  109. }
  110. - (void)poolTest
  111. {
  112. [self printLog:@"====== Begin Pool Test. ======"];
  113. // Pool Test
  114. [self printLog:@"Set 10 values to the default pool."];
  115. [self.kache newPoolWithName:@"for_test_pool" size:10];
  116. for (int i = 0; i < 10; i ++) {
  117. // After (i+3) seconds, the value will be expired.
  118. [self.kache setValue:[NSString stringWithFormat:@"PoolValue-Offset-%d", i]
  119. inPool:@"for_test_pool"
  120. forKey:[NSString stringWithFormat:@"pool_key_%d", i]
  121. expiredAfter:i + 3];
  122. }
  123. [self performSelector:@selector(printLog:)
  124. withObject:[NSString stringWithFormat:@"Value of pool_key_0: \"%@\"", [self.kache valueForKey:@"pool_key_0"]]
  125. afterDelay:1.0f];
  126. [self performSelector:@selector(printLog:)
  127. withObject:[NSString stringWithFormat:@"Value of pool_key_1: \"%@\"", [self.kache valueForKey:@"pool_key_1"]]
  128. afterDelay:2.0f];
  129. [self performSelector:@selector(printLog:)
  130. withObject:@"Set 2 more value to the Pool."
  131. afterDelay:3.0f];
  132. [self.kache setValue:[NSString stringWithFormat:@"PoolValue-Offset-%d", 10]
  133. inPool:@"for_test_pool"
  134. forKey:[NSString stringWithFormat:@"pool_key_%d", 10]
  135. expiredAfter:20];
  136. [self.kache setValue:[NSString stringWithFormat:@"PoolValue-Offset-%d", 11]
  137. inPool:@"for_test_pool"
  138. forKey:[NSString stringWithFormat:@"pool_key_%d", 11]
  139. expiredAfter:20];
  140. [self performSelector:@selector(printLog:)
  141. withObject:@"Value of pool_key_0 and pool_key_1 should be removed."
  142. afterDelay:4.0f];
  143. [self performSelector:@selector(printLog:)
  144. withObject:[NSString stringWithFormat:@"Value of pool_key_0: \"%@\"", [self.kache valueForKey:@"pool_key_0"]]
  145. afterDelay:5.0f];
  146. [self performSelector:@selector(printLog:)
  147. withObject:[NSString stringWithFormat:@"Value of pool_key_1: \"%@\"", [self.kache valueForKey:@"pool_key_1"]]
  148. afterDelay:6.0f];
  149. }
  150. - (void)saveTest
  151. {
  152. [self printLog:@"====== Begin Save Test. ======"];
  153. [self printLog:@"Set 10 simple values."];
  154. for (int i = 0; i < 12; i ++) {
  155. // expiredAfter:0 means expired after default duration (KACHE_DEFAULT_LIFE_DURATION: 10 days by default.)
  156. [self.kache setValue:[NSString stringWithFormat:@"SaveNewValueWithLifeDuration-%d-AndOffset-%d", i + 3, i]
  157. forKey:[NSString stringWithFormat:@"save_key_%d", i]
  158. expiredAfter:0];
  159. }
  160. [self performSelector:@selector(printLog:)
  161. withObject:[NSString stringWithFormat:@"Value of \"save_key_0\"\n\"%@\"",
  162. [self.kache valueForKey:@"save_key_0"]]
  163. afterDelay:1.0f];
  164. [self performSelector:@selector(printLog:) withObject:@"Push 10 values to the Queue." afterDelay:2.0f];
  165. [self.kache newQueueWithName:@"for_save_test_queue" size:10];
  166. for (int i = 0; i < 10; i ++) {
  167. [self.kache pushValue:[NSString stringWithFormat:@"SaveNewQueueValue-%d", i] toQueue:@"for_save_test_queue"];
  168. }
  169. [self performSelector:@selector(printLog:)
  170. withObject:[NSString stringWithFormat:@"Do one Pop:\n\"%@\"",
  171. [self.kache popFromQueue:@"for_save_test_queue"]]
  172. afterDelay:3.0f];
  173. [self performSelector:@selector(printLog:) withObject:@"Set 10 values to the Pool." afterDelay:4.0f];
  174. [self.kache newPoolWithName:@"for_save_test_pool" size:10];
  175. for (int i = 0; i < 10; i ++) {
  176. // expiredAfter:0 means expired after default duration (KACHE_DEFAULT_LIFE_DURATION: 10 days by default.)
  177. [self.kache setValue:[NSString stringWithFormat:@"SaveNewPoolValue-Offset-%d", i]
  178. inPool:@"for_save_test_pool"
  179. forKey:[NSString stringWithFormat:@"pool_key_%d", i]
  180. expiredAfter:0];
  181. }
  182. [self performSelector:@selector(printLog:)
  183. withObject:[NSString stringWithFormat:@"Value of \"pool_key_0\"\n\"%@\"",
  184. [self.kache valueForKey:@"pool_key_0"]]
  185. afterDelay:5.0f];
  186. [self performSelector:@selector(printLog:) withObject:@"Save to Disk." afterDelay:6.0f];
  187. [self.kache save];
  188. }
  189. - (void)loadTest
  190. {
  191. [self printLog:@"====== Begin Load Test. ======"];
  192. Kache *tmpKache = [[Kache alloc] initWithFiletoken:@"demo"];
  193. [tmpKache load];
  194. [self printLog:@"New Kache instance load from disk."];
  195. [self performSelector:@selector(printLog:)
  196. withObject:[NSString stringWithFormat:@"Value of \"save_key_0\"\n\"%@\"",
  197. [tmpKache valueForKey:@"save_key_0"]]
  198. afterDelay:1.0f];
  199. [self performSelector:@selector(printLog:)
  200. withObject:[NSString stringWithFormat:@"Do one Pop:\n\"%@\"",
  201. [tmpKache popFromQueue:@"for_save_test_queue"]]
  202. afterDelay:2.0f];
  203. [self performSelector:@selector(printLog:)
  204. withObject:[NSString stringWithFormat:@"Value of \"pool_key_0\"\n\"%@\"",
  205. [tmpKache valueForKey:@"pool_key_0"]]
  206. afterDelay:3.0f];
  207. }
  208. - (void)performPrintValue:(NSString *)key
  209. {
  210. [self printLog:[NSString stringWithFormat:@"Value of %@:\n\"%@\"",
  211. key, [self.kache valueForKey:key]]];
  212. }
  213. - (void)countDown
  214. {
  215. self.banner.text = [NSString stringWithFormat:@"Kache Demo, %d second(s)", self.timmer];
  216. self.timmer ++;
  217. }
  218. - (void)printLog:(NSString *)log
  219. {
  220. NSLog(@"%@", log);
  221. self.body.text = [NSString stringWithFormat:@"%@\n\n%@", log, self.body.text];
  222. [self.body sizeToFit];
  223. self.body.frame = CGRectMake(0.0f, 0.0f, 300.0f, self.body.bounds.size.height);
  224. self.bodyBackground.contentSize = CGSizeMake(300.0f, self.body.bounds.size.height);
  225. }
  226. - (void)viewDidUnload
  227. {
  228. [super viewDidUnload];
  229. // Release any retained subviews of the main view.
  230. }
  231. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  232. {
  233. return (interfaceOrientation == UIInterfaceOrientationPortrait);
  234. }
  235. @end