package config import "time" // ConversationCacheConfig 会话缓存配置 type ConversationCacheConfig struct { // TTL 配置 DetailTTL string `mapstructure:"detail_ttl"` ListTTL string `mapstructure:"list_ttl"` ParticipantTTL string `mapstructure:"participant_ttl"` UnreadTTL string `mapstructure:"unread_ttl"` // 消息缓存配置 MessageDetailTTL string `mapstructure:"message_detail_ttl"` MessageListTTL string `mapstructure:"message_list_ttl"` MessageIndexTTL string `mapstructure:"message_index_ttl"` MessageCountTTL string `mapstructure:"message_count_ttl"` // 批量写入配置 BatchInterval string `mapstructure:"batch_interval"` BatchThreshold int `mapstructure:"batch_threshold"` BatchMaxSize int `mapstructure:"batch_max_size"` BufferMaxSize int `mapstructure:"buffer_max_size"` } // ConversationCacheSettings 会话缓存运行时配置(用于传递给 cache 包) type ConversationCacheSettings struct { DetailTTL time.Duration ListTTL time.Duration ParticipantTTL time.Duration UnreadTTL time.Duration MessageDetailTTL time.Duration MessageListTTL time.Duration MessageIndexTTL time.Duration MessageCountTTL time.Duration BatchInterval time.Duration BatchThreshold int BatchMaxSize int BufferMaxSize int } // ToSettings 将 ConversationCacheConfig 转换为 ConversationCacheSettings func (c *ConversationCacheConfig) ToSettings() *ConversationCacheSettings { return &ConversationCacheSettings{ DetailTTL: parseDuration(c.DetailTTL, 5*time.Minute), ListTTL: parseDuration(c.ListTTL, 60*time.Second), ParticipantTTL: parseDuration(c.ParticipantTTL, 5*time.Minute), UnreadTTL: parseDuration(c.UnreadTTL, 30*time.Second), MessageDetailTTL: parseDuration(c.MessageDetailTTL, 30*time.Minute), MessageListTTL: parseDuration(c.MessageListTTL, 5*time.Minute), MessageIndexTTL: parseDuration(c.MessageIndexTTL, 30*time.Minute), MessageCountTTL: parseDuration(c.MessageCountTTL, 30*time.Minute), BatchInterval: parseDuration(c.BatchInterval, 5*time.Second), BatchThreshold: c.BatchThreshold, BatchMaxSize: c.BatchMaxSize, BufferMaxSize: c.BufferMaxSize, } } // parseDuration 解析持续时间字符串,如果解析失败则返回默认值 func parseDuration(s string, defaultVal time.Duration) time.Duration { if s == "" { return defaultVal } d, err := time.ParseDuration(s) if err != nil { return defaultVal } return d }