KHolder.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // KHolder.m
  3. // KacheDemo
  4. //
  5. // Created by jiajun on 7/25/12.
  6. // Copyright (c) 2012 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "KConfig.h"
  9. #import "KHolder.h"
  10. #import "KObject.h"
  11. #import "KPool.h"
  12. #import "KQueue.h"
  13. #import "KUtil.h"
  14. @interface KHolder ()
  15. @property (strong, nonatomic) NSMutableDictionary *objects;
  16. @property (strong, atomic) NSMutableArray *keys;
  17. - (void)cleanExpiredObjects;
  18. @end
  19. @implementation KHolder
  20. @synthesize objects = _objects;
  21. @synthesize keys = _keys;
  22. #pragma mark - init
  23. - (id)init
  24. {
  25. self = [super init];
  26. if (self) {
  27. self.objects = [[NSMutableDictionary alloc] init];
  28. self.keys = [[NSMutableArray alloc] init];
  29. return self;
  30. }
  31. return nil;
  32. }
  33. #pragma mark - private
  34. - (void)cleanExpiredObjects
  35. {
  36. if (self.keys && 0 < [self.keys count]) {
  37. for (int i = 0; i < [self.keys count] - 1; i ++) {
  38. NSString *tmpKey = [self.keys objectAtIndex:i];
  39. KObject *leftObject = [self objectForKey:tmpKey];
  40. if ([leftObject expiredTimestamp] < [KUtil nowTimestamp]) {
  41. [self.keys removeObject:tmpKey];
  42. [self.objects removeObjectForKey:tmpKey];
  43. }
  44. else {
  45. break;
  46. }
  47. }
  48. }
  49. }
  50. #pragma mark - public
  51. - (void)removeObjectForKey:(NSString *)key {
  52. [self.keys removeObject:key];
  53. [self.objects removeObjectForKey:key];
  54. }
  55. - (void)setValue:(id)value forKey:(NSString *)key expiredAfter:(NSInteger)duration
  56. {
  57. KObject *object = [[KObject alloc] initWithData:value andLifeDuration:duration];
  58. [self.objects setValue:object.object forKey:key];
  59. KObject *suchObject = [self objectForKey:key];
  60. // TODO sort the key by expired time.
  61. [self.keys removeObject:key];
  62. if (0 < [self.keys count]) {
  63. [self cleanExpiredObjects];
  64. for (int i = [self.keys count] - 1; i >= 0; i --) {
  65. NSString *tmpKey = [self.keys objectAtIndex:i];
  66. KObject *leftObject = [self objectForKey:tmpKey];
  67. if ([leftObject expiredTimestamp] <= [suchObject expiredTimestamp]) {
  68. if (([self.keys count] - 1) == i) {
  69. [self.keys addObject:key];
  70. }
  71. else {
  72. [self.keys insertObject:key atIndex:i + 1];
  73. }
  74. break;
  75. }
  76. }
  77. }
  78. if (! [self.keys containsObject:key]) {
  79. [self.keys insertObject:key atIndex:0];
  80. }
  81. }
  82. - (id)valueForKey:(NSString *)key
  83. {
  84. KObject *object = [self objectForKey:key];
  85. if (object && ! [object expired]) {
  86. return [object value];
  87. }
  88. // No such object.
  89. return nil;
  90. }
  91. - (KObject *)objectForKey:(NSString *)key
  92. {
  93. if ([[self.objects allKeys] containsObject:key]) {
  94. return [[KObject alloc] initWithDictionary:[self.objects objectForKey:key]];
  95. }
  96. return nil;
  97. }
  98. // Convert object to NSDictionary.
  99. - (NSDictionary *)serialize
  100. {
  101. return [NSDictionary dictionaryWithObjectsAndKeys:
  102. self.objects, @"objects",
  103. self.keys, @"keys",
  104. nil];
  105. }
  106. // Convert NSDictionary to object.
  107. - (void)unserializeFrom:(NSDictionary *)dict
  108. {
  109. if ([[dict allKeys] containsObject:@"objects"]
  110. && [[dict allKeys] containsObject:@"keys"]) {
  111. self.objects = [dict objectForKey:@"objects"];
  112. self.keys = [dict objectForKey:@"keys"];
  113. }
  114. }
  115. @end