Files
keyboard/keyBoard/Class/AiTalk/M/KBAIChatMessageCacheManager.m
2026-01-28 18:58:30 +08:00

124 lines
4.0 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// KBAIChatMessageCacheManager.m
// keyBoard
//
// Created by Mac on 2026/1/28.
//
#import "KBAIChatMessageCacheManager.h"
#import "KBAiChatMessage.h"
#import <UIKit/UIKit.h>
@interface KBAIChatMessageCacheManager ()
/// 消息缓存Key = companionIdValue = messages 数组
@property (nonatomic, strong) NSCache<NSNumber *, NSMutableArray<KBAiChatMessage *> *> *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<KBAiChatMessage *> *)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<KBAiChatMessage *> *)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<KBAiChatMessage *> *)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