npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

redis-swagger-json-cache

v0.1.2

Published

Redis cache for swagger JSON api routes

Downloads

4

Readme

Build Status Coverage Status

redis-swagger-json-cache

Simple drop in redis cache for swagger JSON apis using express.

Setup

  1. npm install redis-swagger-json-cache

Include node_modules in fittingsDirs array of your swagger configuration. fittingsDirs: [ api/fittings, 'node_modules' ]

Include x-redis-swagger-json-cache top level configuration in your swagger spec:

x-redis-swagger-json-cache:
  name: 'my-api'

Include x-redis-swagger-json-cache path specific configuration in your swagger spec to enable cache for path:

paths:
  "/my-json-path":
    x-redis-swagger-json-cache:
      enabled: true
    get:
      ...

Good to go.

Requirements

  • Swagger
  • Express
  • Redis

Configure redis

By default it will try to use a localhost redis. To configure add a redis configuration in your swagger configuration file

redis:
  host: 'redis'

or:

redis:
  connectionString: 'redis://127.0.0.1:6379'

TTL

Time to live is calculated on demand in a cache miss so that you can do something more fancy then a set amount of time.

To use a custom TTL create a helper file in your swagger helpers directory.

// in helpers/your-helper-file.js
exports.ttl = (req, redisCacheGlobalOptions, redisCachePathOptions) => {
  return redisCachePathOptions.ttl;
}

In this example include a ttl option in your path configuration.

paths:
  "/my-json-path":
    x-redis-swagger-json-cache:
      enabled: true
      ttl: 100
    get:
      ...

Tell the cache to use your helper

x-redis-swagger-json-cache:
  name: 'my-api'
  ttl:
    helper: your-helper-file
    function: ttl

You can do whatever fancy TTL logic needed, however it must be synchronous at this point.

Build in TTL functions

untilNextMinute

Cache a new request until the next minute.

const TTL = require('redis-swagger-json-cache/TTL');
exports.ttl = TTL.untilNextMinute;

untilNextDay

Cache a new request until the next day, i.e. invalidate cached entry at midnight.

const TTL = require('redis-swagger-json-cache/TTL');
exports.ttl = TTL.untilNextDay;

You can also specify a timezone that will determine when midnight is. This is done in the top-level configuration of the cache.

x-redis-swagger-json-cache:
  name: 'my-api'
  ttl:
    helper: redis-swagger-json-cache
    function: ttl
  timezone: 'Europe/Stockholm'

Key

The cache key for a response is determined calculated on demand in a cache miss so that you can base the keys on information about the request.

To use a custom cache key you use the same method as in TTL.

x-redis-swagger-json-cache:
  name: 'my-api'
  key:
    helper: your-helper-file
    function: key
// in helpers/your-helper-file.js
exports.key = (req, redisCacheGlobalOptions, redisCachePathOptions) => {
  return `my-special-prefix:${req.originalUrl}`;
}

The default cache key is sufficient for most operations.

Important notes

  • Only supports JSON responses
  • Only GET requests are cached
  • Simple in nature, will not do fancy things with headers and redirects at this point
  • Default TTL is 5 seconds
  • Default cache key is name:full-url (includes query parameters)
  • Errors will not be cached
  • Errors on cache hits or misses will fall through to regular request, but will be logged
  • Include debug, use DEBUG=redis-swagger-json-cache* to get debug information
  • Will fail at startup if redis is not available

TODO

  • Implicit cache invalidation (PUT/POST/DELETE)