|
| 1 | +# @fortedigital/nextjs-cache-handler |
| 2 | + |
| 3 | +This package extends the functionality of [`@neshca/cache-handler`](https://www.npmjs.com/package/@neshca/cache-handler) by providing additional cache handlers for specialized use cases, specifically for Redis-based caching solutions. The original `@neshca/cache-handler` offers a robust caching API for Next.js applications, and this package introduces two new handlers for managing Redis cache with different expiration strategies and tag-based revalidation. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +To install this package along with its dependencies: |
| 8 | + |
| 9 | +```bash |
| 10 | +npm install @fortedigital/nextjs-cache-handler |
| 11 | +``` |
| 12 | + |
| 13 | +Ensure that you have already set up `@neshca/cache-handler` before integrating these extended handlers. |
| 14 | + |
| 15 | +## Handlers |
| 16 | + |
| 17 | +### 1. `redis-strings` |
| 18 | + |
| 19 | +This handler is designed to manage cache operations using Redis strings. It supports key-based and tag-based caching with flexible expiration strategies. |
| 20 | + |
| 21 | +#### Features: |
| 22 | + |
| 23 | +- **Key Expiration Strategy**: Choose between `EXAT` (more efficient with Redis 6.2 or newer) or `EXPIREAT` (compatible with Redis 4.0 or newer). |
| 24 | +- **Tag-based Revalidation**: Supports cache revalidation using tags for efficient and fine-grained cache invalidation. |
| 25 | +- **TTL Management**: Automatically manages time-to-live (TTL) for cache keys and tags. |
| 26 | + |
| 27 | +#### Usage: |
| 28 | + |
| 29 | +```js |
| 30 | +const redisHandler = await createHandler({ |
| 31 | + client, // Redis client instance |
| 32 | + keyPrefix: "myApp:", |
| 33 | + sharedTagsKey: "myTags", |
| 34 | + sharedTagsTtlKey: "myTagTtls", |
| 35 | +}); |
| 36 | +``` |
| 37 | + |
| 38 | +### 2. `composite` |
| 39 | + |
| 40 | +The composite handler allows for managing cache operations using a combination of other handlers. It provides a flexible way to route cache operations to multiple underlying handlers based on a defined strategy. |
| 41 | + |
| 42 | +#### Features: |
| 43 | + |
| 44 | +- **Flexible Handling**: Supports multiple handlers, enabling complex caching strategies. |
| 45 | +- **Custom Set Strategy**: Allows you to define how cache entries are routed to different handlers. |
| 46 | +- **On-Demand Revalidation**: Retrieves values from the first available handler, ensuring efficient data access. |
| 47 | + |
| 48 | +#### Usage: |
| 49 | + |
| 50 | +```js |
| 51 | +const compositeHandler = createHandler({ |
| 52 | + handlers: [handler1, handler2], // Array of underlying handlers |
| 53 | + setStrategy: (data) => (data?.tags.includes("handler1") ? 0 : 1), |
| 54 | +}); |
| 55 | +``` |
| 56 | +
|
| 57 | +## Reference to Original Package |
| 58 | +
|
| 59 | +This package builds upon the core functionality provided by [`@neshca/cache-handler`](https://www.npmjs.com/package/@neshca/cache-handler). You can find more information about the core library, including usage examples and API documentation, at the [official documentation page](https://caching-tools.github.io/next-shared-cache). |
| 60 | +
|
| 61 | +## License |
| 62 | +
|
| 63 | +This project is licensed under the [MIT License](./LICENSE), as is the original `@neshca/cache-handler` package. |
0 commit comments