Skip to content

Commit

Permalink
add route cache readme (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
denbon05 authored Nov 1, 2024
1 parent 0b711b3 commit 3552f41
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ data on first request and use it next time until <a href="https://en.wikipedia.o
npm i fastify-lcache
```

<h2>Table of Contents</h2>

- [Example](#example)
- [Plugin API](#api)
- [Route caching](#route-caching)

## Example

```ts
Expand Down Expand Up @@ -62,6 +68,7 @@ axios.get(url);
statusesToCache?: [200],
methodsToCache?: ['GET'],
excludeRoutes?: [],
includeRoutes?: '*'
}
```

Expand All @@ -88,6 +95,31 @@ interface ILightCache {
}
```

### Route caching

If a route matches any pattern in `excludeRoutes`, it will **never be cached**.

**Unspecified Routes**<br />
If a route is neither in `includeRoutes` nor `excludeRoutes`:<br />
When `includeRoutes` specifies particular routes, only those specified are cached,
while all other routes are ignored by the cache.

```js
// Scenario 1: includeRoutes: '*' with specific excludeRoutes
// Behavior: All routes are cached except '/auth/*' and '/admin/*'
{
includeRoutes: '*',
excludeRoutes: ['/auth*', '/admin*']
}
*
// Scenario 2: Specific Routes for includeRoutes and excludeRoutes
// Behavior: Only '/api/users' and '/api/orders' are cached, and the rest is ignored
{
includeRoutes: ['/api/users', '/api/orders']
excludeRoutes: ['/api/secret'],
}
```

### Fastify version compatibility

| Fastify | lcache |
Expand Down
2 changes: 1 addition & 1 deletion lib/types/lcache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export interface ICacheOptions {
*
* @example
* // Scenario 1: includeRoutes: '*' with specific excludeRoutes
* // Behavior: All routes are cached except '/auth/*' and '/admin/*'
* // Behavior: All routes are cached except '/auth*' and '/admin*'
* {
* includeRoutes: '*',
* excludeRoutes: ['/auth/*', '/admin/*']
Expand Down

0 comments on commit 3552f41

Please sign in to comment.