修改成redis

This commit is contained in:
2026-04-20 18:26:54 +08:00
parent e944a25e56
commit a9cb0b2481
17 changed files with 2408 additions and 933 deletions
+193
View File
@@ -23,6 +23,8 @@ type Config struct {
Search SearchConfig `yaml:"search"`
Backlink BacklinkConfig `yaml:"backlink"`
Storage StorageConfig `yaml:"storage"`
MySQL MySQLConfig `yaml:"mysql"`
Redis RedisConfig `yaml:"redis"`
Prometheus PrometheusConfig `yaml:"prometheus"`
}
@@ -77,6 +79,62 @@ type StorageConfig struct {
Path string `yaml:"path"`
}
// MySQLConfig MySQL数据库连接配置
// 支持两种连接方式:Unix Socket 和 TCP
// 优先级:UnixSocket > TCP(如果UnixSocket非空则优先使用)
type MySQLConfig struct {
// 是否启用 MySQL(默认关闭)
Enabled bool `yaml:"enabled"`
// 连接方式: "socket" 或 "tcp"(自动推断,可不填)
Network string `yaml:"network"`
// Unix Socket 路径(Linux/macOS),优先使用
// 示例: "/var/run/mysqld/mysqld.sock" 或 "/tmp/mysql.sock"
UnixSocket string `yaml:"unix_socket"`
// TCP 连接方式:服务器地址
Host string `yaml:"host"`
// TCP 连接方式:端口号
Port int `yaml:"port"`
// 用户名
User string `yaml:"user"`
// 密码
Password string `yaml:"password"`
// 数据库名
Database string `yaml:"database"`
// 连接超时时间(秒)
ConnMaxLifetime int `yaml:"conn_max_lifetime"` // 秒
// 最大空闲连接数
MaxIdleConns int `yaml:"max_idle_conns"`
// 最大打开连接数
MaxOpenConns int `yaml:"max_open_conns"`
}
// RedisConfig Redis连接配置
// 支持两种连接方式:Unix Socket 和 TCP
// 优先级:UnixSocket > TCP(如果UnixSocket非空则优先使用)
type RedisConfig struct {
// 连接方式: "socket" 或 "tcp"(自动推断,可不填)
Network string `yaml:"network"`
// Unix Socket 路径,优先使用
// 示例: "/var/run/redis/redis.sock" 或 "/tmp/redis.sock"
UnixSocket string `yaml:"unix_socket"`
// TCP 连接方式:服务器地址
Host string `yaml:"host"`
// TCP 连接方式:端口号
Port int `yaml:"port"`
// 密码(无密码则留空)
Password string `yaml:"password"`
// 数据库编号(0-15),默认 15
DB int `yaml:"db"`
// 池大小(最大连接数)
PoolSize int `yaml:"pool_size"`
// 最小空闲连接数
MinIdleConns int `yaml:"min_idle_conns"`
// 读超时时间(毫秒)
ReadTimeout int `yaml:"read_timeout"` // 毫秒
// 写超时时间(毫秒)
WriteTimeout int `yaml:"write_timeout"` // 毫秒
}
// PrometheusConfig Prometheus监控端口配置
type PrometheusConfig struct {
CrawlerPort int `yaml:"crawler_port"`
@@ -217,6 +275,31 @@ func GetDefaultConfig() Config {
Storage: StorageConfig{
Path: "./savedata",
},
MySQL: MySQLConfig{
Enabled: false,
Network: "tcp",
UnixSocket: "",
Host: "localhost",
Port: 3306,
User: "root",
Password: "",
Database: "sese_engine",
ConnMaxLifetime: 3600, // 1小时
MaxIdleConns: 10,
MaxOpenConns: 100,
},
Redis: RedisConfig{
Network: "tcp",
UnixSocket: "",
Host: "localhost",
Port: 6379,
Password: "",
DB: 15, // 默认使用15号数据库
PoolSize: 100,
MinIdleConns: 10,
ReadTimeout: 500, // 毫秒
WriteTimeout: 500, // 毫秒
},
Prometheus: PrometheusConfig{
CrawlerPort: 14950,
BacklinkPort: 14952,
@@ -357,3 +440,113 @@ func MaxPriorityChildren() int {
// 为了向后兼容,保留 StoragePath 常量
const StoragePath = "./savedata"
// ---- MySQL 配置访问函数 ----
// MySQLEnabled 返回是否启用 MySQL(默认关闭)
func MySQLEnabled() bool {
return Global.MySQL.Enabled
}
// MySQLDSN 返回 MySQL 连接字符串(DSN)
// 根据配置自动选择 Unix Socket 或 TCP 方式
func MySQLDSN() string {
cfg := Global.MySQL
if cfg.UnixSocket != "" {
// 使用 Unix Socket 连接(推荐,本地连接性能更好)
return cfg.User + ":" + cfg.Password + "@unix(" + cfg.UnixSocket + ")/" + cfg.Database + "?parseTime=true&loc=Local"
}
// 使用 TCP 连接
return cfg.User + ":" + cfg.Password + "@tcp(" + cfg.Host + ":" + itoa(cfg.Port) + ")/" + cfg.Database + "?parseTime=true&loc=Local"
}
// MySQLConnMaxLifetime 返回连接最大生命周期(秒)
func MySQLConnMaxLifetime() int {
if Global.MySQL.ConnMaxLifetime <= 0 {
return 3600
}
return Global.MySQL.ConnMaxLifetime
}
// MySQLMaxIdleConns 返回最大空闲连接数
func MySQLMaxIdleConns() int {
if Global.MySQL.MaxIdleConns <= 0 {
return 10
}
return Global.MySQL.MaxIdleConns
}
// MySQLMaxOpenConns 返回最大打开连接数
func MySQLMaxOpenConns() int {
if Global.MySQL.MaxOpenConns <= 0 {
return 100
}
return Global.MySQL.MaxOpenConns
}
// ---- Redis 配置访问函数 ----
// RedisAddr 返回 Redis 连接地址
// 根据配置自动选择 Unix Socket 或 TCP 方式
func RedisAddr() string {
cfg := Global.Redis
if cfg.UnixSocket != "" {
return cfg.UnixSocket
}
return cfg.Host + ":" + itoa(cfg.Port)
}
// RedisPoolSize 返回连接池大小
func RedisPoolSize() int {
if Global.Redis.PoolSize <= 0 {
return 100
}
return Global.Redis.PoolSize
}
// RedisMinIdleConns 返回最小空闲连接数
func RedisMinIdleConns() int {
if Global.Redis.MinIdleConns <= 0 {
return 10
}
return Global.Redis.MinIdleConns
}
// RedisDB 返回数据库编号
func RedisDB() int {
return Global.Redis.DB
}
// RedisPassword 返回密码(空字符串表示无密码)
func RedisPassword() string {
return Global.Redis.Password
}
// RedisReadTimeout 返回读超时时间(毫秒)
func RedisReadTimeout() int {
if Global.Redis.ReadTimeout <= 0 {
return 500
}
return Global.Redis.ReadTimeout
}
// RedisWriteTimeout 返回写超时时间(毫秒)
func RedisWriteTimeout() int {
if Global.Redis.WriteTimeout <= 0 {
return 500
}
return Global.Redis.WriteTimeout
}
// itoa 将 int 转换为字符串(避免导入 strconv)
func itoa(n int) string {
if n == 0 {
return "0"
}
result := ""
for n > 0 {
result = string(rune('0'+n%10)) + result
n /= 10
}
return result
}