Wrapper around dataloader to cache the results of the loader
npm install @labdigital/dataloader-cache-wrapperts
import { DataLoaderCache } from "@labdigital/dataloader-cache-wrapper"const loader = new DataLoaderCache(ProductDataLoader, {
cache: {
storeFn: () => new Keyv(),
ttl: 3600,
},
cacheKeyFn: (ref: ProdProductReferenceuctRef) => {
const key =
${ref.store}-${ref.locale}-${ref.currency};
return some-data:${key}:id:${ref.slug}
},
maxBatchSize: 50,
});`Or use the older API with the
dataloaderCache function:`ts
import { dataloaderCache } from "@labdigital/dataloader-cache-wrapper"export const createProductBySlugLoader = () => {
return new DataLoader(ProductDataLoader, {
maxBatchSize: 50,
});
};
export const ProductDataLoader = async (keys: readonly any[]): Promise<(Product | null)[]> => {
return dataloaderCache(_uncachedProductDataLoader, keys, {
store: new Keyv(),
ttl: 3600,
cacheKeysFn: (ref: ProductReference) => {
const key =
${ref.store}-${ref.locale}-${ref.currency};
return [some-data:${key}:id:${ref.slug}];
},
})
}
``