Options
All
  • Public
  • Public/Protected
  • All
Menu

Allows you to split or normalize the cache space for a given route. Common use cases include:

  • serving multiple variants of the same URL based on a currency and/or language cookie.
  • caching different responses based on device type
  • ignoring all but a specific set of query parameters when looking up a response from the cache.

Example

 import { Router, CustomCacheKey } from '@xdn/core/router'

 new Router()
   .match('/some-path', ({ cache }) => {
     cache({
       edge: {
         maxAgeSeconds: 60 * 60,
         key: new CustomCacheKey()
           .excludeAllQueryParametersExcept('color', 'size')
           .addCookie('currency')
           .addCookie('location', cookie => {
             cookie.group('na').byPattern('us|ca')
             cookie.group('eur').byPattern('de|fr|ee')
           })
       }
     })
   })

Hierarchy

  • CustomCacheKey

Index

Methods

addBrowser

  • addBrowser(): this
  • Adds the x-xdn-browser header.

    Returns this

    A self-reference, suitable for chaining.

addCookie

  • Adds a cookie to the cache key.

    Parameters

    • name: string

      The name of the cookie

    • createGroups: GroupCreator

      An function that accepts a Groups instance and calls groups.group(name) to group multiple values into a single cache key component

    Returns this

    A self-reference, suitable for chaining.

addDevice

  • addDevice(): this
  • Adds the x-xdn-device header.

    Returns this

    A self-reference, suitable for chaining.

addHeader

  • addHeader(name: string, createGroups?: GroupCreator): this
  • Adds a request header to the cache key.

    Parameters

    • name: string

      The name of the request header

    • Optional createGroups: GroupCreator

      An function that accepts a Groups instance and calls groups.group(name) to group multiple values into a single cache key component

    Returns this

    A self-reference, suitable for chaining.

addIsBot

  • addIsBot(): this
  • Adds the x-xdn-device-is-bot header.

    Returns this

    A self-reference, suitable for chaining.

addVendor

  • addVendor(): this
  • Adds the x-xdn-vendor header.

    Returns this

    A self-reference, suitable for chaining.

excludeAllQueryParameters

  • excludeAllQueryParameters(): this
  • Excludes the entire query string from the cache key.

    Returns this

    A self-reference, suitable for chaining.

excludeAllQueryParametersExcept

  • excludeAllQueryParametersExcept(...names: string[]): this
  • Excludes all query parameters except those provided from the cache key. We also refer to this as "whitelisting" query parameters. We recommend using this method over excludeQueryParameters as it's difficult to know all of the query parameters your application might receive and unexpected query parameters can lead to significantly lower cache hit rates.

    Parameters

    • Rest ...names: string[]

      The names of the query parameters to whitelist

    Returns this

    A self-reference, suitable for chaining.

excludeQueryParameters

  • excludeQueryParameters(...names: string[]): this
  • Excludes the provided query parameters from the cache key. All other parameters will still be part of the cache key. We also refer to this as "blacklisting" query parameters.

    Parameters

    • Rest ...names: string[]

      The names of the query parameters to blacklist

    Returns this

    A self-reference, suitable for chaining.

toJSON

Generated using TypeDoc