♻️ Refactored cache system

This commit is contained in:
2024-09-22 13:13:05 +08:00
parent 648f10b25a
commit bbceb65dbf
7 changed files with 524 additions and 52 deletions

24
pkg/internal/cache/store.go vendored Normal file
View File

@ -0,0 +1,24 @@
package cache
import (
"github.com/dgraph-io/ristretto"
"github.com/eko/gocache/lib/v4/store"
ristrettoCache "github.com/eko/gocache/store/ristretto/v4"
)
var S store.StoreInterface
func NewStore() error {
ristretto, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 1000,
MaxCost: 100,
BufferItems: 64,
})
if err != nil {
return err
}
S = ristrettoCache.NewRistretto(ristretto)
return nil
}

View File

@ -71,7 +71,6 @@ func (v AuthTicket) IsCanBeAvailble() error {
}
type AuthContext struct {
Ticket AuthTicket `json:"ticket"`
Account Account `json:"account"`
LastUsedAt time.Time `json:"last_used_at"`
Ticket AuthTicket `json:"ticket"`
Account Account `json:"account"`
}

View File

@ -1,19 +1,21 @@
package services
import (
"context"
"fmt"
"sync"
"time"
"github.com/eko/gocache/lib/v4/cache"
"github.com/eko/gocache/lib/v4/marshaler"
"github.com/eko/gocache/lib/v4/store"
jsoniter "github.com/json-iterator/go"
localCache "git.solsynth.dev/hydrogen/passport/pkg/internal/cache"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"github.com/gofiber/fiber/v2"
"github.com/rs/zerolog/log"
)
var authContextCache sync.Map
func Authenticate(atk, rtk string, rty int) (ctx models.AuthContext, perms map[string]any, newAtk, newRtk string, err error) {
var claims PayloadClaims
claims, err = DecodeJwt(atk)
@ -45,14 +47,20 @@ func Authenticate(atk, rtk string, rty int) (ctx models.AuthContext, perms map[s
return
}
func GetAuthContextCacheKey(jti string) string {
return fmt.Sprintf("auth-context#%s", jti)
}
func GetAuthContext(jti string) (models.AuthContext, error) {
var err error
var ctx models.AuthContext
if val, ok := authContextCache.Load(jti); ok {
cacheManager := cache.New[any](localCache.S)
marshal := marshaler.New(cacheManager)
contx := context.Background()
if val, err := marshal.Get(contx, GetAuthContextCacheKey(jti), new(models.AuthContext)); err != nil {
ctx = val.(models.AuthContext)
ctx.LastUsedAt = time.Now()
authContextCache.Store(jti, ctx)
} else {
ctx, err = CacheAuthContext(jti)
log.Debug().Str("jti", jti).Msg("Created a new auth context cache")
@ -90,38 +98,32 @@ func CacheAuthContext(jti string) (models.AuthContext, error) {
}
ctx = models.AuthContext{
Ticket: ticket,
Account: user,
LastUsedAt: time.Now(),
Ticket: ticket,
Account: user,
}
// Put the data into memory for cache
authContextCache.Store(jti, ctx)
// Put the data into cache
cacheManager := cache.New[any](localCache.S)
marshal := marshaler.New(cacheManager)
contx := context.Background()
marshal.Set(
contx,
GetAuthContextCacheKey(jti),
ctx,
store.WithExpiration(3*time.Minute),
store.WithTags([]string{"auth-context", fmt.Sprintf("user#%d", user.ID)}),
)
return ctx, nil
}
func RecycleAuthContext() {
affected := 0
authContextCache.Range(func(key, value any) bool {
val := value.(models.AuthContext)
if val.LastUsedAt.Add(60*time.Second).Unix() < time.Now().Unix() {
affected++
authContextCache.Delete(key)
}
return true
})
log.Debug().Int("affected", affected).Msg("Recycled auth context...")
}
func InvalidAuthCacheWithUser(userId uint) {
authContextCache.Range(func(key, value any) bool {
val := value.(models.AuthContext)
if val.Account.ID == userId {
authContextCache.Delete(key)
}
return true
})
cacheManager := cache.New[any](localCache.S)
contx := context.Background()
cacheManager.Invalidate(
contx,
store.WithInvalidateTags([]string{"auth-context", fmt.Sprintf("user#%d", userId)}),
)
}

View File

@ -3,21 +3,33 @@ package services
import (
"context"
"fmt"
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
"git.solsynth.dev/hydrogen/passport/pkg/internal/gap"
"time"
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
localCache "git.solsynth.dev/hydrogen/passport/pkg/internal/cache"
"git.solsynth.dev/hydrogen/passport/pkg/internal/gap"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"github.com/eko/gocache/lib/v4/cache"
"github.com/eko/gocache/lib/v4/marshaler"
"github.com/eko/gocache/lib/v4/store"
"github.com/samber/lo"
)
var statusCache = make(map[uint]models.Status)
func GetStatusCacheKey(uid uint) string {
return fmt.Sprintf("user-status#%d", uid)
}
func GetStatus(uid uint) (models.Status, error) {
if status, ok := statusCache[uid]; ok {
cacheManager := cache.New[any](localCache.S)
marshal := marshaler.New(cacheManager)
contx := context.Background()
if val, err := marshal.Get(contx, GetStatusCacheKey(uid), new(models.Status)); err == nil {
status := val.(models.Status)
if status.ClearAt != nil && status.ClearAt.Before(time.Now()) {
delete(statusCache, uid)
marshal.Delete(contx, GetStatusCacheKey(uid))
} else {
return status, nil
}
@ -29,11 +41,24 @@ func GetStatus(uid uint) (models.Status, error) {
First(&status).Error; err != nil {
return status, err
} else {
statusCache[uid] = status
CacheUserStatus(uid, status)
}
return status, nil
}
func CacheUserStatus(uid uint, status models.Status) {
cacheManager := cache.New[any](localCache.S)
marshal := marshaler.New(cacheManager)
contx := context.Background()
marshal.Set(
contx,
GetStatusCacheKey(uid),
status,
store.WithTags([]string{"user-status", fmt.Sprintf("user#%d", uid)}),
)
}
func GetUserOnline(uid uint) bool {
pc := proto.NewStreamControllerClient(gap.H.GetDealerGrpcConn())
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
@ -77,7 +102,7 @@ func NewStatus(user models.Account, status models.Status) (models.Status, error)
if err := database.C.Save(&status).Error; err != nil {
return status, err
} else {
statusCache[user.ID] = status
CacheUserStatus(user.ID, status)
}
return status, nil
}
@ -86,7 +111,7 @@ func EditStatus(user models.Account, status models.Status) (models.Status, error
if err := database.C.Save(&status).Error; err != nil {
return status, err
} else {
statusCache[user.ID] = status
CacheUserStatus(user.ID, status)
}
return status, nil
}
@ -98,7 +123,11 @@ func ClearStatus(user models.Account) error {
Updates(models.Status{ClearAt: lo.ToPtr(time.Now())}).Error; err != nil {
return err
} else {
delete(statusCache, user.ID)
cacheManager := cache.New[any](localCache.S)
marshal := marshaler.New(cacheManager)
contx := context.Background()
marshal.Delete(contx, GetStatusCacheKey(user.ID))
}
return nil

View File

@ -13,6 +13,7 @@ import (
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"github.com/robfig/cron/v3"
"git.solsynth.dev/hydrogen/passport/pkg/internal/cache"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
@ -43,6 +44,11 @@ func main() {
log.Fatal().Err(err).Msg("An error occurred when running database auto migration.")
}
// Initialize cache
if err := cache.NewStore(); err != nil {
log.Fatal().Err(err).Msg("An error occurred when initializing cache.")
}
// Connect other services
if err := gap.RegisterService(); err != nil {
log.Error().Err(err).Msg("An error occurred when registering service to gateway...")
@ -58,7 +64,6 @@ func main() {
quartz := cron.New(cron.WithLogger(cron.VerbosePrintfLogger(&log.Logger)))
quartz.AddFunc("@every 60m", services.DoAutoSignoff)
quartz.AddFunc("@every 60m", services.DoAutoDatabaseCleanup)
quartz.AddFunc("@every 60s", services.RecycleAuthContext)
quartz.AddFunc("@midnight", services.RecycleUnConfirmAccount)
quartz.AddFunc("@every 60s", services.SaveEventChanges)
quartz.Start()