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

node-persistent-cache

v1.0.4

Published

Cache using node-cache with easy engine installation

Downloads

15

Readme

node-persistent-cache

This package backs node-cache with a storage engine to persist data across app restarts. It exposes all of node-cache's methods directly. It is backed by node-persist as the default persistence engine, but is easily extensible with any persistent storage engine you might prefer.

Basic Usage

Call node-persistent-cache, setting default options. await new NPC.cache(opts). Enjoy!

const NPC = require('node-persistent-cache');

const cache = await NPC.cache({
	persist: true
});

cache.set('key', 'original value');
cache.get('key'); // 'original value'

// Later . . .
cache.set('key', 'new value');

/* - - - app crashes or is restarted - - - */

const NPC = require('node-persistent-cache');
.
.
.
cache.get('key'); // 'new value'

Configuration

Configuring a New Cache

The configuration is the same as node-cache, with a couple of extras:

  • async: (default: false) Control whether the cache is synchronous, exactly like node-cache, or asynchronous. Note:
    • false: Cache is synchronous; the persistence engine attaches listeners to node-cache and updates data whenever set, del, expired, or flush fire.
      • PRO Works just like node-cache; data is restored when the app restarts.
      • CON Data is not persisted asynchronously, so persisted data may not match cached data.
    • true: Cache is asynchronous; methods that update cached data do not return until the persistence engine has stored them.
      • PRO Persisted data guaranteed to be in sync with cached data.
      • CON Behavior does not match node-cache, may require refactoring.
  • persist: (default: true) Options passed to persistence engine. If falsy, cache does not persist. If true, use the defaults below.
    • persist.engine: (default: "node-persist") The name of the engine to use to persist data. Engine must have been registered first; see below.
    • persist.prefix: (default: "") Prefix to apply to every key. For example, node-persist prepends prefix to the directory so multiple caches may be used without danger of collision. Not all persistence engines use this feature.
    • persist.opts: (default: {}) Options to pass to the selected persistence engine.

Note that using both persist and useClones: false will cause a warning, as data loaded from a backup cache into node-cache will, by definition, be cloned data.

Configuring Cache Defaults

Just call NPC() to set defaults. The options are exactly like those passed to a new cache.

Storage Engines

By default, node-persistent-cache uses node-persist as the backing persistence engine, but any engine can be registered with the engines object. See engines/base-class for the base class, and see engines/node-persist for the node-persist implementation. Anything that implements the methods in base-class and is newable will work with the storage layer.

Example

const NPC = require('node-persistent-cache');
const engine = function(engineOpts) {
	const self = this;
	.
	.
	.
	return self;
};
NPC.engines.register('my-custom-engine', engine);
const cache = new NPC.cache({
	persist: {
		engine: 'my-custom-engine'
	}
});