| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- //
- // JXCacheManager.m
- // AICity
- //
- // Created by TogetherWatch on 2025-10-13.
- //
- #import "JXCacheManager.h"
- @interface JXCacheManager ()
- @property (nonatomic, strong) NSCache *memoryCache;
- @property (nonatomic, strong) NSString *cacheDirectory;
- @end
- @implementation JXCacheManager
- + (instancetype)sharedManager {
- static JXCacheManager *instance = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- instance = [[self alloc] init];
- });
- return instance;
- }
- - (instancetype)init {
- self = [super init];
- if (self) {
- _memoryCache = [[NSCache alloc] init];
- _memoryCache.countLimit = 100; // 最多缓存100个对象
-
- // 创建缓存目录
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
- NSString *cachesDirectory = [paths firstObject];
- _cacheDirectory = [cachesDirectory stringByAppendingPathComponent:@"JuXing"];
-
- NSFileManager *fileManager = [NSFileManager defaultManager];
- if (![fileManager fileExistsAtPath:_cacheDirectory]) {
- [fileManager createDirectoryAtPath:_cacheDirectory
- withIntermediateDirectories:YES
- attributes:nil
- error:nil];
- }
- }
- return self;
- }
- #pragma mark - 分类缓存
- - (void)saveCategoryList:(NSArray *)categories {
- if (!categories) return;
-
- // 内存缓存
- [self.memoryCache setObject:categories forKey:@"category_list"];
-
- // 磁盘缓存
- NSString *filePath = [self.cacheDirectory stringByAppendingPathComponent:@"categories.plist"];
- [NSKeyedArchiver archiveRootObject:categories toFile:filePath];
- }
- - (NSArray *)getCategoryList {
- // 先从内存缓存获取
- NSArray *cached = [self.memoryCache objectForKey:@"category_list"];
- if (cached) {
- return cached;
- }
-
- // 从磁盘缓存获取
- NSString *filePath = [self.cacheDirectory stringByAppendingPathComponent:@"categories.plist"];
- NSArray *diskCached = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
- if (diskCached) {
- [self.memoryCache setObject:diskCached forKey:@"category_list"];
- return diskCached;
- }
-
- return nil;
- }
- #pragma mark - 短剧列表缓存
- - (void)saveDramaList:(NSArray *)dramas
- forCategory:(NSString *)categoryId
- page:(NSInteger)page {
- if (!dramas) return;
-
- NSString *key = [NSString stringWithFormat:@"drama_list_%@_%ld", categoryId ?: @"all", (long)page];
-
- // 内存缓存
- [self.memoryCache setObject:dramas forKey:key];
-
- // 磁盘缓存
- NSString *fileName = [NSString stringWithFormat:@"dramas_%@_p%ld.plist", categoryId ?: @"all", (long)page];
- NSString *filePath = [self.cacheDirectory stringByAppendingPathComponent:fileName];
- [NSKeyedArchiver archiveRootObject:dramas toFile:filePath];
- }
- - (NSArray *)getDramaListForCategory:(NSString *)categoryId
- page:(NSInteger)page {
- NSString *key = [NSString stringWithFormat:@"drama_list_%@_%ld", categoryId ?: @"all", (long)page];
-
- // 先从内存缓存获取
- NSArray *cached = [self.memoryCache objectForKey:key];
- if (cached) {
- return cached;
- }
-
- // 从磁盘缓存获取
- NSString *fileName = [NSString stringWithFormat:@"dramas_%@_p%ld.plist", categoryId ?: @"all", (long)page];
- NSString *filePath = [self.cacheDirectory stringByAppendingPathComponent:fileName];
- NSArray *diskCached = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
- if (diskCached) {
- [self.memoryCache setObject:diskCached forKey:key];
- return diskCached;
- }
-
- return nil;
- }
- #pragma mark - 短剧详情缓存
- - (void)saveDramaDetail:(NSDictionary *)detail
- forDramaId:(NSString *)dramaId {
- if (!detail || !dramaId) return;
-
- NSString *key = [NSString stringWithFormat:@"drama_detail_%@", dramaId];
-
- // 内存缓存
- [self.memoryCache setObject:detail forKey:key];
-
- // 磁盘缓存
- NSString *fileName = [NSString stringWithFormat:@"drama_%@.plist", dramaId];
- NSString *filePath = [self.cacheDirectory stringByAppendingPathComponent:fileName];
- [NSKeyedArchiver archiveRootObject:detail toFile:filePath];
- }
- - (NSDictionary *)getDramaDetailForDramaId:(NSString *)dramaId {
- if (!dramaId) return nil;
-
- NSString *key = [NSString stringWithFormat:@"drama_detail_%@", dramaId];
-
- // 先从内存缓存获取
- NSDictionary *cached = [self.memoryCache objectForKey:key];
- if (cached) {
- return cached;
- }
-
- // 从磁盘缓存获取
- NSString *fileName = [NSString stringWithFormat:@"drama_%@.plist", dramaId];
- NSString *filePath = [self.cacheDirectory stringByAppendingPathComponent:fileName];
- NSDictionary *diskCached = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
- if (diskCached) {
- [self.memoryCache setObject:diskCached forKey:key];
- return diskCached;
- }
-
- return nil;
- }
- #pragma mark - 缓存清理
- - (void)clearAllCache {
- // 清除内存缓存
- [self.memoryCache removeAllObjects];
-
- // 清除磁盘缓存
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSArray *files = [fileManager contentsOfDirectoryAtPath:self.cacheDirectory error:nil];
- for (NSString *file in files) {
- NSString *filePath = [self.cacheDirectory stringByAppendingPathComponent:file];
- [fileManager removeItemAtPath:filePath error:nil];
- }
-
- NSLog(@"剧星缓存已清除");
- }
- - (void)clearExpiredCache {
- // 简化实现:清除超过1天的缓存文件
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSArray *files = [fileManager contentsOfDirectoryAtPath:self.cacheDirectory error:nil];
- NSDate *now = [NSDate date];
-
- for (NSString *file in files) {
- NSString *filePath = [self.cacheDirectory stringByAppendingPathComponent:file];
- NSDictionary *attributes = [fileManager attributesOfItemAtPath:filePath error:nil];
- NSDate *modificationDate = attributes[NSFileModificationDate];
-
- if (modificationDate) {
- NSTimeInterval interval = [now timeIntervalSinceDate:modificationDate];
- if (interval > 86400) { // 超过1天
- [fileManager removeItemAtPath:filePath error:nil];
- }
- }
- }
-
- NSLog(@"过期缓存已清除");
- }
- @end
|