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

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

View File

@ -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(&notification).Error; err != nil {
return notification, err