openreplay/backend/pkg/sessions/cache.go
2023-08-09 10:34:38 +04:00

51 lines
1.1 KiB
Go

package sessions
import (
"errors"
"log"
"time"
"openreplay/backend/pkg/cache"
)
type Cache interface {
Set(session *Session) error
Get(sessionID uint64) (*Session, error)
}
var ErrSessionNotFound = errors.New("session not found")
type inMemoryCacheImpl struct {
sessions cache.Cache
redis Cache
}
func (i *inMemoryCacheImpl) Set(session *Session) error {
i.sessions.Set(session.SessionID, session)
if err := i.redis.Set(session); err != nil && !errors.Is(err, ErrDisabledCache) {
log.Printf("Failed to cache session: %v", err)
}
return nil
}
func (i *inMemoryCacheImpl) Get(sessionID uint64) (*Session, error) {
if session, ok := i.sessions.Get(sessionID); ok {
return session.(*Session), nil
}
session, err := i.redis.Get(sessionID)
if err == nil {
i.sessions.Set(sessionID, session)
return session, nil
}
if !errors.Is(err, ErrDisabledCache) && err.Error() != "redis: nil" {
log.Printf("Failed to get session from cache: %v", err)
}
return nil, ErrSessionNotFound
}
func NewInMemoryCache(redisCache Cache) Cache {
return &inMemoryCacheImpl{
sessions: cache.New(time.Minute*3, time.Minute*10),
redis: redisCache,
}
}