Skip to content

Commit

Permalink
make lint happy.
Browse files Browse the repository at this point in the history
  • Loading branch information
bugtower100 committed Jan 9, 2025
1 parent 2d5b85b commit a06131d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
12 changes: 10 additions & 2 deletions dice/model/database/cache/gormcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@ import (
"gorm.io/gorm"
)

// TODO: 采用context传参的方式,区分三种(四种?)不同的DB,从而细化缓存粒度
// TODO: 切换为otter缓存,那个缓存比这个好
type OtterDBCacher struct {
otter *otter.Cache[string, caches.Query[any]]
}

const (
CacheKey = "gorm_cache"
LogsDBCacheKey = "logs-db::"
DataDBCacheKey = "data-db::"
CensorsDBCacheKey = "censor-db::"
)

func (c *OtterDBCacher) getKeyWithCtx(ctx context.Context, key string) string {
// 获取ctx中的key字段
ctxCacheKey := fmt.Sprintf("%v", ctx.Value("gorm_cache"))
Expand Down Expand Up @@ -92,6 +97,9 @@ func GetOtterCacheDB(db *gorm.DB) (*gorm.DB, error) {
CollectStats().
WithTTL(time.Hour).
Build()
if err != nil {
return nil, err
}
cachesPlugin := &caches.Caches{Conf: &caches.Config{
Easer: true,
Cacher: &OtterDBCacher{
Expand Down
7 changes: 4 additions & 3 deletions dice/model/engine_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"gorm.io/gorm"

"sealdice-core/dice/model/database"
"sealdice-core/dice/model/database/cache"
log "sealdice-core/utils/kratos"
)

Expand Down Expand Up @@ -129,7 +130,7 @@ func (s *MYSQLEngine) DBCheck() {

// DataDBInit 初始化
func (s *MYSQLEngine) DataDBInit() (*gorm.DB, error) {
dataContext := context.WithValue(s.ctx, "gorm_cache", "data-db::")
dataContext := context.WithValue(s.ctx, cache.CacheKey, cache.DataDBCacheKey)
dataDB := s.DB.WithContext(dataContext)
err := dataDB.AutoMigrate(
// TODO: 这个的索引有没有必要进行修改
Expand All @@ -147,7 +148,7 @@ func (s *MYSQLEngine) DataDBInit() (*gorm.DB, error) {

func (s *MYSQLEngine) LogDBInit() (*gorm.DB, error) {
// logs特殊建表
logsContext := context.WithValue(s.ctx, "gorm_cache", "logs-db::")
logsContext := context.WithValue(s.ctx, cache.CacheKey, cache.LogsDBCacheKey)
logDB := s.DB.WithContext(logsContext)
if err := logDB.AutoMigrate(&LogInfoHookMySQL{}, &LogOneItemHookMySQL{}); err != nil {
return nil, err
Expand All @@ -165,7 +166,7 @@ func (s *MYSQLEngine) LogDBInit() (*gorm.DB, error) {
}

func (s *MYSQLEngine) CensorDBInit() (*gorm.DB, error) {
censorContext := context.WithValue(s.ctx, "gorm_cache", "censor-db::")
censorContext := context.WithValue(s.ctx, cache.CacheKey, cache.CensorsDBCacheKey)
censorDB := s.DB.WithContext(censorContext)
if err := censorDB.AutoMigrate(&CensorLog{}); err != nil {
return nil, err
Expand Down
7 changes: 4 additions & 3 deletions dice/model/engine_pgsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"gorm.io/gorm"

"sealdice-core/dice/model/database"
"sealdice-core/dice/model/database/cache"
)

type PGSQLEngine struct {
Expand Down Expand Up @@ -42,7 +43,7 @@ func (s *PGSQLEngine) DBCheck() {
// DataDBInit 初始化
func (s *PGSQLEngine) DataDBInit() (*gorm.DB, error) {
// data建表
dataContext := context.WithValue(s.ctx, "gorm_cache", "data-db::")
dataContext := context.WithValue(s.ctx, cache.CacheKey, cache.DataDBCacheKey)
dataDB := s.DB.WithContext(dataContext)
err := dataDB.AutoMigrate(
&GroupPlayerInfoBase{},
Expand All @@ -59,7 +60,7 @@ func (s *PGSQLEngine) DataDBInit() (*gorm.DB, error) {

func (s *PGSQLEngine) LogDBInit() (*gorm.DB, error) {
// logs建表
logsContext := context.WithValue(s.ctx, "gorm_cache", "logs-db::")
logsContext := context.WithValue(s.ctx, cache.CacheKey, cache.LogsDBCacheKey)
logDB := s.DB.WithContext(logsContext)
if err := logDB.AutoMigrate(&LogInfo{}, &LogOneItem{}); err != nil {
return nil, err
Expand All @@ -68,7 +69,7 @@ func (s *PGSQLEngine) LogDBInit() (*gorm.DB, error) {
}

func (s *PGSQLEngine) CensorDBInit() (*gorm.DB, error) {
censorContext := context.WithValue(s.ctx, "gorm_cache", "censor-db::")
censorContext := context.WithValue(s.ctx, cache.CacheKey, cache.CensorsDBCacheKey)
censorDB := s.DB.WithContext(censorContext)
// 创建基本的表结构,并通过标签定义索引
if err := censorDB.AutoMigrate(&CensorLog{}); err != nil {
Expand Down
7 changes: 4 additions & 3 deletions dice/model/engine_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"gorm.io/gorm"

"sealdice-core/dice/model/database"
"sealdice-core/dice/model/database/cache"
log "sealdice-core/utils/kratos"
)

Expand Down Expand Up @@ -130,7 +131,7 @@ func (s *SQLiteEngine) DataDBInit() (*gorm.DB, error) {
return nil, err
}
// 添加并设置context
dataContext := context.WithValue(s.ctx, "gorm_cache", "data-db::")
dataContext := context.WithValue(s.ctx, cache.CacheKey, cache.DataDBCacheKey)
dataDB = dataDB.WithContext(dataContext)
// 特殊情况建表语句处置
tx := dataDB.Begin()
Expand Down Expand Up @@ -191,7 +192,7 @@ func (s *SQLiteEngine) LogDBInit() (*gorm.DB, error) {
return nil, err
}
// 添加并设置context
logsContext := context.WithValue(s.ctx, "gorm_cache", "logs-db::")
logsContext := context.WithValue(s.ctx, cache.CacheKey, cache.LogsDBCacheKey)
logsDB = logsDB.WithContext(logsContext)
// logs建表
if err = logsDB.AutoMigrate(&LogInfo{}); err != nil {
Expand Down Expand Up @@ -225,7 +226,7 @@ func (s *SQLiteEngine) CensorDBInit() (*gorm.DB, error) {
return nil, err
}
// 添加并设置context
censorContext := context.WithValue(s.ctx, "gorm_cache", "censor-db::")
censorContext := context.WithValue(s.ctx, cache.CacheKey, cache.CensorsDBCacheKey)
censorDB = censorDB.WithContext(censorContext)
// 创建基本的表结构,并通过标签定义索引
if err = censorDB.AutoMigrate(&CensorLog{}); err != nil {
Expand Down

0 comments on commit a06131d

Please sign in to comment.