lab2: postgres, sessions, redis, persistence, stale read
Skeleton contracts / contracts (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"github.com/charawein/mtusi2026lab2/internal/domain"
|
||||
)
|
||||
|
||||
type MessageCache struct {
|
||||
client *redis.Client
|
||||
}
|
||||
|
||||
func NewMessageCache(client *redis.Client) *MessageCache {
|
||||
return &MessageCache{client: client}
|
||||
}
|
||||
|
||||
func (c *MessageCache) Get(ctx context.Context, chatID, cursor string) ([]*domain.Message, bool) {
|
||||
key := fmt.Sprintf("messages:%s:%s", chatID, cursor)
|
||||
raw, err := c.client.Get(ctx, key).Bytes()
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
var msgs []*domain.Message
|
||||
if err := json.Unmarshal(raw, &msgs); err != nil {
|
||||
return nil, false
|
||||
}
|
||||
return msgs, true
|
||||
}
|
||||
|
||||
func (c *MessageCache) Set(ctx context.Context, chatID, cursor string, msgs []*domain.Message) {
|
||||
key := fmt.Sprintf("messages:%s:%s", chatID, cursor)
|
||||
raw, _ := json.Marshal(msgs)
|
||||
_ = c.client.Set(ctx, key, raw, 30*time.Second).Err()
|
||||
}
|
||||
|
||||
func (c *MessageCache) Invalidate(ctx context.Context, chatID string) error {
|
||||
pattern := fmt.Sprintf("messages:%s:*", chatID)
|
||||
keys, err := c.client.Keys(ctx, pattern).Result()
|
||||
if err != nil || len(keys) == 0 {
|
||||
return err
|
||||
}
|
||||
return c.client.Del(ctx, keys...).Err()
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"github.com/charawein/mtusi2026lab2/internal/domain"
|
||||
"github.com/charawein/mtusi2026lab2/internal/repository"
|
||||
)
|
||||
|
||||
type SessionRepository struct {
|
||||
client *redis.Client
|
||||
}
|
||||
|
||||
func NewSessionRepository(client *redis.Client) *SessionRepository {
|
||||
return &SessionRepository{client: client}
|
||||
}
|
||||
|
||||
func (r *SessionRepository) Create(ctx context.Context, s *domain.Session) error {
|
||||
key := "session:" + s.Token
|
||||
ttl := time.Until(s.ExpiresAt)
|
||||
if ttl <= 0 {
|
||||
return fmt.Errorf("session already expired")
|
||||
}
|
||||
if err := r.client.Set(ctx, key, s.UserID, ttl).Err(); err != nil {
|
||||
return fmt.Errorf("set session: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *SessionRepository) GetByToken(ctx context.Context, token string) (*domain.Session, error) {
|
||||
key := "session:" + token
|
||||
userID, err := r.client.Get(ctx, key).Result()
|
||||
if err != nil {
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return nil, repository.ErrNotFound
|
||||
}
|
||||
return nil, fmt.Errorf("get session: %w", err)
|
||||
}
|
||||
ttl, err := r.client.TTL(ctx, key).Result()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("ttl: %w", err)
|
||||
}
|
||||
return &domain.Session{
|
||||
Token: token,
|
||||
UserID: userID,
|
||||
ExpiresAt: time.Now().UTC().Add(ttl),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *SessionRepository) Delete(ctx context.Context, token string) error {
|
||||
return r.client.Del(ctx, "session:"+token).Err()
|
||||
}
|
||||
|
||||
func (r *SessionRepository) DeleteExpired(ctx context.Context) error {
|
||||
// Redis TTL сам удаляет истёкшие; no-op.
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewClient(ctx context.Context, url string) (*redis.Client, error) {
|
||||
opts, err := redis.ParseURL(url)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse redis url: %w", err)
|
||||
}
|
||||
client := redis.NewClient(opts)
|
||||
if err := client.Ping(ctx).Err(); err != nil {
|
||||
return nil, fmt.Errorf("ping redis: %w", err)
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
Reference in New Issue
Block a user