Skip to content

Commit

Permalink
✨ feat: Add caching for web content fetching
Browse files Browse the repository at this point in the history
- Introduced `FetchContentWithCache` method in `bookmarks/service.go` to handle content fetching with caching
- Refactored `WebSummaryHandler` in `websummary.go` to use the new cached content fetching method
- Removed redundant reader initialization and caching logic from `WebSummaryHandler`
- Added `cache` and `time` package imports to support caching functionality
  • Loading branch information
vaayne committed Jan 9, 2025
1 parent c61bc27 commit 01765ac
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
24 changes: 19 additions & 5 deletions internal/core/bookmarks/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/url"
"recally/internal/pkg/auth"
"recally/internal/pkg/cache"
"recally/internal/pkg/db"
"recally/internal/pkg/llms"
"recally/internal/pkg/logger"
Expand All @@ -15,6 +16,7 @@ import (
"regexp"
"slices"
"strings"
"time"

"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgtype"
Expand Down Expand Up @@ -261,11 +263,7 @@ func (s *Service) FetchContent(ctx context.Context, tx db.DBTX, id, userID uuid.
return nil, fmt.Errorf("failed to get bookmark by id '%s': %w", id.String(), err)
}

reader, err := reader.New(fetcherType, dto.URL)
if err != nil {
return nil, fmt.Errorf("failed to create reader: %w", err)
}
content, err := reader.Read(ctx, dto.URL)
content, err := s.FetchContentWithCache(ctx, fetcherType, dto.URL)
if err != nil {
return nil, fmt.Errorf("failed to fetch content: %w", err)
}
Expand Down Expand Up @@ -293,6 +291,22 @@ func (s *Service) FetchContent(ctx context.Context, tx db.DBTX, id, userID uuid.
return s.Update(ctx, tx, id, userID, dto)
}

func (s *Service) FetchContentWithCache(ctx context.Context, fetcherType fetcher.FecherType, uri string) (*webreader.Content, error) {
return cache.RunInCache(ctx, cache.DefaultDBCache,
cache.NewCacheKey(fmt.Sprintf("WebReader-%s", fetcherType), uri), 24*time.Hour, func() (*webreader.Content, error) {
// read the content using jina reader
reader, err := reader.New(fetcher.TypeJinaReader, uri)
if err != nil {
return nil, fmt.Errorf("failed to create reader: %w", err)
}
content, err := reader.Read(ctx, uri)
if err != nil {
return nil, fmt.Errorf("failed to read content: %w", err)
}
return content, nil
})
}

func (s *Service) SummarierContent(ctx context.Context, tx db.DBTX, id, userID uuid.UUID) (*ContentDTO, error) {
user, err := auth.LoadUser(ctx, tx, userID)
if err != nil {
Expand Down
16 changes: 2 additions & 14 deletions internal/port/bots/handlers/websummary.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"recally/internal/pkg/logger"
"recally/internal/pkg/webreader/fetcher"
"recally/internal/pkg/webreader/processor"
"recally/internal/pkg/webreader/reader"
"regexp"
"strings"
"time"
Expand Down Expand Up @@ -103,25 +102,14 @@ func (h *Handler) WebSummaryHandler(c tele.Context) error {
summary, err := cache.RunInCache[string](ctx, cache.DefaultDBCache, cache.NewCacheKey("WebSummary", url), 24*time.Hour, func() (*string, error) {
isSummaryCached = false
// cache the content
content, err := cache.RunInCache[string](ctx, cache.DefaultDBCache, cache.NewCacheKey("WebReader", url), 24*time.Hour, func() (*string, error) {
// read the content using jina reader
reader, err := reader.New(fetcher.TypeJinaReader, url)
if err != nil {
return nil, fmt.Errorf("failed to create reader: %w", err)
}
content, err := reader.Read(ctx, url)
if err != nil {
return nil, fmt.Errorf("failed to read content: %w", err)
}
return &content.Markwdown, nil
})
content, err := h.bookmarkService.FetchContentWithCache(ctx, fetcher.TypeJinaReader, url)
if err != nil {
return nil, fmt.Errorf("failed to get content: %w", err)
}

// process the summary
summarier := processor.NewSummaryProcessor(h.llm, processor.WithSummaryOptionUser(user))
summarier.StreamingSummary(ctx, *content, sendToUser)
summarier.StreamingSummary(ctx, content.Markwdown, sendToUser)
return &resp, nil
})
if err != nil {
Expand Down

0 comments on commit 01765ac

Please sign in to comment.