Skip to content

Commit ce16792

Browse files
committed
Updated readme and 2.3.0 version
1 parent 5d86d6b commit ce16792

File tree

3 files changed

+85
-3
lines changed

3 files changed

+85
-3
lines changed

README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,88 @@ This project was originally based on [`@neshca/cache-handler`](https://www.npmjs
473473
474474
For context or historical documentation, you may still reference the [original project](https://caching-tools.github.io/next-shared-cache).
475475
476+
## neshClassicCache
477+
478+
⚠️ Deprecated: This function was migrated from @neshca for compatibility purposes only. Use with caution - no further development or support is planned.
479+
480+
`neshClassicCache` allows you to cache the results of expensive operations, like database queries, and reuse them across multiple requests. Unlike the [`neshCache`](/functions/nesh-cache) or [`unstable_cache` ↗](https://nextjs.org/docs/app/api-reference/functions/unstable_cache) function, `neshClassicCache` must be used in a Next.js Pages Router allowing users to cache data in the `getServerSideProps` and API routes.
481+
482+
483+
> [!NOTE]
484+
>
485+
> Cache entries created with `neshClassicCache` can be revalidated only by the [`revalidateTag` ↗](https://nextjs.org/docs/app/api-reference/functions/revalidateTag) method.
486+
487+
### Parameters
488+
489+
#### `fetchData`
490+
491+
This is an asynchronous function that fetches the data you want to cache. It must be a function that returns a `Promise`.
492+
493+
#### `commonOptions`
494+
495+
This is an object that controls how the cache behaves. It can contain the following properties:
496+
497+
- `tags` - An array of tags to associate with the cached result. Tags are used to revalidate the cache using the `revalidateTag` and `revalidatePath` functions.
498+
499+
- `revalidate` - The revalidation interval in seconds. Must be a positive integer or `false` to disable revalidation. Defaults to `export const revalidate = time;` in the current route.
500+
501+
- `argumentsSerializer` - A function that serializes the arguments passed to the callback function. Use it to create a cache key. Defaults to `JSON.stringify(args)`.
502+
503+
- `resultSerializer` - A function that serializes the result of the callback function.Defaults to `Buffer.from(JSON.stringify(data)).toString('base64')`.
504+
505+
- `resultDeserializer` - A function that deserializes the string representation of the result of the callback function. Defaults to `JSON.parse(Buffer.from(data, 'base64').toString('utf-8'))`.
506+
507+
- `responseContext` - The response context object. If provided, it is used to set the cache headers acoording to the `revalidate` option. Defaults to `undefined`.
508+
509+
### Returns
510+
511+
`neshClassicCache` returns a function that when invoked, returns a `Promise` that resolves to the cached data. If the data is not in the cache, the provided function will be invoked, and its result will be cached and returned. The first argument is the `options` which can be used to override the common [`options`](/functions/nesh-classic-cache#commonoptions). In addition, there is a `cacheKey` option that can be used to provide a custom cache key.
512+
513+
### Example
514+
515+
```jsx filename="src/pages/api/api-example.js" copy
516+
import { neshClassicCache } from '@fortedigital/nextjs-cache-handler/functions';
517+
import axios from 'axios';
518+
519+
export const config = {
520+
runtime: 'nodejs',
521+
};
522+
523+
async function getViaAxios(url) {
524+
try {
525+
return (await axios.get(url.href)).data;
526+
} catch (_error) {
527+
return null;
528+
}
529+
}
530+
531+
const cachedAxios = neshClassicCache(getViaAxios);
532+
533+
export default async function handler(request, response) {
534+
if (request.method !== 'GET') {
535+
return response.status(405).send(null);
536+
}
537+
538+
const revalidate = 5;
539+
540+
const url = new URL('https://api.example.com/data.json');
541+
542+
// Add tags to be able to revalidate the cache
543+
const data = await cachedAxios(
544+
{ revalidate, tags: [url.pathname], responseContext: response },
545+
url,
546+
);
547+
548+
if (!data) {
549+
response.status(404).send('Not found');
550+
551+
return;
552+
}
553+
554+
response.json(data);
555+
}
556+
```
557+
476558
---
477559
478560
## License

packages/nextjs-cache-handler/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/nextjs-cache-handler/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"next",
1919
"redis"
2020
],
21-
"version": "2.2.0",
21+
"version": "2.3.0",
2222
"type": "module",
2323
"license": "MIT",
2424
"description": "Next.js cache handlers",

0 commit comments

Comments
 (0)