Cache
When writing Rspack plugins, you can use compiler.getCache(name: string) or compilation.getCache(name: string) to get the cache object which can share data in the build process. The cache data is stored on the Compiler, so it can be used in multiple Compilations in the watch mode.
- Only available when cache: true which it is enabled in
mode="development"by default. - Only used for the JavaScript plugins, the Rust cache cannot be accessed.
- Only memory cache is provided, persistent cache is not supported yet.
Example
The following code finds out the newly added assets in the processAssets:
Methods
Caching
get/getPromise
Get cache data asynchronously, callback by function or promise.
- Type:
get:<T>(identifier: string, etag: Etag | null, callback: (err: Error, result: T) => void): voidgetPromise:<T>(identifier: string, etag: Etag | null): Promise<T>;
- Arguments:
- identifier: ID of data item
- etag: Etag of the data item, can be generated by
getLazyHashedEtag
store/storePromise
Store cache data asynchronously, callback by function or promise.
- Type:
store:<T>(identifier: string, etag: Etag | null, data: T, callback: (err: Error) => void): void;storePromise:<T>(identifier: string, etag: Etag | null): Promise<T>;
- Arguments:
- identifier: ID of data item
- etag: Etag of the data item, can be generated by
getLazyHashedEtag
provide/providePromise
Try to get cache data asynchronously, call the computer function to generate when not exists, callback by function or promise.
- Type:
provide:providePromise
- Arguments:
- identifier: ID of data item
- etag: Etag of the data item, can be generated by
getLazyHashedEtag - computer: The called generating function when cache is not exists
getLazyHashedEtag/mergeEtags
By using the getLazyHashedEtag and mergeEtags methods, an etag can be created as the unique identifier of the data item. It will not be calculated immediately when created, but rather delayed until it is used, and also can be cached. This can be used to improve performance when complex data objects are used as the unique identifier.
getLazyHashedEtag:(obj: HashableObject): Etag, calculates the hash of the object to generate the etag as the data identifier, the object needs to implement theupdateHash(hash: Hash).mergeEtags:(a: Etag, b: Etag): Etag, merges two etags to one.
getItemCache
By using the getItemCache method, a cache object for a single data item can be created. This cache object provides simplified data access methods, do not need identifier and etag as arguments any more.
- Type:
(identifier, etag): ItemCacheFacade
getChildCache
By using the getChildCache method, a child cache object can be generated, with its interface being completely consistent, and it can be utilized when there are numerous caches that require grouping for storage.
- Type:
(name: string): CacheFacade

