⚡ Add cache into querying user
This commit is contained in:
@ -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)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user