⚡ Add cache into querying user
This commit is contained in:
parent
77c543f88e
commit
02f122328a
12
.idea/workspace.xml
generated
12
.idea/workspace.xml
generated
@ -4,9 +4,13 @@
|
||||
<option name="autoReloadType" value="ALL" />
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":bug: Bug fix directory service wasn't registered">
|
||||
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":bug: Fix random panic">
|
||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/pkg/internal/services/check_in.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/check_in.go" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/pkg/internal/grpc/user.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/grpc/user.go" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/pkg/internal/http/api/accounts_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/http/api/accounts_api.go" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/pkg/internal/http/api/userinfo_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/http/api/userinfo_api.go" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/pkg/internal/services/accounts.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/accounts.go" afterDir="false" />
|
||||
<change beforePath="$PROJECT_DIR$/pkg/internal/services/preferences.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/preferences.go" afterDir="false" />
|
||||
</list>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
@ -159,7 +163,6 @@
|
||||
</component>
|
||||
<component name="VcsManagerConfiguration">
|
||||
<option name="CHECK_CODE_SMELLS_BEFORE_PROJECT_COMMIT" value="false" />
|
||||
<MESSAGE value=":loud_sound: Verbose notifying logging" />
|
||||
<MESSAGE value=":loud_sound: Verbose notifying check logging" />
|
||||
<MESSAGE value=":bug: Fix notifiable is empty when user do not set" />
|
||||
<MESSAGE value=":bug: Fix notification push batch emitted twice" />
|
||||
@ -184,7 +187,8 @@
|
||||
<MESSAGE value=":loud_sound: Verbose logging at setting last seen at" />
|
||||
<MESSAGE value=":loud_sound: Verbose logging at receive broadcasting event" />
|
||||
<MESSAGE value=":bug: Bug fix directory service wasn't registered" />
|
||||
<option name="LAST_COMMIT_MESSAGE" value=":bug: Bug fix directory service wasn't registered" />
|
||||
<MESSAGE value=":bug: Fix random panic" />
|
||||
<option name="LAST_COMMIT_MESSAGE" value=":bug: Fix random panic" />
|
||||
<option name="GROUP_MULTIFILE_MERGE_BY_DIRECTORY" value="true" />
|
||||
</component>
|
||||
<component name="VgoProject">
|
||||
|
@ -5,25 +5,68 @@ import (
|
||||
"fmt"
|
||||
"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) {
|
||||
tx := database.C
|
||||
if request.UserId != nil {
|
||||
tx = tx.Where("id = ?", uint(request.GetUserId()))
|
||||
}
|
||||
if request.Name != nil {
|
||||
tx = tx.Where("name = ?", request.GetName())
|
||||
}
|
||||
cacheManager := cache.New[any](localCache.S)
|
||||
marshal := marshaler.New(cacheManager)
|
||||
contx := context.Background()
|
||||
|
||||
var account models.Account
|
||||
if err := tx.First(&account).Error; err != nil {
|
||||
return nil, status.Errorf(codes.NotFound, fmt.Sprintf("requested user with id %d was not found", request.GetUserId()))
|
||||
|
||||
tx := database.C
|
||||
hitCache := false
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
||||
if !hitCache {
|
||||
if err := tx.
|
||||
Preload("Profile").
|
||||
Preload("Badges", func(db *gorm.DB) *gorm.DB {
|
||||
return db.Order("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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
services.CacheAccount(account)
|
||||
}
|
||||
|
||||
return account.EncodeToUserInfo(), nil
|
||||
}
|
||||
|
||||
|
@ -65,8 +65,9 @@ func getUserinfo(c *fiber.Ctx) error {
|
||||
|
||||
var resp fiber.Map
|
||||
raw, _ := jsoniter.Marshal(data)
|
||||
jsoniter.Unmarshal(raw, &resp)
|
||||
_ = jsoniter.Unmarshal(raw, &resp)
|
||||
|
||||
// Used to support OIDC standard
|
||||
resp["sub"] = strconv.Itoa(int(data.ID))
|
||||
resp["family_name"] = data.Profile.FirstName
|
||||
resp["given_name"] = data.Profile.LastName
|
||||
|
@ -1,8 +1,12 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
||||
localCache "git.solsynth.dev/hypernet/passport/pkg/internal/cache"
|
||||
"github.com/eko/gocache/lib/v4/cache"
|
||||
"github.com/eko/gocache/lib/v4/marshaler"
|
||||
"gorm.io/gorm"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -15,10 +19,21 @@ import (
|
||||
func getOtherUserinfo(c *fiber.Ctx) error {
|
||||
alias := c.Params("alias")
|
||||
|
||||
cacheManager := cache.New[any](localCache.S)
|
||||
marshal := marshaler.New(cacheManager)
|
||||
ctx := context.Background()
|
||||
|
||||
if val, err := marshal.Get(ctx, services.GetAccountCacheKey(alias), new(models.Account)); err == nil {
|
||||
return c.JSON(*val.(*models.Account))
|
||||
}
|
||||
|
||||
tx := database.C.Where("name = ?", alias)
|
||||
|
||||
numericId, err := strconv.Atoi(alias)
|
||||
if err == nil {
|
||||
if val, err := marshal.Get(ctx, services.GetAccountCacheKey(numericId), new(models.Account)); err == nil {
|
||||
return c.JSON(*val.(*models.Account))
|
||||
}
|
||||
tx = tx.Or("id = ?", numericId)
|
||||
}
|
||||
|
||||
@ -44,6 +59,8 @@ func getOtherUserinfo(c *fiber.Ctx) error {
|
||||
}
|
||||
}
|
||||
|
||||
services.CacheAccount(account)
|
||||
|
||||
return c.JSON(account)
|
||||
}
|
||||
|
||||
|
@ -6,6 +6,10 @@ import (
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/proto"
|
||||
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
||||
localCache "git.solsynth.dev/hypernet/passport/pkg/internal/cache"
|
||||
"github.com/eko/gocache/lib/v4/cache"
|
||||
"github.com/eko/gocache/lib/v4/marshaler"
|
||||
"github.com/eko/gocache/lib/v4/store"
|
||||
"time"
|
||||
"unicode"
|
||||
|
||||
@ -22,6 +26,31 @@ import (
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
func GetAccountCacheKey(query any) string {
|
||||
return fmt.Sprintf("account-query#%v", query)
|
||||
}
|
||||
|
||||
func CacheAccount(account models.Account) {
|
||||
cacheManager := cache.New[any](localCache.S)
|
||||
marshal := marshaler.New(cacheManager)
|
||||
ctx := context.Background()
|
||||
|
||||
_ = marshal.Set(
|
||||
ctx,
|
||||
GetAccountCacheKey(account.Name),
|
||||
account,
|
||||
store.WithExpiration(30*time.Minute),
|
||||
store.WithTags([]string{"account", fmt.Sprintf("user#%d", account.ID)}),
|
||||
)
|
||||
_ = marshal.Set(
|
||||
ctx,
|
||||
GetAccountCacheKey(account.ID),
|
||||
account,
|
||||
store.WithExpiration(30*time.Minute),
|
||||
store.WithTags([]string{"account", fmt.Sprintf("user#%d", account.ID)}),
|
||||
)
|
||||
}
|
||||
|
||||
func ValidateAccountName(val string, min, max int) bool {
|
||||
actualLength := 0
|
||||
for _, r := range val {
|
||||
|
@ -49,10 +49,10 @@ func GetNotificationPreference(account models.Account) (models.PreferenceNotific
|
||||
var notification models.PreferenceNotification
|
||||
cacheManager := cache.New[any](localCache.S)
|
||||
marshal := marshaler.New(cacheManager)
|
||||
contx := context.Background()
|
||||
ctx := context.Background()
|
||||
|
||||
if val, err := marshal.Get(contx, GetNotificationPreferenceCacheKey(account.ID), new(models.PreferenceNotification)); err == nil {
|
||||
notification = val.(models.PreferenceNotification)
|
||||
if val, err := marshal.Get(ctx, GetNotificationPreferenceCacheKey(account.ID), new(models.PreferenceNotification)); err == nil {
|
||||
notification = *val.(*models.PreferenceNotification)
|
||||
} else {
|
||||
if err := database.C.Where("account_id = ?", account.ID).First(¬ification).Error; err != nil {
|
||||
return notification, err
|
||||
|
Loading…
Reference in New Issue
Block a user