Appearance
Cache Backend
This module uses unstorage to handle the cache layer. This means it's possible to use any cache backend. By default the memory
backend is used.
If a Nuxt app runs several instances you cannot use a memory cache anymore, since purging a cache via the API would only affect a single instance. To solve that you have to use an external cache backend.
Example using Redis
This minimal example uses the Redis driver provided by unstorage.
typescript
import { defineMultiCacheOptions } from 'nuxt-multi-cache/dist/runtime/serverOptions'
import redisDriver from 'unstorage/drivers/redis'
export default defineMultiCacheOptions({
component: {
storage: {
driver: redisDriver({
base: 'component:',
}),
},
},
})
Custom Driver
Checkout the full example on how to create a custom driver.
This example recreates the default storage (in-memory) using a simple cache
object.
typescript
import { defineMultiCacheOptions } from 'nuxt-multi-cache/dist/runtime/serverOptions'
import { defineDriver } from 'unstorage'
const customDriver = defineDriver((_opts) => {
let cache = {}
return {
hasItem(key: string) {
return !!cache[key]
},
getItem(key: string) {
return cache[key]
},
setItem(key, value) {
return (cache[key] = value)
},
removeItem(key) {
cache[key] = undefined
},
getKeys() {
return Object.keys(cache)
},
clear() {
cache = {}
},
dispose() {},
}
})
export default defineMultiCacheOptions({
component: {
storage: {
driver: customDriver(),
},
},
})