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-filesystem-cache

v1.0.2

Published

File cached for NodeJS

Downloads

214

Readme

node-filesystem-cache

Module cache for NodeJs using Filesystem. Storing string, integer, boolean, object, class,...

Installation

npm install node-filesystem-cache --save

Configuration

Custom cache directory

const CacheApi = require('node-filesystem-cache');
const cachePath = __dirname + '/cache';
const Cache = new CacheApi(cachePath);

Cache Usage

Retrieving Items From The Cache

The get method on the Cache facade is used to retrieve items from the cache. If the item does not exist in the cache, null will be returned. If you wish, you may pass a second argument to the get method specifying the default value you wish to be returned if the item doesn't exist:

value = Cache.get('key');
value = Cache.get('key', 'default');

Checking For Item Existence

The has method may be used to determine if an item exists in the cache. This method will return false if the value is null:

if (Cache.has('key')) {
    //
}

Retrieve & Store

Sometimes you may wish to retrieve an item from the cache, but also store a default value if the requested item doesn't exist.

// Synchronous
value = Cache.remember('users', seconds, () => {
     return [
        {
            id: 1,
            name: 'User 1',
        },
        {
            id: 2,
            name: 'User 2',
        }
     ];
 });

// Asynchronous
value = await Cache.rememberAsync('users', seconds, async () => {
     return await userRepository.getUsers();
 });

Retrieve & Delete

If you need to retrieve an item from the cache and then delete the item, you may use the pull method. Like the get method, null will be returned if the item does not exist in the cache:

value = Cache.pull('key');

Storing Items In The Cache

You may use the put method on the Cache to store items in the cache:

Cache.put('key', 'value', seconds);

If the storage time is not passed to the put method, the item will be stored indefinitely:

Cache.put('key', 'value');

Store If Not Present

The add method will only add the item to the cache if it does not already exist in the cache store. The method will return true if the item is actually added to the cache. Otherwise, the method will return false:

Cache.add('key', 'value', seconds);

Storing Items Forever

The forever method may be used to store an item in the cache permanently. Since these items will not expire, they must be manually removed from the cache using the forget method:

Cache.forever('key', 'value');

Removing Items From The Cache

You may remove items from the cache using the forget method:

Cache.forget('key');

You may clear the entire cache using the clear method:

Cache.clear();