Storage & Configuration
Configure where and how your application stores its cached content (Memory vs Disk).
Cache Storage Strategies
JopiJS provides two built-in storage mechanisms for its cache engine. Choosing the right one depends on your deployment environment and persistence needs.
1. In-Memory Cache (Default)
The default strategy stores all cached HTML and data directly in the application's RAM.
Pros:
- โก Ultra-fast: No file I/O overhead.
- ๐งน Self-cleaning: Automatically managed by the Garbage Collector based on memory usage or item count.
- ๐ Zero setup: Works out of the box.
Cons:
- ๐ฅ Volatile: Cache is cleared when the server restarts or (by default) during hot-reloads in development.
- ๐ง Memory limited: Large sites might consume significant RAM.
Configuration
You can customize the memory cache limits to fit your hosting environment (e.g., small VPS vs large instance).
import { jopiApp } from "jopijs";
jopiApp.startApp(
import.meta,
website => {
website.configure_htmlCache()
.use_inMemoryCache({
// Maximum memory usage in Megabytes (default: 500)
maxMemoryUsage_mo: 1024,
// Maximum number of items (default: 5000)
maxItemCount: 10000,
// Bun.js only: Clean cache on hot-reload? (default: false)
// Set to true to force fresh data when you change code.
clearOnHotReload: true
});
}
);2. File System Cache (Disk)
This strategy stores cached content as files on the disk.
Pros:
- ๐พ Persistent: Cache survives server restarts and deployments.
- ๐ฆ Large capacity: Only limited by disk space.
- ๐ Shared potential: Can be useful in some containerized setups (though usually, memory is preferred for speed).
Cons:
- ๐ข Slower than RAM: Involves file I/O (though JopiJS optimizes this heavily).
- ๐งน Cleanup: Requires management of disk space over time.
Configuration
To switch to disk-based caching, specify the directory where files should be stored.
import { jopiApp } from "jopijs";
import path from "node:path";
jopiApp.startApp(
import.meta,
website => {
website.configure_htmlCache()
// Store cache in a "cache" folder at the project root
.use_fileSystemCache(path.resolve("./.cache_store"));
}
);Detailed Configuration Options
InMemoryCacheOptions
| Option | Type | Default | Description |
|---|---|---|---|
maxMemoryUsage_mo | number | 500 | Maximum estimated memory usage in MB before GC kicks in. |
maxItemCount | number | 5000 | Maximum number of entries (HTML pages + data objects). |
clearOnHotReload | boolean | false | If true, clears the cache when development hot-reload occurs. |
maxContentLength | number | 600kb | Items larger than this (in bytes) won't be cached to save memory. |