♻️ Rebuilt cache with the new cache system from nexus

This commit is contained in:
2025-03-29 13:15:20 +08:00
parent c8e8104d5d
commit a4c6e9a905
15 changed files with 188 additions and 752 deletions

View File

@ -4,81 +4,26 @@ import (
"context"
"fmt"
"maps"
"git.solsynth.dev/hypernet/nexus/pkg/proto"
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
localCache "git.solsynth.dev/hypernet/passport/pkg/internal/cache"
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
"git.solsynth.dev/hypernet/passport/pkg/internal/services"
"github.com/eko/gocache/lib/v4/cache"
"github.com/eko/gocache/lib/v4/marshaler"
"github.com/samber/lo"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"gorm.io/gorm"
)
func (v *App) GetUser(ctx context.Context, request *proto.GetUserRequest) (*proto.UserInfo, error) {
cacheManager := cache.New[any](localCache.S)
marshal := marshaler.New(cacheManager)
contx := context.Background()
var account models.Account
tx := database.C
hitCache := false
var err error
if request.UserId != nil {
if val, err := marshal.Get(contx, services.GetAccountCacheKey(request.GetUserId()), new(models.Account)); err == nil {
account = *val.(*models.Account)
hitCache = true
} else {
tx = tx.Where("id = ?", uint(request.GetUserId()))
}
}
if request.Name != nil {
if val, err := marshal.Get(contx, services.GetAccountCacheKey(request.GetName()), new(models.Account)); err == nil {
account = *val.(*models.Account)
hitCache = true
} else {
tx = tx.Where("name = ?", request.GetName())
}
account, err = services.GetAccountForEnd(uint(request.GetUserId()))
} else if request.Name != nil {
account, err = services.GetAccountForEnd(request.GetName())
}
if !hitCache {
if err := tx.
Preload("Profile").
Preload("Badges", func(db *gorm.DB) *gorm.DB {
return db.Order("badges.is_active DESC, badges.type DESC")
}).
First(&account).Error; err != nil {
return nil, status.Errorf(codes.NotFound, fmt.Sprintf("requested user with id %d was not found", request.GetUserId()))
}
groups, err := services.GetUserAccountGroup(account)
if err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("unable to get account groups: %v", err))
}
for _, group := range groups {
for k, v := range group.PermNodes {
if _, ok := account.PermNodes[k]; !ok {
account.PermNodes[k] = v
}
}
}
punishments, err := services.ListPunishments(account)
if err != nil {
return nil, status.Errorf(codes.Internal, fmt.Sprintf("unable to get account punishments: %v", err))
}
account.Punishments = punishments
for _, punishment := range punishments {
if punishment.Type == models.PunishmentTypeLimited && len(punishment.PermNodes) > 0 {
maps.Copy(account.PermNodes, punishment.PermNodes)
}
}
services.CacheAccount(account)
if err != nil {
return nil, status.Errorf(codes.NotFound, fmt.Sprintf("unable to get account punishments: %v", err))
}
return account.EncodeToUserInfo(), nil