feat 修改usn类型为string

This commit is contained in:
2026-01-11 16:16:27 +08:00
parent 1b893cad87
commit 11141edcda
17 changed files with 1019 additions and 43 deletions

View File

@@ -0,0 +1,21 @@
package repository
import (
"fmt"
"git.hlsq.asia/mmorpg/service-common/db/mysql"
)
var dbName mysql.DBName = "user_db"
var (
cacheBySn = "c:%v:s:%v"
userCacheByPhone = "c:user:p:%v"
)
func keyCacheBySn(sn string, tableName string) string {
return fmt.Sprintf(cacheBySn, tableName, sn)
}
func keyUserCacheByPhone(phone string) string {
return fmt.Sprintf(userCacheByPhone, phone)
}

View File

@@ -0,0 +1,83 @@
package repository
import (
"context"
"git.hlsq.asia/mmorpg/service-common/db/mysql"
"git.hlsq.asia/mmorpg/service-common/db/redis"
"git.hlsq.asia/mmorpg/service-common/utils"
"git.hlsq.asia/mmorpg/service-user/internal/dao/model"
"git.hlsq.asia/mmorpg/service-user/internal/dao/query"
"gorm.io/gorm"
"time"
)
type QuestionDao struct {
ctx context.Context
query *query.Query
cache *redis.CacheClient
}
func NewQuestionDao(ctx context.Context, cache ...*redis.CacheClient) *QuestionDao {
dao := &QuestionDao{
ctx: ctx,
query: query.Use(mysql.GetDB(dbName)),
}
if len(cache) > 0 {
dao.cache = cache[0]
}
return dao
}
func (d *QuestionDao) Create(question *model.Question) error {
err := d.query.Question.WithContext(d.ctx).
Create(question)
return err
}
func (d *QuestionDao) FindByRandom() (*model.Question, error) {
count, err := d.query.Question.WithContext(d.ctx).Count()
if err != nil {
return nil, err
}
if count == 0 {
return nil, gorm.ErrRecordNotFound
}
first, err := d.query.Question.WithContext(d.ctx).
Offset(utils.RandInt(0, int(count-1))).
First()
if err != nil {
return nil, err
}
return first, nil
}
func (d *QuestionDao) FindBySn(sn string) (*model.Question, error) {
if d.cache != nil {
var question model.Question
if ok := d.cache.Get(d.ctx, keyCacheBySn(sn, question.TableName()), &question); ok {
return &question, nil
}
}
first, err := d.query.Question.WithContext(d.ctx).
Where(d.query.Question.Sn.Eq(sn)).
First()
if err != nil {
return nil, err
}
if d.cache != nil {
d.cache.Set(d.ctx, keyCacheBySn(sn, first.TableName()), first, 5*time.Minute)
}
return first, nil
}
func (d *QuestionDao) FindCategory() ([]string, error) {
var categories []string
err := d.query.Question.WithContext(d.ctx).
Select(d.query.Question.Category).
Distinct().
Scan(&categories)
if err != nil {
return nil, err
}
return categories, nil
}

View File

@@ -2,7 +2,6 @@ package repository
import (
"context"
"fmt"
"git.hlsq.asia/mmorpg/service-common/db/mysql"
"git.hlsq.asia/mmorpg/service-common/db/redis"
"git.hlsq.asia/mmorpg/service-user/internal/dao/model"
@@ -10,11 +9,6 @@ import (
"time"
)
var (
cacheBySn = "c:user:s:%v"
cacheByPhone = "c:user:p:%v"
)
type UserDao struct {
ctx context.Context
query *query.Query
@@ -24,7 +18,7 @@ type UserDao struct {
func NewUserDao(ctx context.Context, cache ...*redis.CacheClient) *UserDao {
dao := &UserDao{
ctx: ctx,
query: query.Use(mysql.GetDB("user_db")),
query: query.Use(mysql.GetDB(dbName)),
}
if len(cache) > 0 {
dao.cache = cache[0]
@@ -44,17 +38,17 @@ func (d *UserDao) Updates(user *model.User) error {
Updates(user)
if info.RowsAffected > 0 {
if d.cache != nil {
d.cache.Del(d.ctx, fmt.Sprintf(cacheBySn, user.Sn))
d.cache.Del(d.ctx, fmt.Sprintf(cacheByPhone, user.Phone))
d.cache.Del(d.ctx, keyCacheBySn(user.Sn, user.TableName()))
d.cache.Del(d.ctx, keyUserCacheByPhone(user.Phone))
}
}
return info.Error
}
func (d *UserDao) FindBySn(sn int64) (*model.User, error) {
func (d *UserDao) FindBySn(sn string) (*model.User, error) {
if d.cache != nil {
var user model.User
if ok := d.cache.Get(d.ctx, fmt.Sprintf(cacheBySn, sn), &user); ok {
if ok := d.cache.Get(d.ctx, keyCacheBySn(sn, user.TableName()), &user); ok {
return &user, nil
}
}
@@ -65,7 +59,7 @@ func (d *UserDao) FindBySn(sn int64) (*model.User, error) {
return nil, err
}
if d.cache != nil {
d.cache.Set(d.ctx, fmt.Sprintf(cacheBySn, sn), first, 5*time.Minute)
d.cache.Set(d.ctx, keyCacheBySn(sn, first.TableName()), first, 5*time.Minute)
}
return first, nil
}
@@ -73,7 +67,7 @@ func (d *UserDao) FindBySn(sn int64) (*model.User, error) {
func (d *UserDao) FindByPhone(phone string) (*model.User, error) {
if d.cache != nil {
var user model.User
if ok := d.cache.Get(d.ctx, fmt.Sprintf(cacheByPhone, phone), &user); ok {
if ok := d.cache.Get(d.ctx, keyUserCacheByPhone(phone), &user); ok {
return &user, nil
}
}
@@ -84,7 +78,7 @@ func (d *UserDao) FindByPhone(phone string) (*model.User, error) {
return nil, err
}
if d.cache != nil {
d.cache.Set(d.ctx, fmt.Sprintf(cacheByPhone, phone), first, 5*time.Minute)
d.cache.Set(d.ctx, keyUserCacheByPhone(phone), first, 5*time.Minute)
}
return first, nil
}