Usage
The server is launched, to which the required cacheHandler is passed:
// @ts-check
const { createServer } = require('@nimpl/cache-adapter');
const CacheHandler = require('@nimpl/cache-in-memory');
const server = createServer(new CacheHandler({}));
server.listen('4000', () => {
console.log('Server is running at http://localhost:4000');
});
A special adapter is set up to work with the cache:
cache-handler.js
// @ts-check
const { AppAdapter } = require('@nimpl/cache-adapter');
const CacheHandler = require('@nimpl/cache-in-memory');
class CustomCacheHandler extends AppAdapter {
/** @param {any} options */
constructor(options) {
super({
CacheHandler,
buildId: process.env.BUILD_ID || 'base_id',
cacheUrl: 'http://localhost:4000',
cacheMode: 'remote',
options,
})
}
}
module.exports = CustomCacheHandler;
The created cache-handler is connected in the next.js configuration:
next.config.js
module.exports = {
cacheHandler: require.resolve('./cache-handler.js'),
cacheMaxMemorySize: 0, // disable default in-memory caching
}
That’s it. Now the cache is configured.