UNPKG

sim-sdk-web

Version:

Sim SDK for Web

180 lines (179 loc) 4.6 kB
import { ConversationItem, MessageItem } from '../types/entity'; declare const DEFAULT_CONFIG: { MAX_CONVERSATIONS: number; MAX_GROUP_MESSAGES: number; STORAGE_CHUNK_SIZE: number; CACHE_TTL: number; EXPIRY_CHECK_INTERVAL: number; }; /** * 缓存管理器 - 负责内存缓存和持久化存储的管理 */ export declare class CacheManager { private conversationCache; private groupMessageCache; private accessTimeRecord; private userInfo; private activeConversation?; private metaInfo; private persistEnabled; private debug; private config; private cleanupTimer?; /** * 创建缓存管理器实例 * @param options 选项 */ constructor({ persistEnabled, debug, config }?: { persistEnabled?: boolean; debug?: boolean; config?: Partial<typeof DEFAULT_CONFIG>; }); /** * 初始化缓存 * @param username 当前用户名 */ initialize(username?: string): void; /** * 清除所有缓存 */ clear(): void; /** * 获取会话缓存 */ getConversations(): ConversationItem[]; /** * 清除指定会话的未读数 * @param conversationId 会话ID */ clearConversationUnread(conversationId: number): void; /** * 清除所有会话的未读数 */ clearAllConversationUnread(): void; /** * 清除会话 */ getActiveConversation(): ConversationItem | undefined; /** * 设置会话缓存 * @param conversations 会话列表 */ setConversations(conversations: ConversationItem[]): void; /** * 添加或更新会话 * @param conversation 会话项 */ updateConversation(conversation: ConversationItem): void; /** * 使用消息更新会话 * @param message 消息 * @returns 是否有更新 */ updateConversationWithMessage(message: MessageItem, username?: string): boolean; /** * 获取群组消息 * @param groupId 群组ID */ getGroupMessages(groupId: number): MessageItem[]; /** * 添加群组消息 * @param message 消息 */ addGroupMessage(message: MessageItem): void; /** * 批量添加群组消息 * @param messages 消息数组 */ addGroupMessages(messages: MessageItem[]): void; /** * 对会话按最新消息时间排序 */ private sortConversations; /** * 对群组消息按时间排序 * @param groupId 群组ID */ private sortGroupMessages; /** * 限制群组消息缓存大小 * @param groupId 群组ID */ private limitGroupMessageCache; /** * 记录访问时间(用于LRU算法) * @param groupId 群组ID */ private recordAccess; /** * 强制限制会话数量 */ private enforceConversationLimit; /** * 强制内存限制 - 可能从内存中移除不活跃的群组消息 */ private enforceMemoryLimits; /** * 设置定期清理过期数据的定时器 */ private setupCleanupInterval; /** * 清理过期数据 */ private cleanupExpiredData; /** * 从本地存储加载数据 */ private loadFromStorage; /** * 从存储中加载特定群组的消息 * @param groupId 群组ID */ private loadGroupMessagesFromStorage; /** * 保存会话列表到本地存储 */ private saveConversationsToStorage; /** * 保存群组消息到本地存储 * @param groupId 如果提供,只保存指定群组的消息 */ private saveGroupMessagesToStorage; /** * 保存元数据到本地存储 */ private saveMetaInfoToStorage; /** * 处理存储错误,主要处理配额超出问题 */ private handleStorageError; /** * 清除特定群组的消息存储 * @param groupId 群组ID */ private clearGroupMessageStorage; /** * 保存用户信息到本地存储 */ private saveUserInfoToStorage; /** * 清除本地存储 */ private clearStorage; /** * 析构函数 - 清理定时器 */ destroy(): void; /** * 标记会话为活跃状态 * @param conversationId 会话ID * @returns 是否成功找到并标记会话 */ markConversationAsActive(conversationId: number): boolean; /** * 重置当前活跃会话 * @returns 是否成功重置(如果之前有活跃会话则为true) */ resetActiveConversation(): boolean; } export {};