JXCacheManager.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // JXCacheManager.m
  3. // AICity
  4. //
  5. // Created by TogetherWatch on 2025-10-13.
  6. //
  7. #import "JXCacheManager.h"
  8. @interface JXCacheManager ()
  9. @property (nonatomic, strong) NSCache *memoryCache;
  10. @property (nonatomic, strong) NSString *cacheDirectory;
  11. @end
  12. @implementation JXCacheManager
  13. + (instancetype)sharedManager {
  14. static JXCacheManager *instance = nil;
  15. static dispatch_once_t onceToken;
  16. dispatch_once(&onceToken, ^{
  17. instance = [[self alloc] init];
  18. });
  19. return instance;
  20. }
  21. - (instancetype)init {
  22. self = [super init];
  23. if (self) {
  24. _memoryCache = [[NSCache alloc] init];
  25. _memoryCache.countLimit = 100; // 最多缓存100个对象
  26. // 创建缓存目录
  27. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  28. NSString *cachesDirectory = [paths firstObject];
  29. _cacheDirectory = [cachesDirectory stringByAppendingPathComponent:@"JuXing"];
  30. NSFileManager *fileManager = [NSFileManager defaultManager];
  31. if (![fileManager fileExistsAtPath:_cacheDirectory]) {
  32. [fileManager createDirectoryAtPath:_cacheDirectory
  33. withIntermediateDirectories:YES
  34. attributes:nil
  35. error:nil];
  36. }
  37. }
  38. return self;
  39. }
  40. #pragma mark - 分类缓存
  41. - (void)saveCategoryList:(NSArray *)categories {
  42. if (!categories) return;
  43. // 内存缓存
  44. [self.memoryCache setObject:categories forKey:@"category_list"];
  45. // 磁盘缓存
  46. NSString *filePath = [self.cacheDirectory stringByAppendingPathComponent:@"categories.plist"];
  47. [NSKeyedArchiver archiveRootObject:categories toFile:filePath];
  48. }
  49. - (NSArray *)getCategoryList {
  50. // 先从内存缓存获取
  51. NSArray *cached = [self.memoryCache objectForKey:@"category_list"];
  52. if (cached) {
  53. return cached;
  54. }
  55. // 从磁盘缓存获取
  56. NSString *filePath = [self.cacheDirectory stringByAppendingPathComponent:@"categories.plist"];
  57. NSArray *diskCached = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
  58. if (diskCached) {
  59. [self.memoryCache setObject:diskCached forKey:@"category_list"];
  60. return diskCached;
  61. }
  62. return nil;
  63. }
  64. #pragma mark - 短剧列表缓存
  65. - (void)saveDramaList:(NSArray *)dramas
  66. forCategory:(NSString *)categoryId
  67. page:(NSInteger)page {
  68. if (!dramas) return;
  69. NSString *key = [NSString stringWithFormat:@"drama_list_%@_%ld", categoryId ?: @"all", (long)page];
  70. // 内存缓存
  71. [self.memoryCache setObject:dramas forKey:key];
  72. // 磁盘缓存
  73. NSString *fileName = [NSString stringWithFormat:@"dramas_%@_p%ld.plist", categoryId ?: @"all", (long)page];
  74. NSString *filePath = [self.cacheDirectory stringByAppendingPathComponent:fileName];
  75. [NSKeyedArchiver archiveRootObject:dramas toFile:filePath];
  76. }
  77. - (NSArray *)getDramaListForCategory:(NSString *)categoryId
  78. page:(NSInteger)page {
  79. NSString *key = [NSString stringWithFormat:@"drama_list_%@_%ld", categoryId ?: @"all", (long)page];
  80. // 先从内存缓存获取
  81. NSArray *cached = [self.memoryCache objectForKey:key];
  82. if (cached) {
  83. return cached;
  84. }
  85. // 从磁盘缓存获取
  86. NSString *fileName = [NSString stringWithFormat:@"dramas_%@_p%ld.plist", categoryId ?: @"all", (long)page];
  87. NSString *filePath = [self.cacheDirectory stringByAppendingPathComponent:fileName];
  88. NSArray *diskCached = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
  89. if (diskCached) {
  90. [self.memoryCache setObject:diskCached forKey:key];
  91. return diskCached;
  92. }
  93. return nil;
  94. }
  95. #pragma mark - 短剧详情缓存
  96. - (void)saveDramaDetail:(NSDictionary *)detail
  97. forDramaId:(NSString *)dramaId {
  98. if (!detail || !dramaId) return;
  99. NSString *key = [NSString stringWithFormat:@"drama_detail_%@", dramaId];
  100. // 内存缓存
  101. [self.memoryCache setObject:detail forKey:key];
  102. // 磁盘缓存
  103. NSString *fileName = [NSString stringWithFormat:@"drama_%@.plist", dramaId];
  104. NSString *filePath = [self.cacheDirectory stringByAppendingPathComponent:fileName];
  105. [NSKeyedArchiver archiveRootObject:detail toFile:filePath];
  106. }
  107. - (NSDictionary *)getDramaDetailForDramaId:(NSString *)dramaId {
  108. if (!dramaId) return nil;
  109. NSString *key = [NSString stringWithFormat:@"drama_detail_%@", dramaId];
  110. // 先从内存缓存获取
  111. NSDictionary *cached = [self.memoryCache objectForKey:key];
  112. if (cached) {
  113. return cached;
  114. }
  115. // 从磁盘缓存获取
  116. NSString *fileName = [NSString stringWithFormat:@"drama_%@.plist", dramaId];
  117. NSString *filePath = [self.cacheDirectory stringByAppendingPathComponent:fileName];
  118. NSDictionary *diskCached = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
  119. if (diskCached) {
  120. [self.memoryCache setObject:diskCached forKey:key];
  121. return diskCached;
  122. }
  123. return nil;
  124. }
  125. #pragma mark - 缓存清理
  126. - (void)clearAllCache {
  127. // 清除内存缓存
  128. [self.memoryCache removeAllObjects];
  129. // 清除磁盘缓存
  130. NSFileManager *fileManager = [NSFileManager defaultManager];
  131. NSArray *files = [fileManager contentsOfDirectoryAtPath:self.cacheDirectory error:nil];
  132. for (NSString *file in files) {
  133. NSString *filePath = [self.cacheDirectory stringByAppendingPathComponent:file];
  134. [fileManager removeItemAtPath:filePath error:nil];
  135. }
  136. NSLog(@"剧星缓存已清除");
  137. }
  138. - (void)clearExpiredCache {
  139. // 简化实现:清除超过1天的缓存文件
  140. NSFileManager *fileManager = [NSFileManager defaultManager];
  141. NSArray *files = [fileManager contentsOfDirectoryAtPath:self.cacheDirectory error:nil];
  142. NSDate *now = [NSDate date];
  143. for (NSString *file in files) {
  144. NSString *filePath = [self.cacheDirectory stringByAppendingPathComponent:file];
  145. NSDictionary *attributes = [fileManager attributesOfItemAtPath:filePath error:nil];
  146. NSDate *modificationDate = attributes[NSFileModificationDate];
  147. if (modificationDate) {
  148. NSTimeInterval interval = [now timeIntervalSinceDate:modificationDate];
  149. if (interval > 86400) { // 超过1天
  150. [fileManager removeItemAtPath:filePath error:nil];
  151. }
  152. }
  153. }
  154. NSLog(@"过期缓存已清除");
  155. }
  156. @end