Skip to content

feat: Add collection caching for improved performance - #63

Open
mattkduran wants to merge 3 commits into
tubearchivist:masterfrom
mattkduran:feat/collection-caching
Open

feat: Add collection caching for improved performance#63
mattkduran wants to merge 3 commits into
tubearchivist:masterfrom
mattkduran:feat/collection-caching

Conversation

@mattkduran

@mattkduran mattkduran commented Oct 17, 2025

Copy link
Copy Markdown
Contributor

Problem

The current implementation performs 3 database lookups (GetItemById) for every playback event to validate if content belongs to the TubeArchivist collection. This creates unnecessary overhead, especially when playing non-TubeArchivist content like movies or music.

Solution

Cache the TubeArchivist collection GUID once at startup and use it for O(1) validation checks.

Changes

  1. Added collection ID caching:

    • _tubeArchivistCollectionId field stores the cached GUID
    • CacheTubeArchivistCollectionId() finds and caches the collection on startup
  2. Dynamic cache updates:

    • RefreshTubeArchivistCollectionId() public method to refresh the cache
    • Updated PluginConfiguration.CollectionTitle setter to trigger refresh on changes
    • No restart required when changing collection name
  3. Efficient validation:

    • IsItemInTubeArchivistCollection() helper method
    • Walks parent hierarchy comparing cached GUID (fast)
    • Falls back to name-based lookup if cache is not initialized

Performance Impact

Before:

  • 3 calls per playback event for ALL media
  • Example: Playing 100 movies = 300 lookups

After:

  • 1 call on startup (caching)
  • 0 calls for rejected media (type check + cached ID)
  • 1-3 calls only for TubeArchivist Episodes
  • Example: Playing 100 movies = 0 lookups, instant rejection

Testing

  • Startup: Collection cached successfully
  • Config change: Cache refreshes automatically
  • Movie playback: Fast rejection with cached ID
  • TubeArchivist video: Still validates and syncs correctly
  • Missing collection: Falls back to name-based validation

- Added _tubeArchivistCollectionId field to cache collection GUID on startup
- Added CacheTubeArchivistCollectionId() to find and cache collection once
- Added RefreshTubeArchivistCollectionId() to update cache on config changes
- Added IsItemInTubeArchivistCollection() helper for efficient validation
- Updated PluginConfiguration.CollectionTitle to trigger cache refresh on change
- Updated OnPlaybackProgress to use cached ID for O(1) validation

This eliminates repeated database lookups (3 per playback event) by caching
the collection ID once at startup. For libraries with thousands of movies,
this significantly reduces overhead when non-TubeArchivist media is played.

Performance improvement:
- Before: 3 calls per playback event for all media types
- After: 0 calls for rejected media (type check), 1-3 for Episodes only
When configuration is saved, Jellyfin creates a new configuration object,
so we need to pass the new collection title directly to the refresh method
instead of reading from the Configuration property which may still hold
the old value during the setter execution.

Changes:
- Pass collectionTitle parameter to RefreshTubeArchivistCollectionId()
- Update CacheTubeArchivistCollectionId() to accept optional title parameter
- Prevents searching for stale collection name when config changes
@DarkFighterLuke

Copy link
Copy Markdown
Collaborator

Could you please align with the latest modifications?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants