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

@comodinx/cache

v0.0.11

Published

@comodinx/cache is a Node.js cache helper.

Downloads

122

Readme

Cache

@comodinx/cache is a Node.js cache helper.

Index

Download & Install

NPM

For use with redis

    npm install @comodinx/cache redis

For use with memory cache

    npm install @comodinx/cache memory-cache

Source code

$ git clone https://gitlab.com/comodinx/cache.git
$ cd cache
$ npm install

How is it used?

Configure

| Environment variable | Value | Default value | |:--------------------------|:--------------|:--------------| | CACHE_ENABLED | true/false | true | | CACHE_TYPE | memory/redis | 'memory' |

Only for Redis

| Environment variable | Default value | Value | |:--------------------------|:--------------|:--------------| | CACHE_HOST | redis host | localhost | | CACHE_PORT | redis port | 6379 |

Only for API cache middleware

| Environment variable | Value | Default value | |:--------------------------|:--------------------------|:------------------| | CACHE_MIDDLEWARE_DURATION | number unit | '1 hour' | | CACHE_HEADER_CONTROL | cache control header name | 'cache-control' |

const cache = require('@comodinx/cache');

// Simple usage
await cache.put('foo', 'bar');
const foo = await cache.get('foo');
console.log(foo);

// Expiration time usage
await cache.put('cash', 'yes', 100);
const cash = await cache.get('cash');
console.log('I have cash? ' + cash);

setTimeout(() => {
  const cash = await cache.get('cash');
  console.log('I have cash? ' + cash);
}, 200);

// Print on console:
//
// bar
// I have cash? yes
// I have cash? undefined

Middleware

Inspirated on apicache

API response caching middleware for Express/Node.

Simple usage

const { apicache } = require('@comodinx/cache');
const express = require('express');
const app = express();
const port = 3000;

app.use(apicache('1 hour'));

app.get('/', (req, res) => {
    res.send({ foo: 'bar' });
});

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`);
});

Or at a specific endpoint.

const { apicache } = require('@comodinx/cache');
const express = require('express');
const app = express();
const port = 3000;

app.get('/', apicache('1 hour'), (req, res) => {
    res.send({ foo: 'bar' });
});

app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port}`);
});

Time format

{number} {unit time}

| Available unit time | |:--------------------| | m | ms | | minute | minutes | | hour | hours | | day | days | | week | weeks | | month | months |

Available options

{
  enabled:             process.env.CACHE_ENABLED,                           // if false or undefined, turns off caching globally (useful on dev)
  defaultDuration:     process.env.CACHE_MIDDLEWARE_DURATION || '1 hour',   // should be either a number (in ms) or a string, defaults to '1 hour'
  headerCacheControl:  process.env.CACHE_HEADER_CONTROL || 'cache-control', // should be either a string, defaults to 'cache-control'
  respectCacheControl: process.env.CACHE_RESPECT_HEADER_CONTROL,            // should be either a boolean (true | 1), defaults to false
  cacheKey:            req.originalUrl || req.url,                          // should be either a strong or a function (in function case, return a string custom key), defaults to req.originalUrl || req.url
  statusCode:          {                                                    // list of status codes that should never be cached
    include: [],
    exclude: []
  },
  headerBlacklist:     []                                                   // list of headers that should never be cached
}

Other ways of use

Use apicache with custom options

app.get('/', apicache(), (req, res) => /* ... */);

// OR

app.get('/', apicache('2 hours', 'my-custom-cache-key'), (req, res) => /* ... */);

// OR

app.get(
    '/',
    apicache({
        duration: '2 hours'
        cacheKey: 'my-custom-cache-key'
    }),
    (req, res) => /* ... */
);

For only cache success responses, use apicache.ok (status code between 200 - 299)

app.get('/', apicache.ok(), (req, res) => /* ... */);

// OR

app.get('/', apicache.ok('2 hours', 'my-custom-cache-key'), (req, res) => /* ... */);

// OR

app.get(
    '/',
    apicache.ok({
        duration: '2 hours'
        cacheKey: 'my-custom-cache-key'
    }),
    (req, res) => /* ... */
);

For only cache success responses, use apicache.ok (status code between 200 - 299)

app.get('/', apicache.ok(), (req, res) => /* ... */);

// OR

app.get('/', apicache.ok('2 hours', 'my-custom-cache-key'), (req, res) => /* ... */);

// OR

app.get(
    '/',
    apicache.ok({
        duration: '2 hours'
        cacheKey: 'my-custom-cache-key'
    }),
    (req, res) => /* ... */
);

Tests

In order to see more concrete examples, I INVITE YOU TO LOOK AT THE TESTS :)

Run the unit tests

npm test