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

persistent-cache

v1.1.2

Published

A simple module to persistently store/cache arbitrary data.

Downloads

17,835

Readme

persistent-cache

A simple Node module to persistently store/cache arbitrary data.

How to install

Just run

npm install persistent-cache

add the --save option to add persistent-cache to the dependencies in your package.json

Usage

Create cache

var cache = require('persistent-cache');

var cats = cache();

cats is now a cache with the default options, meaning it is a persistent cache utilizing memory caching for performance, it has the name cache, caches data forever and is located in the cache-directory of the current main module. For all available options, see the bottom of this page.

An empty cache of cats is kind of sad. Let's bring some life to it.

Insert data

//Asynchronous
cats.put('Cindy', {color: 'red'}, someCallback);

//Synchronous
cats.putSync('babies', ['Ron', 'Emily']);

Now our cats-cache has an entry Cindy containing an object {color: 'red'} and an entry babies containing an array of some names (strings).

cache.put(key, data, cb) will store any arbitrary data in the cache under the provided key and call the provided callback when done (passing err as the first argument, following node convention). cache.putSync(key, data) is the synchronous counterpart (throwing possible errors).

If there is already an entry for the provided key, cache.put will overwrite it.

Argh, my dog quit the program. I miss my cats :-( Lets retrieve them from the cache.

Retrieve data

cats.get('babies', function(err, babies) {
    //check err for errors

    console.log(babies); //['Ron', 'Emily']
});

console.log(cats.getSync('Cindy')); //{ color: 'red' }

There they are :D

cache.get(key, cb) will get the data saved under key from the cache and call the provided callback when done, passing the retrieved data as the second argument (again, passing error first following node convention). cache.getSync is the synchronous counterpart, returning the data. If there is no (valid) cache entry for the provided key, undefined will be returned/passed.

Getting available keys

I forgot which cats I put in my cache. Fortunately I can look it up:

cats.keys(function(err, keys) {
    //Handle errors

    console.log(keys); //['Cindy', 'babies']
});

console.log(cats.keysSync()); //['Cindy', 'babies']

keys (and its synchronous counterpart keysSync) finds all available keys in a cache.

I found a new owner for my cute cat babies. So I need to remove them from my cache.

Delete data

cats.delete('babies', function(err) {
    //Handle errors

    console.log('babies removed from cache');
});

I safely removed my cat babies from the cache. Yey!

cache.delete(key, cb) will remove the provided key from the cache and call the provided callback when done. cache.deleteSync is the synchronous counterpart.

Tidying up

If you want to delete the folder and files of a persistent cache, simply call unlink on it:

cache.unlink(function(err) {
    //The cache folder and files are gone now
})

A persistent cache will obviously not work anymore after unlinking it.

Options

var someCache = cache({
    base: 'some/folder',
    name: 'foo',
    duration: 1000 * 3600 * 24 //one day
});

When creating a new cache, you may pass an options object, with the following available properties:

options.base

The base directory where persistent-cache will save its caches.

Defaults to the main modules directory

options.name

The name of the cache. Determines the name of the created folder where the data is stored, which is just base + name.

Defaults to cache

options.duration

The amount of milliseconds a cache entry should be valid for. If not set, cache entries are not invalidated (stay until deleted).

Defaults to undefined (infinite)

options.memory

Whether the cache should use memory caching or not (mirrors all cache data in the ram, saving disk I/O and increasing performance).

Defaults to true

options.persist

Whether the cache should be persistent, aka if it should write its data to the disk for later use or not. Set this to false to create a memory-only cache.

Defaults to true