Add cache into querying user

This commit is contained in:
2024-12-08 20:21:40 +08:00
parent 77c543f88e
commit 02f122328a
6 changed files with 111 additions and 17 deletions

View File

@ -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

View File

@ -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)
}