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

file-cache-simple

v0.0.7

Published

Simple file caching

Downloads

23

Readme

Simple file cache

A simple file cache that is easy to use and doesn't need a lot of set up. I made this for my portfolio site project thingy and extracted it.

Install

npm i file-cache-simple

Use

Every key is stored in its own file in cacheDir. You can set a prefix for the file name, see the options below. This also allows you to easily store data in subdirectories.

const FileCacheSimple = require('file-cache-simple');
let cache = new FileCacheSimple(); // see below for options

// Store some data
// In this case, 'some data' is stored in cacheDir / prefix.key.json
// You can easily store JSON without stringifying it
cache.set('key.name', 'some data');

// You can also overwrite the default cache expiry time for a setting:
cache.set('key.name', { 'my': 'data' }, 43200 * 1000); // 12h

// And get some out
cache.get('key.name')
	.then(function(value) {
		if(!value || value === null) {
			return console.log('No value was found for this key');
		}
		return console.log('The value is', value);
	})

When get()ting a key, if it doesn't exist it returns null.

You can also change the default options by giving them to the constructor. The defaults are:

const DEFAULT_OPTIONS = {
	'cacheDir': process.cwd() + '/cache',
	'cacheExpire': 3600 * 1000, // 1hr
	'prefix': 'cs', // cache files are prefixed with this
	'fixCacheExpire': false,
	'rejectOnNull': false
};

Is fixCacheExpire in the options is set to true, the active cache expiry time (either given through the settings or as the last argument to set) is stored in the JSON file as well and will be used to check if the cache is still fresh when get()ting it. This allows you to hve different expiry times for different keys.

If rejectOnNull is set to true, the promise will be rejected instead of resolving with null.

Changelog

  • v0.0.7 - 28 June 2016
    • Do not modify default options when creating object, contributed by ifrade
  • v0.0.6 - 27 June 2016
    • Reject on expired cache when rejectOnNull is set, contributed by TomTom101
  • v0.0.5 - 13 Januari 2016
    • Return promises on remove and write, contributed by gregorskii
  • v0.0.4 - 31 December 2015
    • Add rejectOnNull to default options
  • v0.0.3 - 14 December 2015
    • Add fixCacheExpire to cache.set() and default options
  • v0.0.1 - v0.0.2 - 11 December 2015
    • (0.0.2) Add dependencies
    • (0.0.1) Initial commit

License

Copyright 2015 Michiel van der Velde.

This software is licensed under the MIT License.