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