40 lines
864 B
Go
40 lines
864 B
Go
package cache
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/jellydator/ttlcache/v3"
|
|
|
|
"payouts/internal/service/database/orm"
|
|
)
|
|
|
|
type cacheService struct {
|
|
cache *ttlcache.Cache[string, orm.User]
|
|
}
|
|
|
|
func NewSessionCache(ttl time.Duration) (Service, error) {
|
|
return &cacheService{
|
|
cache: ttlcache.New(ttlcache.WithTTL[string, orm.User](ttl)),
|
|
}, nil
|
|
}
|
|
|
|
// PutSession implements [Service].
|
|
func (c *cacheService) PutSession(sessionID string, user orm.User) {
|
|
c.cache.Set(sessionID, user, ttlcache.DefaultTTL)
|
|
}
|
|
|
|
// GetUserFromSession implements [Service].
|
|
func (c *cacheService) GetSession(sessionId string) (orm.User, error) {
|
|
if !c.cache.Has(sessionId) {
|
|
return orm.User{}, NoSessionFound
|
|
}
|
|
cachedItem := c.cache.Get(sessionId)
|
|
|
|
return cachedItem.Value(), nil
|
|
}
|
|
|
|
// StartBackground implements [Service].
|
|
func (c *cacheService) StartBackground() {
|
|
c.cache.Start()
|
|
}
|