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).

src/server/init.ts
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.

src/server/init.ts
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

OptionTypeDefaultDescription
maxMemoryUsage_monumber500Maximum estimated memory usage in MB before GC kicks in.
maxItemCountnumber5000Maximum number of entries (HTML pages + data objects).
clearOnHotReloadbooleanfalseIf true, clears the cache when development hot-reload occurs.
maxContentLengthnumber600kbItems larger than this (in bytes) won't be cached to save memory.