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 🙏

© 2026 – Pkg Stats / Ryan Hefner

bcrypt-cache

v2.0.1

Published

Cache bcrypt successful comparisons to reduce CPU load on frequent bcrypt checks

Readme

bcrypt-cache v2.0

bcrypt-cache is a module designed to reduce CPU usage when frequently doing bcrypt comparisons.

For example, you may store API secret keys in a database using bcrypt. Checking the API secret on every API call is CPU instensive.

By using bcrypt-cache, the first time the token is received, it'll be compared to the bcrypt hash. Once verified, a much simpler SHA1 hash of the token will be stored in the cache and used for future comparisons.

Storing the SHA1 makes comparisons less CPU instensive while not leaking secrets by storing plain text in the cache.

Installation

npm install --save bcrypt-cache

Usage

The bcrypt-cache module comes with two default caching mechanisms: Memory and Redis.

Using Redis as the cache

Use Redis if you have multiple instances of the application running to share the cache.

const Redis = require('ioredis')
const { BcryptCache, RedisCache } = require('.');

const cache = new RedisCache({
  redis: new Redis(),
  ttl: 300, // cache for 5 minutes
  refreshTTL: true // reset the expiration TTL on each GET
});

const bcryptCache = new BcryptCache(cache);

async function verifyApiToken (req, res, next) {
  // Get the plain text token passed in by the user
  const token = req.headers['X-API-TOKEN'];
  // Get the hashed token
  const hashedToken = req.user.apiSecretToken;
  // Compare the plain text to the bcrypt hash
  const valid = await bcryptCache.compare(token, hashedToken);

  if (!valid) {
    next(new Error('Invalid access token'));
  } else {
    next();
  }
}

Using memory as a cache

Each instance of the app will have it's own cache.

const { BcryptCache, MemoryCache } = require('bcrypt-cache');

const cache = new MemoryCache({
  pruneTimer: 60, // how frequenctly to prune expired cache entries
  ttl: 300, // cache for 5 minutes
  refreshTTL: true // reset the expiration TTL on each GET
});

const bcryptCache = new BcryptCache(cache);

RedisBcryptCache options

| Option | Description | | ------ | ----------- | | redis (required) | A Redis client instance (must expose async functions, such as ioredis) | | ttl | The number of seconds a token will be stored in the cache. Default: 600 | | prefix | A string to use to prefix cache keys in Redis. Default: bcrypt-cache: | | refreshTTL | Whether to refresh the expiration time when compare finds a token in the cache. Default: true

MemoryCache options

| Option | Description | | ------ | ----------- | | ttl | The number of seconds a token will be stored in the cache. Default: 600 | | pruneTimer | The number of seconds the memory cache will be scanned for expired tokens. Set to a falsy value to disable. Default: 60 | | refreshTTL | Whether to refresh the expiration time when compare finds a token in the cache. Default: true

Create your own custom cache

You can create your own cache mechanism.

interface Cache {
  get: (key: string) => Promise<string | null>;
  set: (key: string, value: string) => Promise<boolean>;
}

See memory-cache.js or redis-cache.js for examples.

Debug

Use the debug module to get error messages

DEBUG='bcrypt-cache:*'

Change Log

v2.0.0:

  • Refactor to split the BcryptCache module to only expose compare function.
  • Pass a cache instance into the BcryptCache constructor
  • bcrypt and bcryptjs are now peerDependency
  • Requires node v8 or higher

v1.1.0:

  • Update depdendencies due to vulnerabilities reported by GitHub/npm

v1.0.0:

  • Initial release