Skip to content

Performance: Add LRU cache for rendered PDF pages #507

@cbcoutinho

Description

@cbcoutinho

Problem

Each PDF preview request re-downloads and re-renders the PDF, even for the same page. This is inefficient when users navigate back and forth between pages.

Recommendation

Add a simple LRU cache for rendered pages:

from functools import lru_cache
import hashlib

# Cache key: (user_id, file_path, page, scale, pdf_hash)
@lru_cache(maxsize=100)  # Cache last 100 rendered pages
def _render_pdf_page_cached(cache_key: tuple) -> tuple[bytes, int]:
    user_id, file_path, page_num, scale, pdf_hash = cache_key
    # Actual rendering logic here
    ...

# In get_pdf_preview():
pdf_hash = hashlib.sha256(pdf_bytes).hexdigest()[:16]
cache_key = (user_id, file_path, page_num, scale, pdf_hash)
png_bytes, total_pages = _render_pdf_page_cached(cache_key)

Benefit: Significantly reduces CPU load when users navigate between pages.

Trade-off: Memory usage (100 pages × ~500KB ≈ 50MB is reasonable).

Location

  • nextcloud_mcp_server/api/visualization.py

Priority

Nice to have - Performance optimization

Parent Issue

Part of #502

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions