ЛР 2: PostgreSQL, сессии в Valkey, кэш истории (трек 1)

This commit is contained in:
Нурлыгаянова Екатерина
2026-10-29 11:29:38 +03:00
parent 65003b6119
commit 1503b5db81
132 changed files with 15114 additions and 20 deletions
+49
View File
@@ -0,0 +1,49 @@
package session
import (
"context"
"sync"
"time"
)
var _ Store = (*Memory)(nil)
type Memory struct {
mu sync.Mutex
sessions map[string]Session
now func() time.Time
}
func NewMemory() *Memory {
return &Memory{sessions: make(map[string]Session), now: time.Now}
}
func (m *Memory) Save(_ context.Context, tokenHash []byte, s Session) error {
m.mu.Lock()
defer m.mu.Unlock()
m.sessions[string(tokenHash)] = s
return nil
}
func (m *Memory) Get(_ context.Context, tokenHash []byte) (Session, error) {
m.mu.Lock()
defer m.mu.Unlock()
s, ok := m.sessions[string(tokenHash)]
if !ok {
return Session{}, ErrNotFound
}
if !m.now().Before(s.ExpiresAt) {
delete(m.sessions, string(tokenHash))
return Session{}, ErrNotFound
}
return s, nil
}
func (m *Memory) Delete(_ context.Context, tokenHash []byte) error {
m.mu.Lock()
defer m.mu.Unlock()
delete(m.sessions, string(tokenHash))
return nil
}
func (m *Memory) Ping(context.Context) error { return nil }