184 lines
4.3 KiB
Go
184 lines
4.3 KiB
Go
package redis
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"mtusi-messenger/internal/domain"
|
|
"mtusi-messenger/internal/platform/cache"
|
|
)
|
|
|
|
type ChatSource interface {
|
|
Create(ctx context.Context, draft domain.ChatDraft) (domain.Chat, error)
|
|
GetVisible(ctx context.Context, chatID, callerID uuid.UUID) (domain.Chat, error)
|
|
ListVersion(ctx context.Context, userID uuid.UUID) (int64, error)
|
|
ListVisible(
|
|
ctx context.Context,
|
|
callerID uuid.UUID,
|
|
after *domain.Cursor,
|
|
limit int,
|
|
) (domain.Page[domain.Chat], error)
|
|
}
|
|
|
|
type ChatRepository struct {
|
|
source ChatSource
|
|
cache *cache.DistributedCache
|
|
ttl time.Duration
|
|
log *slog.Logger
|
|
}
|
|
|
|
func NewChatRepository(
|
|
source ChatSource,
|
|
cache *cache.DistributedCache,
|
|
ttl time.Duration,
|
|
log *slog.Logger,
|
|
) *ChatRepository {
|
|
return &ChatRepository{source: source, cache: cache, ttl: ttl, log: log}
|
|
}
|
|
|
|
func (r *ChatRepository) Create(ctx context.Context, draft domain.ChatDraft) (domain.Chat, error) {
|
|
return r.source.Create(ctx, draft)
|
|
}
|
|
|
|
func (r *ChatRepository) GetVisible(ctx context.Context, chatID, callerID uuid.UUID) (domain.Chat, error) {
|
|
return r.source.GetVisible(ctx, chatID, callerID)
|
|
}
|
|
|
|
type sourceError struct{ err error }
|
|
|
|
func (e sourceError) Error() string { return e.err.Error() }
|
|
|
|
func (e sourceError) Unwrap() error { return e.err }
|
|
|
|
const cacheOperationTimeout = 500 * time.Millisecond
|
|
|
|
func (r *ChatRepository) ListVisible(
|
|
ctx context.Context,
|
|
callerID uuid.UUID,
|
|
after *domain.Cursor,
|
|
limit int,
|
|
) (domain.Page[domain.Chat], error) {
|
|
if after != nil {
|
|
return r.source.ListVisible(ctx, callerID, after, limit)
|
|
}
|
|
|
|
version, err := r.source.ListVersion(ctx, callerID)
|
|
if err != nil {
|
|
return domain.Page[domain.Chat]{}, fmt.Errorf("chat list version: %w", err)
|
|
}
|
|
|
|
key := chatListKey(callerID, version, limit)
|
|
|
|
loader := func(ctx context.Context) ([]byte, error) {
|
|
page, err := r.source.ListVisible(ctx, callerID, nil, limit)
|
|
if err != nil {
|
|
return nil, sourceError{err}
|
|
}
|
|
|
|
return json.Marshal(newChatPageDTO(page))
|
|
}
|
|
|
|
cctx, cancel := context.WithTimeout(ctx, cacheOperationTimeout)
|
|
defer cancel()
|
|
|
|
encoded, err := r.cache.GetOrLoad(cctx, key, r.ttl, loader)
|
|
if err != nil {
|
|
var sourceErr sourceError
|
|
switch {
|
|
case errors.As(err, &sourceErr):
|
|
return domain.Page[domain.Chat]{}, sourceErr.err
|
|
case ctx.Err() != nil:
|
|
return domain.Page[domain.Chat]{}, ctx.Err()
|
|
}
|
|
} else {
|
|
var dto chatPageDTO
|
|
unmarshalErr := json.Unmarshal(encoded, &dto)
|
|
if unmarshalErr == nil {
|
|
return dto.toDomain(), nil
|
|
}
|
|
err = unmarshalErr
|
|
|
|
_ = r.cache.Delete(ctx, key)
|
|
}
|
|
|
|
if r.log != nil {
|
|
r.log.WarnContext(ctx, "chat list cache unavailable, falling back to source", slog.Any("error", err))
|
|
}
|
|
|
|
return r.source.ListVisible(ctx, callerID, nil, limit)
|
|
}
|
|
|
|
func chatListKey(userID uuid.UUID, version int64, limit int) string {
|
|
return fmt.Sprintf("chat_list:%s:%d:%d", userID, version, limit)
|
|
}
|
|
|
|
type chatPageDTO struct {
|
|
Items []chatDTO `json:"items"`
|
|
Next *cursorDTO `json:"next,omitempty"`
|
|
}
|
|
|
|
type chatDTO struct {
|
|
ID uuid.UUID `json:"id"`
|
|
Title string `json:"title"`
|
|
MemberIDs []uuid.UUID `json:"member_ids"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
}
|
|
|
|
type cursorDTO struct {
|
|
Scope uuid.UUID `json:"scope"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
ID uuid.UUID `json:"id"`
|
|
}
|
|
|
|
func newChatPageDTO(page domain.Page[domain.Chat]) chatPageDTO {
|
|
items := make([]chatDTO, 0, len(page.Items))
|
|
for _, chat := range page.Items {
|
|
items = append(items, chatDTO{
|
|
ID: chat.ID,
|
|
Title: chat.Title,
|
|
MemberIDs: chat.MemberIDs,
|
|
CreatedAt: chat.CreatedAt,
|
|
})
|
|
}
|
|
|
|
var next *cursorDTO
|
|
if page.Next != nil {
|
|
next = &cursorDTO{
|
|
Scope: page.Next.Scope,
|
|
CreatedAt: page.Next.CreatedAt,
|
|
ID: page.Next.ID,
|
|
}
|
|
}
|
|
|
|
return chatPageDTO{Items: items, Next: next}
|
|
}
|
|
|
|
func (d chatPageDTO) toDomain() domain.Page[domain.Chat] {
|
|
items := make([]domain.Chat, 0, len(d.Items))
|
|
for _, chat := range d.Items {
|
|
items = append(items, domain.Chat{
|
|
ID: chat.ID,
|
|
Title: chat.Title,
|
|
MemberIDs: chat.MemberIDs,
|
|
CreatedAt: chat.CreatedAt,
|
|
})
|
|
}
|
|
|
|
var next *domain.Cursor
|
|
if d.Next != nil {
|
|
next = &domain.Cursor{
|
|
Scope: d.Next.Scope,
|
|
CreatedAt: d.Next.CreatedAt,
|
|
ID: d.Next.ID,
|
|
}
|
|
}
|
|
|
|
return domain.Page[domain.Chat]{Items: items, Next: next}
|
|
}
|