Outdated render cache
Nutrient caches rendered pages to reduce page access time. PdfFragment, PdfActivity, and manual rendering through PdfDocument use cached pages when available. Nutrient tracks document changes and invalidates the cache when necessary.
How caching works
Nutrient usually caches rendered pages in memory. PdfActivity and PdfFragment also cache rendered pages and thumbnails on disk by default.
Nutrient generates a unique cache key for each rendered page. If that key already has a cached image, Nutrient returns the image without rendering again.
Cache key generation
A page’s cache key includes the document’s unique identifier (UID) and page index. Disk cache entries also include a hash of document data.
The hash helps PdfActivity and PdfFragment detect a different PDF at the same path or provider. It samples only each source’s beginning and end, so it doesn’t detect every change.
Retrieve the UID with document.getUid(). Nutrient derives it from the document source:
- Data provider — Nutrient calls
DataProvider’sgetUid()method. - Local file — Nutrient hashes the file path.
- Compound document — If the first source is a data provider, Nutrient uses that provider’s UID. Otherwise, it hashes source file paths, or provider UIDs when sources mix files and providers.
For multiple sources, refer to the compound documents guide.
Log document.getUid() to inspect a PdfDocument instance’s UID.
What causes the cache to become stale?
Some changes require manual cache invalidation. A stale cache can cause flickering pages or incorrect thumbnails.
These operations can leave cached pages outdated:
- Changing an open PDF externally, such as adding annotations through another app.
- Disabling autosave and manually saving the
PdfDocument. - Reusing a provider UID across documents or changing bytes without reporting changed document data.
For save behavior, refer to the autosave guide.
Invalidate the cache manually when Nutrient doesn’t detect a file change.
Manual cache invalidation
Choose a method based on the scope of the change:
- Call
PdfDocument#invalidateCacheForPage()to invalidate one page. - Call
PdfDocument#invalidateCache()to invalidate every page in a document. - Call
Nutrient#clearCaches()to invalidate all documents’ render caches. Use this sparingly because clearing all caches can slow page access.