Files
backend/internal/config/redis.go
lafay 98f0c9f2b6
All checks were successful
Build Backend / build (push) Successful in 5m54s
Build Backend / build-docker (push) Successful in 4m7s
feat: add hook system, QR code login, and layered cache
- Implement extensible hook system for content moderation with
  builtin and AI-powered moderation hooks
- Add QR code login feature with SSE for real-time status updates
  and scan/confirm/cancel workflow
- Introduce layered cache with local LRU + Redis backend for
  improved read performance
- Refactor post and comment services to use hook-based moderation
  instead of direct AI service calls
- Add local cache configuration options (size, buckets, ttl)
2026-03-20 12:23:28 +08:00

81 lines
2.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package config
import (
"context"
"fmt"
"github.com/redis/go-redis/v9"
)
// RedisConfig Redis 配置
type RedisConfig struct {
Type string `mapstructure:"type"`
Redis RedisServerConfig `mapstructure:"redis"`
Miniredis MiniredisConfig `mapstructure:"miniredis"`
PoolSize int `mapstructure:"pool_size"`
}
// CacheConfig 缓存配置
type CacheConfig struct {
Enabled bool `mapstructure:"enabled"`
KeyPrefix string `mapstructure:"key_prefix"`
DefaultTTL int `mapstructure:"default_ttl"`
NullTTL int `mapstructure:"null_ttl"`
JitterRatio float64 `mapstructure:"jitter_ratio"`
DisableFlushDB bool `mapstructure:"disable_flushdb"`
Modules CacheModuleTTL `mapstructure:"modules"`
Local LocalCacheConfig `mapstructure:"local"`
}
// LocalCacheConfig 本地缓存配置
type LocalCacheConfig struct {
Enabled bool `mapstructure:"enabled"`
Size int `mapstructure:"size"`
Buckets int `mapstructure:"buckets"`
DefaultTTL int `mapstructure:"default_ttl"`
}
// CacheModuleTTL 缓存模块 TTL 配置
type CacheModuleTTL struct {
PostList int `mapstructure:"post_list_ttl"`
Conversation int `mapstructure:"conversation_ttl"`
UnreadCount int `mapstructure:"unread_count_ttl"`
GroupMembers int `mapstructure:"group_members_ttl"`
}
// RedisServerConfig Redis 服务器配置
type RedisServerConfig struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
Password string `mapstructure:"password"`
DB int `mapstructure:"db"`
}
// Addr 返回 Redis 服务器地址
func (r RedisServerConfig) Addr() string {
return fmt.Sprintf("%s:%d", r.Host, r.Port)
}
// MiniredisConfig Miniredis 配置(用于测试)
type MiniredisConfig struct {
Host string `mapstructure:"host"`
Port int `mapstructure:"port"`
}
// NewRedis 创建 Redis 客户端(真实 Redis
func NewRedis(cfg *RedisConfig) (*redis.Client, error) {
client := redis.NewClient(&redis.Options{
Addr: cfg.Redis.Addr(),
Password: cfg.Redis.Password,
DB: cfg.Redis.DB,
PoolSize: cfg.PoolSize,
})
ctx := context.Background()
if err := client.Ping(ctx).Err(); err != nil {
return nil, fmt.Errorf("failed to connect to redis: %w", err)
}
return client, nil
}