YYCache.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // YYCache.m
  3. // YYKit <https://github.com/ibireme/YYKit>
  4. //
  5. // Created by ibireme on 15/2/13.
  6. // Copyright (c) 2015 ibireme.
  7. //
  8. // This source code is licensed under the MIT-style license found in the
  9. // LICENSE file in the root directory of this source tree.
  10. //
  11. #import "YYCache.h"
  12. #import "YYMemoryCache.h"
  13. #import "YYDiskCache.h"
  14. @implementation YYCache
  15. - (instancetype) init {
  16. NSLog(@"Use \"initWithName\" or \"initWithPath\" to create YYCache instance.");
  17. return [self initWithPath:@""];
  18. }
  19. - (instancetype)initWithName:(NSString *)name {
  20. if (name.length == 0) return nil;
  21. NSString *cacheFolder = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
  22. NSString *path = [cacheFolder stringByAppendingPathComponent:name];
  23. return [self initWithPath:path];
  24. }
  25. - (instancetype)initWithPath:(NSString *)path {
  26. if (path.length == 0) return nil;
  27. YYDiskCache *diskCache = [[YYDiskCache alloc] initWithPath:path];
  28. if (!diskCache) return nil;
  29. NSString *name = [path lastPathComponent];
  30. YYMemoryCache *memoryCache = [YYMemoryCache new];
  31. memoryCache.name = name;
  32. self = [super init];
  33. _name = name;
  34. _diskCache = diskCache;
  35. _memoryCache = memoryCache;
  36. return self;
  37. }
  38. + (instancetype)cacheWithName:(NSString *)name {
  39. return [[self alloc] initWithName:name];
  40. }
  41. + (instancetype)cacheWithPath:(NSString *)path {
  42. return [[self alloc] initWithPath:path];
  43. }
  44. - (BOOL)containsObjectForKey:(NSString *)key {
  45. return [_memoryCache containsObjectForKey:key] || [_diskCache containsObjectForKey:key];
  46. }
  47. - (void)containsObjectForKey:(NSString *)key withBlock:(void (^)(NSString *key, BOOL contains))block {
  48. if (!block) return;
  49. if ([_memoryCache containsObjectForKey:key]) {
  50. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  51. block(key, YES);
  52. });
  53. } else {
  54. [_diskCache containsObjectForKey:key withBlock:block];
  55. }
  56. }
  57. - (id<NSCoding>)objectForKey:(NSString *)key {
  58. id<NSCoding> object = [_memoryCache objectForKey:key];
  59. if (!object) {
  60. object = [_diskCache objectForKey:key];
  61. if (object) {
  62. [_memoryCache setObject:object forKey:key];
  63. }
  64. }
  65. return object;
  66. }
  67. - (void)objectForKey:(NSString *)key withBlock:(void (^)(NSString *key, id<NSCoding> object))block {
  68. if (!block) return;
  69. id<NSCoding> object = [_memoryCache objectForKey:key];
  70. if (object) {
  71. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  72. block(key, object);
  73. });
  74. } else {
  75. [_diskCache objectForKey:key withBlock:^(NSString *key, id<NSCoding> object) {
  76. if (object && ![_memoryCache objectForKey:key]) {
  77. [_memoryCache setObject:object forKey:key];
  78. }
  79. block(key, object);
  80. }];
  81. }
  82. }
  83. - (void)setObject:(id<NSCoding>)object forKey:(NSString *)key {
  84. [_memoryCache setObject:object forKey:key];
  85. [_diskCache setObject:object forKey:key];
  86. }
  87. - (void)setObject:(id<NSCoding>)object forKey:(NSString *)key withBlock:(void (^)(void))block {
  88. [_memoryCache setObject:object forKey:key];
  89. [_diskCache setObject:object forKey:key withBlock:block];
  90. }
  91. - (void)removeObjectForKey:(NSString *)key {
  92. [_memoryCache removeObjectForKey:key];
  93. [_diskCache removeObjectForKey:key];
  94. }
  95. - (void)removeObjectForKey:(NSString *)key withBlock:(void (^)(NSString *key))block {
  96. [_memoryCache removeObjectForKey:key];
  97. [_diskCache removeObjectForKey:key withBlock:block];
  98. }
  99. - (void)removeAllObjects {
  100. [_memoryCache removeAllObjects];
  101. [_diskCache removeAllObjects];
  102. }
  103. - (void)removeAllObjectsWithBlock:(void(^)(void))block {
  104. [_memoryCache removeAllObjects];
  105. [_diskCache removeAllObjectsWithBlock:block];
  106. }
  107. - (void)removeAllObjectsWithProgressBlock:(void(^)(int removedCount, int totalCount))progress
  108. endBlock:(void(^)(BOOL error))end {
  109. [_memoryCache removeAllObjects];
  110. [_diskCache removeAllObjectsWithProgressBlock:progress endBlock:end];
  111. }
  112. - (NSString *)description {
  113. if (_name) return [[NSString alloc] initWithFormat:@"<%@: %p> (%@)", self.class, self, _name];
  114. else return [[NSString alloc] initWithFormat:@"<%@: %p>", self.class, self];
  115. }
  116. @end