1
This commit is contained in:
49
keyBoard/Class/AiTalk/M/KBAIChatMessageCacheManager.h
Normal file
49
keyBoard/Class/AiTalk/M/KBAIChatMessageCacheManager.h
Normal file
@@ -0,0 +1,49 @@
|
||||
//
|
||||
// KBAIChatMessageCacheManager.h
|
||||
// keyBoard
|
||||
//
|
||||
// Created by Mac on 2026/1/28.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
@class KBAiChatMessage;
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
/// 聊天消息缓存管理器
|
||||
/// 用于避免重复请求消息数据,提升性能
|
||||
@interface KBAIChatMessageCacheManager : NSObject
|
||||
|
||||
/// 单例
|
||||
+ (instancetype)shared;
|
||||
|
||||
/// 保存消息到缓存
|
||||
/// @param messages 消息数组
|
||||
/// @param companionId 人设 ID
|
||||
- (void)saveMessages:(NSArray<KBAiChatMessage *> *)messages forCompanionId:(NSInteger)companionId;
|
||||
|
||||
/// 从缓存获取消息
|
||||
/// @param companionId 人设 ID
|
||||
/// @return 消息数组,如果缓存中没有则返回 nil
|
||||
- (NSArray<KBAiChatMessage *> * _Nullable)messagesForCompanionId:(NSInteger)companionId;
|
||||
|
||||
/// 追加消息到缓存(用于上拉加载更多历史消息)
|
||||
/// @param messages 新消息数组
|
||||
/// @param companionId 人设 ID
|
||||
- (void)appendMessages:(NSArray<KBAiChatMessage *> *)messages forCompanionId:(NSInteger)companionId;
|
||||
|
||||
/// 清空指定人设的消息缓存
|
||||
/// @param companionId 人设 ID
|
||||
- (void)clearMessagesForCompanionId:(NSInteger)companionId;
|
||||
|
||||
/// 清空所有消息缓存
|
||||
- (void)clearAllMessages;
|
||||
|
||||
/// 获取缓存状态
|
||||
/// @param companionId 人设 ID
|
||||
/// @return 是否有缓存
|
||||
- (BOOL)hasCacheForCompanionId:(NSInteger)companionId;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
123
keyBoard/Class/AiTalk/M/KBAIChatMessageCacheManager.m
Normal file
123
keyBoard/Class/AiTalk/M/KBAIChatMessageCacheManager.m
Normal 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 = companionId,Value = 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
|
||||
35
keyBoard/Class/AiTalk/M/KBChatSessionResetModel.h
Normal file
35
keyBoard/Class/AiTalk/M/KBChatSessionResetModel.h
Normal file
@@ -0,0 +1,35 @@
|
||||
//
|
||||
// KBChatSessionResetModel.h
|
||||
// keyBoard
|
||||
//
|
||||
// Created by Mac on 2026/1/28.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
/// 重置会话响应数据
|
||||
@interface KBChatSessionResetData : NSObject
|
||||
|
||||
/// 会话 ID
|
||||
@property (nonatomic, assign) NSInteger sessionId;
|
||||
/// AI 角色 ID
|
||||
@property (nonatomic, assign) NSInteger companionId;
|
||||
/// 重置版本号
|
||||
@property (nonatomic, assign) NSInteger resetVersion;
|
||||
/// 创建时间
|
||||
@property (nonatomic, copy) NSString *createdAt;
|
||||
|
||||
@end
|
||||
|
||||
/// 重置会话响应
|
||||
@interface KBChatSessionResetResponse : NSObject
|
||||
|
||||
@property (nonatomic, assign) NSInteger code;
|
||||
@property (nonatomic, strong, nullable) KBChatSessionResetData *data;
|
||||
@property (nonatomic, copy, nullable) NSString *message;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
16
keyBoard/Class/AiTalk/M/KBChatSessionResetModel.m
Normal file
16
keyBoard/Class/AiTalk/M/KBChatSessionResetModel.m
Normal file
@@ -0,0 +1,16 @@
|
||||
//
|
||||
// KBChatSessionResetModel.m
|
||||
// keyBoard
|
||||
//
|
||||
// Created by Mac on 2026/1/28.
|
||||
//
|
||||
|
||||
#import "KBChatSessionResetModel.h"
|
||||
|
||||
@implementation KBChatSessionResetData
|
||||
|
||||
@end
|
||||
|
||||
@implementation KBChatSessionResetResponse
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user