State & Local Resource Caching
Relevant source files
The following files were used as context for generating this wiki page:
The HoloPrint project utilizes a multi-layered caching strategy to optimize performance, minimize redundant network requests, and provide a responsive user experience during the resource pack generation process. Caching is applied to remote vanilla Minecraft data, internal function results, and user-uploaded resource pack assets.
This system ensures that high-volume data, such as block shapes, texture mappings, and version-specific metadata, is stored within the browser's Cache Storage or in-memory, allowing for near-instant retrieval on subsequent operations.
Sources: TERMS_OF_USE.md:21-22, src/fetchers.js:1-10
Persistent Remote Data Caching
The application uses the browser's caches API to persist data fetched from GitHub CDNs. This is primarily handled by the createCachingFetcher factory, which manages versioned caches for vanilla data, bedrock data, and block upgrade schemas.
Fetcher Architecture
Each fetcher is initialized with a specific name, owner, repository, and version. The system automatically handles cache rotation by deleting old versions and maintaining only the most recent previous version for fallback.
The diagram shows the logic flow of the createCachingFetcher when a resource is requested, including the fallback to previous cache versions and change detection.
Sources: src/fetchers.js:34-70
Key Fetcher Components
- Version Management: The
sortVersionsutility ensures that caches are compared and pruned based on semantic-like version strings derived from GitHub tags. - Change Detection: It fetches a
changedFiles.txtlist from a repository tracker to determine if a file in the previous cache version is still valid for the current version. - Resilience: The
retrievefunction implements a retry mechanism (up to 3 attempts) specifically for "Failed to fetch" errors often encountered in Chrome when requesting large volumes of images simultaneously.
Sources: src/fetchers.js:72-115, src/fetchers.js:123-146
In-Memory Function Caching (Memoization)
For intensive operations that occur during the pack generation lifecycle, HoloPrint employs specialized memoization utilities to cache function results based on input arguments.
Weak and Conditional Caching
The application distinguishes between simple memoization and conditional caching based on resource type.
| Cache Utility | Implementation Logic | Usage Example |
|---|---|---|
weaklyCacheUnaryFunc | Uses a WeakMap to store results of a function, typically for objects that can be garbage collected. | Caching the results of readStructureNBT. |
conditionallyCacheUnaryFunc | Caches results only if a specific condition is met (e.g., path matching). | Caching icon textures in fetchPackTemplateFile. |
Sources: src/HoloPrint.js:338-348, src/HoloPrint.js:380-388
User-Facing Cache Management
HoloPrint provides users with direct control over the locally stored resource pack data. This is essential for troubleshooting or when updated resources are required.
Cache Clearing Logic
Users can manually purge the caches storage through the UI. This is implemented in src/index.js via a listener on the #clearResourcePackCacheButton.
The sequence shows the process of clearing persistent browser caches initiated by the user.
Sources: src/index.js:343-347, src/index.html:150-150
Caching Configurations & Constants
The system uses specific URL prefixes and status codes to manage cache validity.
| Constant | Value / Description |
|---|---|
CACHE_URL_PREFIX | https://cache/ - Used as a virtual origin for internal cache keys. |
BAD_STATUS_CODES | [429, 500, 502, 503] - Responses with these codes are never cached or are purged if found. |
GITHUB_CDN | https://cdn.jsdelivr.net/gh - The primary source for remote resources. |
Sources: src/fetchers.js:10-14
State Initialization & Persistence
Upon page load, the application performs a cleanup of legacy cache namespaces (e.g., those prefixed with ResourcePackStack_) to ensure the storage remains optimized.
// src/index.js:464-467
caches.keys().then(cacheNames => {
cacheNames.filter(cacheName => cacheName.startsWith("ResourcePackStack_"))
.forEach(cacheName => caches.delete(cacheName))
});
Sources: src/index.js:464-467
In summary, State & Local Resource Caching in HoloPrint is a robust system combining browser persistent storage for network resources and in-memory memoization for processing logic. This dual approach minimizes bandwidth usage and significantly reduces the computation time required to generate .mcpack files.