// // KBAIChatMessageCacheManager.m // keyBoard // // Created by Mac on 2026/1/28. // #import "KBAIChatMessageCacheManager.h" #import "KBAiChatMessage.h" #import @interface KBAIChatMessageCacheManager () /// 消息缓存:Key = companionId,Value = messages 数组 @property (nonatomic, strong) NSCache *> *messageCache; @end @implementation KBAIChatMessageCacheManager #pragma mark - Singleton + (instancetype)shared { static KBAIChatMessageCacheManager *instance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance = [[self alloc] init]; }); return instance; } - (instancetype)init { if (self = [super init]) { _messageCache = [[NSCache alloc] init]; // 设置缓存上限,防止内存过大 _messageCache.countLimit = 20; // 最多缓存 20 个人设的消息 _messageCache.totalCostLimit = 10 * 1024 * 1024; // 最大 10MB // 监听内存警告,自动清理缓存 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleMemoryWarning) name:UIApplicationDidReceiveMemoryWarningNotification object:nil]; NSLog(@"[MessageCache] 缓存管理器初始化成功,限制:20个人设,最大10MB"); } return self; } #pragma mark - Public Methods - (void)saveMessages:(NSArray *)messages forCompanionId:(NSInteger)companionId { if (!messages || messages.count == 0) { NSLog(@"[MessageCache] 警告:尝试保存空消息数组,companionId=%ld", (long)companionId); return; } NSNumber *key = @(companionId); NSMutableArray *mutableMessages = [messages mutableCopy]; [self.messageCache setObject:mutableMessages forKey:key]; NSLog(@"[MessageCache] 保存成功:companionId=%ld, 消息数=%ld", (long)companionId, (long)messages.count); } - (NSArray *)messagesForCompanionId:(NSInteger)companionId { NSNumber *key = @(companionId); NSMutableArray *messages = [self.messageCache objectForKey:key]; if (messages) { NSLog(@"[MessageCache] 缓存命中:companionId=%ld, 消息数=%ld", (long)companionId, (long)messages.count); } else { NSLog(@"[MessageCache] 缓存未命中:companionId=%ld", (long)companionId); } return messages; } - (void)appendMessages:(NSArray *)messages forCompanionId:(NSInteger)companionId { if (!messages || messages.count == 0) { return; } NSNumber *key = @(companionId); NSMutableArray *existingMessages = [self.messageCache objectForKey:key]; if (existingMessages) { // 追加到现有消息数组 [existingMessages addObjectsFromArray:messages]; NSLog(@"[MessageCache] 追加消息:companionId=%ld, 新增=%ld, 总计=%ld", (long)companionId, (long)messages.count, (long)existingMessages.count); } else { // 如果缓存中没有,直接保存 [self saveMessages:messages forCompanionId:companionId]; } } - (void)clearMessagesForCompanionId:(NSInteger)companionId { NSNumber *key = @(companionId); [self.messageCache removeObjectForKey:key]; NSLog(@"[MessageCache] 清空缓存:companionId=%ld", (long)companionId); } - (void)clearAllMessages { [self.messageCache removeAllObjects]; NSLog(@"[MessageCache] 清空所有缓存"); } - (BOOL)hasCacheForCompanionId:(NSInteger)companionId { NSNumber *key = @(companionId); return [self.messageCache objectForKey:key] != nil; } #pragma mark - Memory Warning - (void)handleMemoryWarning { NSLog(@"[MessageCache] ⚠️ 收到内存警告,清空所有消息缓存"); [self clearAllMessages]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } @end