sqlite 和 redis
This commit is contained in:
parent
63be2bc13f
commit
7c8fafc16e
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
cmd/testserver
|
||||
cmd/authlibskin/config.yaml
|
||||
cmd/authlibskin/skin
|
||||
cmd/authlibskin/authlibskin.exe
|
||||
|
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
@ -1,5 +1,5 @@
|
||||
{
|
||||
"gopls": {
|
||||
"buildFlags":["-tags=wireinject"]
|
||||
"buildFlags":["-tags=wireinject,redis,sqlite"]
|
||||
},
|
||||
}
|
@ -1,2 +1,2 @@
|
||||
SET CGO_ENABLED=0
|
||||
go build -trimpath -ldflags "-w -s"
|
||||
SET CGO_ENABLED=1
|
||||
go build -trimpath -ldflags "-w -s" -tags="redis,sqlite"
|
||||
|
@ -10,13 +10,19 @@ Log:
|
||||
json: false
|
||||
|
||||
sql:
|
||||
mysqlDsn: ""
|
||||
# 可填 mysql 或 sqlite3
|
||||
driverName: "sqlite3"
|
||||
# 填写见 mysql https://github.com/go-sql-driver/mysql#dsn-data-source-name
|
||||
# sqlite https://github.com/mattn/go-sqlite3
|
||||
# 例如 mysql `用户名:密码@tcp(mysqlIP:端口)/数据库名`
|
||||
# sqlite `data.db?_txlock=IMMEDIATE&_journal_mode=WAL&_fk=true``
|
||||
dsn: "data.db?_txlock=IMMEDIATE&_journal_mode=WAL&_fk=true"
|
||||
|
||||
# 输出每条执行的 sql 语句
|
||||
debug: false
|
||||
|
||||
cache:
|
||||
# 默认使用内存缓存,若需要集群部署,请更换 redis
|
||||
# 默认使用内存缓存,若需要集群部署,请填写 redis
|
||||
type: ""
|
||||
# 内存缓存使用大小,单位 b
|
||||
ram: 10000000
|
||||
|
@ -1,31 +1,41 @@
|
||||
package config
|
||||
|
||||
type Config struct {
|
||||
OfflineUUID bool `yaml:"offlineUUID"`
|
||||
Port string `yaml:"port"`
|
||||
Log struct {
|
||||
Level string `yaml:"level"`
|
||||
Json bool `yaml:"json"`
|
||||
} `yaml:"log"`
|
||||
Sql struct {
|
||||
MysqlDsn string `yaml:"mysqlDsn"`
|
||||
} `yaml:"sql"`
|
||||
Debug bool `yaml:"debug"`
|
||||
Cache struct {
|
||||
Type string `yaml:"type"`
|
||||
Ram int `yaml:"ram"`
|
||||
} `yaml:"cache"`
|
||||
RaelIP bool `yaml:"raelIP"`
|
||||
MaxIpUser int `yaml:"maxIpUser"`
|
||||
RsaPriKey string `yaml:"rsaPriKey"`
|
||||
TexturePath string `yaml:"texturePath"`
|
||||
TextureBaseUrl string `yaml:"textureBaseUrl"`
|
||||
WebBaseUrl string `yaml:"webBaseUrl"`
|
||||
ServerName string `yaml:"serverName"`
|
||||
|
||||
Captcha struct {
|
||||
Type string `yaml:"type"`
|
||||
SiteKey string `yaml:"siteKey"`
|
||||
Secret string `yaml:"ecret"`
|
||||
} `yaml:"captcha"`
|
||||
OfflineUUID bool `yaml:"offlineUUID"`
|
||||
Port string `yaml:"port"`
|
||||
Log Log `yaml:"log"`
|
||||
Sql Sql `yaml:"sql"`
|
||||
Debug bool `yaml:"debug"`
|
||||
Cache Cache `yaml:"cache"`
|
||||
RaelIP bool `yaml:"raelIP"`
|
||||
MaxIpUser int `yaml:"maxIpUser"`
|
||||
RsaPriKey string `yaml:"rsaPriKey"`
|
||||
TexturePath string `yaml:"texturePath"`
|
||||
TextureBaseUrl string `yaml:"textureBaseUrl"`
|
||||
WebBaseUrl string `yaml:"webBaseUrl"`
|
||||
ServerName string `yaml:"serverName"`
|
||||
Captcha Captcha `yaml:"captcha"`
|
||||
}
|
||||
|
||||
type Log struct {
|
||||
Level string `yaml:"level"`
|
||||
Json bool `yaml:"json"`
|
||||
}
|
||||
|
||||
type Sql struct {
|
||||
DriverName string `yaml:"driverName"`
|
||||
Dsn string `yaml:"dsn"`
|
||||
}
|
||||
|
||||
type Cache struct {
|
||||
Type string `yaml:"type"`
|
||||
Ram int `yaml:"ram"`
|
||||
Addr string `yaml:"addr"`
|
||||
Password string `yaml:"password"`
|
||||
}
|
||||
|
||||
type Captcha struct {
|
||||
Type string `yaml:"type"`
|
||||
SiteKey string `yaml:"siteKey"`
|
||||
Secret string `yaml:"ecret"`
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ Log:
|
||||
Json: false
|
||||
|
||||
sql:
|
||||
mysqlDsn: "123"
|
||||
dsn: "123"
|
||||
|
||||
# 输出每条执行的 sql 语句
|
||||
debug: false
|
||||
|
5
config/sqlite_init.go
Normal file
5
config/sqlite_init.go
Normal file
@ -0,0 +1,5 @@
|
||||
//go:build sqlite
|
||||
|
||||
package config
|
||||
|
||||
import _ "github.com/mattn/go-sqlite3"
|
@ -14,7 +14,7 @@ func TestYamlDeCode(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if c.Sql.MysqlDsn != "123" {
|
||||
if c.Sql.Dsn != "123" {
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
7
db/cache/no_redis.go
vendored
Normal file
7
db/cache/no_redis.go
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
//go:build !redis
|
||||
|
||||
package cache
|
||||
|
||||
func NewRedis(addr, pass string) Cache {
|
||||
panic("not tag redis")
|
||||
}
|
49
db/cache/redis.go
vendored
Normal file
49
db/cache/redis.go
vendored
Normal file
@ -0,0 +1,49 @@
|
||||
//go:build redis
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
var _ Cache = (*RedisCache)(nil)
|
||||
|
||||
type RedisCache struct {
|
||||
c *redis.Client
|
||||
}
|
||||
|
||||
func NewRedis(addr, pass string) Cache {
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: addr,
|
||||
Password: pass,
|
||||
})
|
||||
return &RedisCache{c: rdb}
|
||||
}
|
||||
|
||||
func (r *RedisCache) Del(k []byte) error {
|
||||
_, err := r.c.Del(context.Background(), string(k)).Result()
|
||||
if err != nil {
|
||||
return fmt.Errorf("RedisCache.Del: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *RedisCache) Get(k []byte) ([]byte, error) {
|
||||
value, err := r.c.Get(context.Background(), string(k)).Bytes()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("RedisCache.Get: %w", err)
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func (r *RedisCache) Put(k []byte, v []byte, timeOut time.Time) error {
|
||||
err := r.c.Set(context.Background(), string(k), v, timeOut.Sub(time.Now())).Err()
|
||||
if err != nil {
|
||||
return fmt.Errorf("RedisCache.Put: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
3
go.mod
3
go.mod
@ -23,6 +23,7 @@ require (
|
||||
github.com/agext/levenshtein v1.2.1 // indirect
|
||||
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.2.0 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
|
||||
github.com/go-openapi/inflect v0.19.0 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
@ -31,7 +32,9 @@ require (
|
||||
github.com/google/go-cmp v0.5.6 // indirect
|
||||
github.com/hashicorp/hcl/v2 v2.13.0 // indirect
|
||||
github.com/leodido/go-urn v1.2.4 // indirect
|
||||
github.com/mattn/go-sqlite3 v1.14.17 // indirect
|
||||
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 // indirect
|
||||
github.com/redis/go-redis/v9 v9.2.1 // indirect
|
||||
github.com/zclconf/go-cty v1.8.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
|
||||
golang.org/x/mod v0.10.0 // indirect
|
||||
|
6
go.sum
6
go.sum
@ -19,6 +19,8 @@ github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XL
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
|
||||
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
|
||||
github.com/go-chi/chi/v5 v5.0.10 h1:rLz5avzKpjqxrYwXNfmjkrYYXOyLJd37pz53UFHC6vk=
|
||||
@ -68,10 +70,14 @@ github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
|
||||
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
|
||||
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
|
||||
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
||||
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
|
||||
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
|
||||
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7 h1:DpOJ2HYzCv8LZP15IdmG+YdwD2luVPHITV96TkirNBM=
|
||||
github.com/mitchellh/go-wordwrap v0.0.0-20150314170334-ad45545899c7/go.mod h1:ZXFpozHsX6DPmq2I0TCekCxypsnAUbP2oI0UX1GXzOo=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/redis/go-redis/v9 v9.2.1 h1:WlYJg71ODF0dVspZZCpYmoF1+U1Jjk9Rwd7pq6QmlCg=
|
||||
github.com/redis/go-redis/v9 v9.2.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
|
||||
github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=
|
||||
github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
|
||||
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
|
||||
|
@ -8,6 +8,7 @@ import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
entsql "entgo.io/ent/dialect/sql"
|
||||
"github.com/go-playground/validator/v10"
|
||||
@ -48,17 +49,17 @@ func ProvideSlog(c config.Config) slog.Handler {
|
||||
}
|
||||
|
||||
func ProvideDB(c config.Config) (*sql.DB, func(), error) {
|
||||
db, err := sql.Open("mysql", c.Sql.MysqlDsn)
|
||||
db, err := sql.Open(c.Sql.DriverName, c.Sql.Dsn)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("ProvideDB: %w", err)
|
||||
}
|
||||
db.SetMaxOpenConns(20)
|
||||
db.SetMaxIdleConns(10)
|
||||
db.SetConnMaxIdleTime(2 * time.Minute)
|
||||
return db, func() { db.Close() }, nil
|
||||
}
|
||||
|
||||
func ProvideEnt(ctx context.Context, db *sql.DB, c config.Config, sl *slog.Logger) (*ent.Client, func(), error) {
|
||||
drv := entsql.OpenDB("mysql", db)
|
||||
drv := entsql.OpenDB(c.Sql.DriverName, db)
|
||||
opts := []ent.Option{ent.Driver(drv), ent.Log(
|
||||
func(a ...any) {
|
||||
sl.Debug(fmt.Sprint(a))
|
||||
@ -80,6 +81,9 @@ func ProvideValidate() *validator.Validate {
|
||||
}
|
||||
|
||||
func ProvideCache(c config.Config) cache.Cache {
|
||||
if c.Cache.Type == "redis" {
|
||||
return cache.NewRedis(c.Cache.Addr, c.Cache.Password)
|
||||
}
|
||||
return cache.NewFastCache(c.Cache.Ram)
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user