3.3.2 • Published 1 year ago

slonik-interceptor-query-cache v3.3.2

Weekly downloads
136
License
BSD-3-Clause
Repository
github
Last release
1 year ago

slonik-interceptor-query-cache

NPM version Canonical Code Style Twitter Follow

Caches Slonik query results.

Usage

Query cache interceptor is initialized using a custom storage service. The Example Usage documentation shows how to create a compatible storage service using node-cache.

Which queries are cached is controlled using cache attributes. Cache attributes are comments starting with -- @cache- prefix. Only queries with cache attributes are cached (see Cache attributes)

Behavior

  • Does not cache queries inside of a transaction.

Cache attributes

@cache-ttl

RequiredFormatDefault
Yes/^d+$/N/A

Number (in seconds) to cache the query for.

@cache-key

RequiredFormatDefault
No/^[$A-Za-z0-9\-_:]+$/$bodyHash:$valueHash

Cache key that uniquely identifies the query.

If present, $bodyHash is substituted with the hash of the query (comments and white-spaces are stripped before hashing the query).

If present, $valueHash is substituted with the hash of the parameter values.

@cache-discard-empty

RequiredFormatDefault
No/^(false\|true)$/false

If set to true, then storage.set is not invoked when query produces no results.

Example usage

This example shows how to create a compatible storage service using node-cache.

import NodeCache from 'node-cache';
import {
  createPool
} from 'slonik';
import {
  createQueryCacheInterceptor
} from 'slonik-interceptor-query-cache';

const nodeCache = new NodeCache({
  checkperiod: 60,
  stdTTL: 60,
  useClones: false,
});

const pool = await createPool('postgres://', {
  interceptors: [
    createQueryCacheInterceptor({
      storage: {
        get: (query, cacheAttributes) => {
          // Returning null results in the query being executed.
          return cache.get(cacheAttributes.key) || null;
        },
        set: (query, cacheAttributes, queryResult) => {
          cache.set(cacheAttributes.key, queryResult, cacheAttributes.ttl);
        },
      },
    }),
  ]
});

These are example queries:

// Caches the query results based on a combination of the query hash and the parameter value hash.
await connection.any(sql`
  -- @cache-ttl 60
  SELECT
    id,
    code_alpha_2
  FROM country
  WHERE
    code_alpha_2 = ${countryCode}
`);

// Does not cache the result when query produces no results.
await connection.any(sql`
  -- @cache-ttl 60
  -- @cache-discard-empty true
  SELECT
    id,
    code_alpha_2
  FROM country
  WHERE
    code_alpha_2 = ${countryCode}
`);

// Caches the query results based only on the parameter value hash.
await connection.any(sql`
  -- @cache-ttl 60
  -- @cache-key $bodyHash
  SELECT
    id,
    code_alpha_2
  FROM country
  WHERE
    code_alpha_2 = ${countryCode}
`);

// Caches the query results using 'foo' key.
await connection.any(sql`
  -- @cache-ttl 60
  -- @cache-key foo
  SELECT
    id,
    code_alpha_2
  FROM country
  WHERE
    code_alpha_2 = ${countryCode}
`);
3.3.1

1 year ago

3.3.0

1 year ago

3.2.0

1 year ago

3.1.0

1 year ago

3.3.2

1 year ago

3.0.0

1 year ago

2.4.1

1 year ago

2.3.0

2 years ago

2.2.0

2 years ago

2.4.0

2 years ago

2.1.0

2 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago