This commit is contained in:
2026-01-28 18:58:30 +08:00
parent 93a20cd92a
commit 66d85f78a0
8 changed files with 310 additions and 8 deletions

View File

@@ -0,0 +1,123 @@
//
// 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