All Projects → fastify → fastify-caching

fastify / fastify-caching

Licence: MIT license
A Fastify plugin to facilitate working with cache headers

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to fastify-caching

fastify-file-upload
Fastify plugin for uploading files
Stars: ✭ 68 (-44.72%)
Mutual labels:  fastify, fastify-plugin
fastify-postgres
Fastify PostgreSQL connection plugin
Stars: ✭ 144 (+17.07%)
Mutual labels:  fastify, fastify-plugin
create-fastify-app
An utility that help you to generate or add plugin to your Fastify project
Stars: ✭ 53 (-56.91%)
Mutual labels:  fastify, fastify-plugin
fastify-autoroutes
fastest way to map directories to URLs in fastify
Stars: ✭ 70 (-43.09%)
Mutual labels:  fastify, fastify-plugin
fastify-env
Fastify plugin to check environment variables
Stars: ✭ 129 (+4.88%)
Mutual labels:  fastify, fastify-plugin
fastify-hasura
A Fastify plugin to have fun with Hasura.
Stars: ✭ 30 (-75.61%)
Mutual labels:  fastify, fastify-plugin
fastify-vite
This plugin lets you load a Vite client application and set it up for Server-Side Rendering (SSR) with Fastify.
Stars: ✭ 497 (+304.07%)
Mutual labels:  fastify, fastify-plugin
fastify-webpack-hmr
Webpack hot module reloading for Fastify
Stars: ✭ 29 (-76.42%)
Mutual labels:  fastify, fastify-plugin
fastify-openapi-glue
A plugin for Fastify to autogenerate a configuration based on a OpenApi(v2/v3) specification.
Stars: ✭ 94 (-23.58%)
Mutual labels:  fastify, fastify-plugin
fastify-cors
Fastify CORS
Stars: ✭ 212 (+72.36%)
Mutual labels:  fastify, fastify-plugin
fastify-cron
Run cron jobs alongside your Fastify server 👷
Stars: ✭ 32 (-73.98%)
Mutual labels:  fastify, fastify-plugin
fastify-axios
Add axios http client to your fastify instance
Stars: ✭ 28 (-77.24%)
Mutual labels:  fastify, fastify-plugin
fastify-awilix
Dependency injection support for fastify
Stars: ✭ 52 (-57.72%)
Mutual labels:  fastify, fastify-plugin
fastify-loader
The route loader for the cool kids!
Stars: ✭ 17 (-86.18%)
Mutual labels:  fastify, fastify-plugin
session
Session plugin for fastify
Stars: ✭ 52 (-57.72%)
Mutual labels:  fastify, fastify-plugin
fastify-etag
Automatically generate etags for HTTP responses, for Fastify
Stars: ✭ 61 (-50.41%)
Mutual labels:  fastify, fastify-plugin
fastify-csrf
A fastify csrf plugin.
Stars: ✭ 88 (-28.46%)
Mutual labels:  fastify, fastify-plugin
fastify-leveldb
Plugin to share a common LevelDB connection across Fastify.
Stars: ✭ 19 (-84.55%)
Mutual labels:  fastify, fastify-plugin
fastify-passport
Use passport strategies for authentication within a fastify application
Stars: ✭ 150 (+21.95%)
Mutual labels:  fastify, fastify-plugin
fastify-vue
A nuxt.js fastify plugin
Stars: ✭ 27 (-78.05%)
Mutual labels:  fastify, fastify-plugin

@fastify/caching

CI NPM version js-standard-style

@fastify/caching is a plugin for the Fastify framework that provides server-side caching and mechanisms for manipulating HTTP cache headers according to RFC 2616 §14.9.

Supports Fastify versions ^3.0.0. Version v5.x supports Fastify ^3.0.0 in the v5.x branch.

This plugin fully supports Fastify's encapsulation. Therefore, routes that should have differing cache settings should be registered within different contexts.

In addition to providing header manipulation, the plugin also decorates the server instance with an object that can be used for caching items. Note: the default cache should not be used in a "production" environment. It is an LRU, in-memory cache that is capped at 100,000 items. It is highly recommended that a full featured cache object be supplied, e.g. abstract-cache-redis.

Example

This example shows using the plugin to disable client side caching of all routes.

const http = require('http')
const fastify = require('fastify')()
const fastifyCaching = require('@fastify/caching')

fastify.register(
  fastifyCaching,
  {privacy: fastifyCaching.privacy.NOCACHE},
  (err) => { if (err) throw err }
)

fastify.get('/', (req, reply) => {
  reply.send({hello: 'world'})
})

fastify.listen({ port: 3000 }, (err) => {
  if (err) throw err

  http.get('http://127.0.0.1:3000/', (res) => {
    console.log(res.headers['cache-control'])
  })
})

This example shows how to register the plugin such that it only provides a server-local cache. It will not set any cache control headers on responses. It also shows how to retain a reference to the cache object so that it can be re-used.

const IORedis = require('ioredis')
const redis = new IORedis({host: '127.0.0.1'})
const abcache = require('abstract-cache')({
  useAwait: false,
  driver: {
    name: 'abstract-cache-redis', // must be installed via `npm i`
    options: {client: redis}
  }
})

const fastify = require('fastify')()
fastify
  .register(require('@fastify/redis'), {client: redis})
  .register(require('@fastify/caching'), {cache: abcache})

fastify.get('/', (req, reply) => {
  fastify.cache.set('hello', {hello: 'world'}, 10000, (err) => {
    if (err) return reply.send(err)
    reply.send({hello: 'world'})
  })
})

fastify.listen({ port: 3000 }, (err) => {
  if (err) throw err
})

API

Options

@fastify/caching accepts the options object:

{
  privacy: 'value',
  expiresIn: 300,
  cache: {get, set},
  cacheSegment: 'segment-name'
}
  • privacy (Default: undefined): can be set to any string that is valid for a cache-response-directive as defined by RFC 2616.
  • expiresIn (Default: undefined): a value, in seconds, for the max-age the resource may be cached. When this is set, and privacy is not set to no-cache, then ', max-age=<value>' will be appended to the cache-control header.
  • cache (Default: abstract-cache.memclient): an abstract-cache protocol compliant cache object. Note: the plugin requires a cache instance to properly support the ETag mechanism. Therefore, if a falsy value is supplied the default will be used.
  • cacheSegment (Default: '@fastify/caching'): segment identifier to use when communicating with the cache.
  • serverExpiresIn (Default: undefined): a value, in seconds, for the length of time the resource is fresh and may be held in a shared cache (e.g. a CDN). Shared caches will ignore max-age when this is specified, though browsers will continue to use max-age. Should be used with expiresIn, not in place of it. When this is set, and privacy is set to public, then ', s-maxage=<value>' will be appended to the cache-control header.

reply.etag(string, number)

This method allows setting of the etag header. It accepts any arbitrary string. Be sure to supply a string that is valid for HTTP headers.

If a tag string is not supplied then uid-safe will be used to generate a tag. This operation will be performed synchronously. It is recommended to always supply a value to this method to avoid this operation.

All incoming requests to paths affected by this plugin will be inspected for the if-none-match header. If the header is present, the value will be used to lookup the tag within the cache associated with this plugin. If the tag is found, then the response will be ended with a 304 status code sent to the client.

Tags will be cached according to the second parameter. If the second parameter is supplied, and it is an integer, then it will be used as the time to cache the etag for generating 304 responses. The time must be specified in milliseconds. The default lifetime, when the parameter is not specified, is 3600000.

reply.expires(date)

This method allows setting of the expires header. It accepts a regular Date object, or a string that is a valid date string according to RFC 2616 §14.21.

License

MIT License

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].