refactor: introduce Wire dependency injection and interface-based architecture
- Add Google Wire for compile-time dependency injection - Replace concrete service types with interfaces across handlers - Remove global state (DB, cache) in favor of constructor injection - Split monolithic files into focused modules: - config: separate files for each config domain - dto: converters split by domain (user, post, message, group) - cache: separate metrics.go and redis_cache.go - Introduce unified apperrors package with string-based error codes - Add transaction support with context-aware repository methods - Upgrade golang.org/x/crypto from 0.17.0 to 0.26.0
This commit is contained in:
70
internal/config/cache.go
Normal file
70
internal/config/cache.go
Normal file
@@ -0,0 +1,70 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user