2.2.1 • Published 8 months ago

fastify-lcache v2.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

fastify-lcache

Maintainability Test Coverage

npm i fastify-lcache

Example

// your app
import fastify from "fastify";
import lcache from "fastify-lcache";

const app = fastify();
const address = "0.0.0.0";
const port = 4000;

app.register(lcache, {
  ttlInMinutes: 10, // set cached data lifetime to 10 minutes
});

app.after(() => {
  // add your routes
  app.get("/ping", async (req, reply) => {
    reply.send("pong");
  });
});

app.listen(port, address);
// client wants data from your app
const url = "http://0.0.0.0:4000/ping";
// first request will return origin data from route '/ping'
// and put result to the cache
axios.get(url);
// the following requests within 10 minutes
// will return cached data on this route
axios.get(url);

API

Options (default)

{
  ttlInMinutes?: 5,
  disableCache?: false,
  statusesToCache?: [200],
  methodsToCache?: ['GET'],
  excludeRoutes?: [],
  includeRoutes?: '*'
}

On fastify instance

interface ILightCache {
  // Get cached data
  get<T>(key: string): T;

  // Set data to cache
  set<T>(key: string, value: T): void;

  // Check if data exists in cache
  has(key: string): boolean;

  // Clear all data in cache if key not specified
  reset(key?: string): void;

  // Clear Interval which check data lifetime
  destroy(): void;
}

Route caching

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

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

// 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

Fastifylcache
^4.102.x
3-4.91-1.2
2.2.1

8 months ago

2.2.0

8 months ago

2.1.4

8 months ago

2.1.5

8 months ago

2.1.3

8 months ago

2.1.1

12 months ago

2.1.0

1 year ago

2.0.2

1 year ago

2.0.1

1 year ago

2.0.0

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.0

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago