181 lines
4.3 KiB
Go
181 lines
4.3 KiB
Go
package redis
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"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)
|
|
ListVisible(
|
|
ctx context.Context,
|
|
callerID uuid.UUID,
|
|
after *domain.Cursor,
|
|
limit int,
|
|
) (domain.Page[domain.Chat], error)
|
|
}
|
|
|
|
type ChatRepository struct {
|
|
source ChatSource
|
|
cache *cache.SingleflightCache
|
|
ttl time.Duration
|
|
log *slog.Logger
|
|
}
|
|
|
|
func NewChatRepository(
|
|
source ChatSource,
|
|
cache *cache.SingleflightCache,
|
|
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) {
|
|
chat, err := r.source.Create(ctx, draft)
|
|
if err != nil {
|
|
return domain.Chat{}, err
|
|
}
|
|
|
|
r.invalidate(ctx, draft.MemberIDs)
|
|
|
|
return chat, nil
|
|
}
|
|
|
|
func (r *ChatRepository) GetVisible(ctx context.Context, chatID, callerID uuid.UUID) (domain.Chat, error) {
|
|
return r.source.GetVisible(ctx, chatID, callerID)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
key := chatListKey(callerID, r.generation(ctx, callerID), limit)
|
|
|
|
loader := func(ctx context.Context) ([]byte, error) {
|
|
page, err := r.source.ListVisible(ctx, callerID, nil, limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return json.Marshal(newChatPageDTO(page))
|
|
}
|
|
|
|
encoded, err := r.cache.GetOrLoad(ctx, key, r.ttl, loader)
|
|
if err != nil {
|
|
return domain.Page[domain.Chat]{}, fmt.Errorf("load chat list: %w", err)
|
|
}
|
|
|
|
var dto chatPageDTO
|
|
if err := json.Unmarshal(encoded, &dto); err != nil {
|
|
return domain.Page[domain.Chat]{}, fmt.Errorf("decode cached chat list: %w", err)
|
|
}
|
|
|
|
return dto.toDomain(), nil
|
|
}
|
|
|
|
func (r *ChatRepository) generation(ctx context.Context, userID uuid.UUID) string {
|
|
value, err := r.cache.Get(ctx, chatGenKey(userID))
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
|
|
return string(value)
|
|
}
|
|
|
|
func (r *ChatRepository) invalidate(ctx context.Context, memberIDs []uuid.UUID) {
|
|
for _, memberID := range memberIDs {
|
|
if err := r.cache.Set(ctx, chatGenKey(memberID), []byte(uuid.NewString()), 0); err != nil && r.log != nil {
|
|
r.log.WarnContext(ctx, "chat cache invalidation failed",
|
|
slog.String("user_id", memberID.String()), slog.Any("error", err))
|
|
}
|
|
}
|
|
}
|
|
|
|
func chatGenKey(userID uuid.UUID) string {
|
|
return "chat_gen:" + userID.String()
|
|
}
|
|
|
|
func chatListKey(userID uuid.UUID, generation string, limit int) string {
|
|
return fmt.Sprintf("chat_list:%s:%s:%d", userID, generation, 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}
|
|
}
|