lab2: fix review — cache consistency, readiness, timeouts, uuid validation, tuple index
Skeleton contracts / contracts (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
This commit is contained in:
+18
-19
@@ -5,25 +5,27 @@ import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"github.com/charawein/mtusi2026lab2/internal/domain"
|
||||
"github.com/charawein/mtusi2026lab2/internal/repository"
|
||||
redisrepo "github.com/charawein/mtusi2026lab2/internal/repository/redis"
|
||||
)
|
||||
|
||||
type MessageService struct {
|
||||
messages repository.MessageRepository
|
||||
chats repository.ChatRepository
|
||||
cache *redisrepo.MessageCache
|
||||
invalidate bool // ВРЕМЕННЫЙ ФЛАГ для демонстрации stale read
|
||||
messages repository.MessageRepository
|
||||
chats repository.ChatRepository
|
||||
cache *redisrepo.MessageCache
|
||||
}
|
||||
|
||||
func NewMessageService(messages repository.MessageRepository, chats repository.ChatRepository, cache *redisrepo.MessageCache) *MessageService {
|
||||
return &MessageService{messages: messages, chats: chats, cache: cache, invalidate: false}
|
||||
return &MessageService{messages: messages, chats: chats, cache: cache}
|
||||
}
|
||||
|
||||
func (s *MessageService) SetInvalidate(v bool) { s.invalidate = v }
|
||||
|
||||
func (s *MessageService) Send(ctx context.Context, senderID, chatID, text string) (*domain.Message, error) {
|
||||
if _, err := uuid.Parse(chatID); err != nil {
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
if !validateText(text) {
|
||||
return nil, ErrInvalidInput
|
||||
}
|
||||
@@ -48,15 +50,16 @@ func (s *MessageService) Send(ctx context.Context, senderID, chatID, text string
|
||||
if err := s.messages.Create(ctx, m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Если invalidate=false — кэш НЕ сбрасывается, будет stale read.
|
||||
if s.invalidate && s.cache != nil {
|
||||
if s.cache != nil {
|
||||
_ = s.cache.Invalidate(ctx, chatID)
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (s *MessageService) List(ctx context.Context, callerID, chatID, cursor string, limit int) ([]*domain.Message, string, error) {
|
||||
if _, err := uuid.Parse(chatID); err != nil {
|
||||
return nil, "", ErrInvalidInput
|
||||
}
|
||||
if limit < 1 || limit > 100 {
|
||||
return nil, "", ErrInvalidInput
|
||||
}
|
||||
@@ -72,10 +75,9 @@ func (s *MessageService) List(ctx context.Context, callerID, chatID, cursor stri
|
||||
return nil, "", ErrNotFound
|
||||
}
|
||||
|
||||
// Попытка достать из кэша (только для первой страницы).
|
||||
if s.cache != nil && cursor == "" {
|
||||
if cached, hit := s.cache.Get(ctx, chatID, cursor); hit {
|
||||
return cached, "", nil
|
||||
if s.cache != nil {
|
||||
if items, next, hit := s.cache.Get(ctx, chatID, cursor, limit); hit {
|
||||
return items, next, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,7 +95,6 @@ func (s *MessageService) List(ctx context.Context, callerID, chatID, cursor stri
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
if items == nil {
|
||||
items = []*domain.Message{}
|
||||
}
|
||||
@@ -105,10 +106,8 @@ func (s *MessageService) List(ctx context.Context, callerID, chatID, cursor stri
|
||||
next = encodeCursor(chatID, last.CreatedAt, last.ID)
|
||||
}
|
||||
|
||||
// Кэшируем только первую страницу.
|
||||
if s.cache != nil && cursor == "" {
|
||||
s.cache.Set(ctx, chatID, cursor, items)
|
||||
if s.cache != nil {
|
||||
s.cache.Set(ctx, chatID, cursor, limit, items, next)
|
||||
}
|
||||
|
||||
return items, next, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user