Skip to content

Commit

Permalink
fix: media group mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
boojack committed Feb 17, 2025
1 parent 96e460c commit 3a600a4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 81 deletions.
68 changes: 0 additions & 68 deletions cache.go

This file was deleted.

27 changes: 14 additions & 13 deletions memogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"path/filepath"
"regexp"
"strings"
"time"
"sync"
"unicode/utf16"

"github.com/go-telegram/bot"
Expand All @@ -28,7 +28,9 @@ type Service struct {
client *MemosClient
config *Config
store *store.Store
cache *Cache

mediaGroupCache sync.Map
mediaGroupMutex sync.Mutex

workspaceProfile *v1pb.WorkspaceProfile
}
Expand All @@ -54,9 +56,7 @@ func NewService() (*Service, error) {
config: config,
client: client,
store: store,
cache: NewCache(),
}
s.cache.startGC()

opts := []bot.Option{
bot.WithDefaultHandler(s.handler),
Expand Down Expand Up @@ -123,17 +123,18 @@ func (s *Service) handleMemoCreation(ctx context.Context, m *models.Update, cont
var err error

if m.Message.MediaGroupID != "" {
cacheMemo, ok := s.cache.get(m.Message.MediaGroupID)
if !ok {
memo, err = s.createMemo(ctx, content)
if err != nil {
return nil, err
}
s.mediaGroupMutex.Lock()
defer s.mediaGroupMutex.Unlock()

s.cache.set(m.Message.MediaGroupID, memo, 24*time.Hour)
} else {
memo = cacheMemo.(*v1pb.Memo)
if cache, ok := s.mediaGroupCache.Load(m.Message.MediaGroupID); ok {
return cache.(*v1pb.Memo), nil
}

memo, err = s.createMemo(ctx, content)
if err != nil {
return nil, err
}
s.mediaGroupCache.Store(m.Message.MediaGroupID, memo)
} else {
memo, err = s.createMemo(ctx, content)
if err != nil {
Expand Down

0 comments on commit 3a600a4

Please sign in to comment.