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-file-caching

v1.14.0

Published

File caching system

Downloads

18

Readme

Node File Caching

Cache response to local cache file

npm

This is a basic module for file caching with the ability to specify the time-to-live (TTL). It doesn't have any dependency as the File System is already provided by Node.

Table of Contents

Why this module?

There are several good modules for file caching and some of them I've used for different projects, however, after years of working in different programming languages and mainly OOP/MVC also being used to set TTL cache I thought to put something simple in place for Node that will offer the similar opportunities. I'm using this module in production for caching pages and endpoints.

Notes

  • This module only uses the Node File System https://nodejs.org/api/fs.html
  • Currently doesn't support custom configurations, however there's a plan to facilitate this.
  • The deprecated methods get, set, remove, removeAll will re removed from the files in the near feature.

Install

  npm i -S node-file-caching 

This package has 4 main methods that can be used and assumes that the defualt cache folder is .cache with a default TTL of 60 minutes.

cacheSet()

This method has 2 mandatory and 2 optional parameters and you'll need to use it for setting the cache. The output of this method will be true.

key = Your own cache identifier

value = Value that you would like to store

ttl = This is optional. Is set to 60 minutes by default.

location = This is optional. Is set to default location of .cache, however, please don't use it as currently there's no state of configuration for persisting the cache location.

const {cacheSet} = require('node-file-caching');

cacheSet("myCacheKey",{foo:"bar"});

cacheGet()

This method has 1 mandatory parameter, called key, which is used to get the cache file. This method has 2 response states:

  1. false = when the key doesn't exist of cache key doesn't have any content

  2. string = it will return the file contents of the key

key = Cache key identifier

const {cacheGet} = require('node-file-caching');

const cacheData = cacheGet("myCacheKey");

cacheRemove()

This method has 1 mandatory parameter, called key, which is used to identify the cache file and remove it. This method has 2 response states:

  1. false = when the key doesn't exist

  2. true = when the key has been removed

key = Cache key identifier

const {cacheRemove} = require('node-file-caching');

cacheRemove("myCacheKey");

cacheRemoveAll()

This method will clear the .cache folder of any files and it will return true as response.

Examples

  • Caching DB response
const {cacheGet,cacheSet} = require('node-file-caching')

const cacheKey = "testCacheKey";
let outputData = cacheGet(cacheKey);

if(!outputData){
  const getDbData = "...";
  outputData = getDbData;
  cacheSet(cacheKey,getDbData);
}
return outputData;
  • Embed it as middleware (app.js)
const {cacheGet,cacheSet} = require("node-file-caching");

// define the middleware      
app.use((req, res, next) => {
          const cacheReqKey = req.originalUrl || req.url;
          const cacheKey = cacheReqKey.replace(/(\/|\-)/g, '_');
          let getCacheData = cacheGet(cacheKey);
          if(!getCacheData){
              res.sendResponse = res.send
              res.send = (body) => {
                cacheSet(cacheKey,body);
                res.sendResponse(body)
              }
              next();
          }
          res.send( getCacheData );
      })
  • Embed as middleware per route
const {cacheGet,cacheSet} = require("node-file-caching");

// define the middleware      
const cacheResponse = (req, res, next) => {
  const cacheReqKey = req.originalUrl || req.url;
  const cacheKey = cacheReqKey.replace(/(\/|\-)/g, '_');
  let getCacheData = cacheGet(cacheKey);
  if(!getCacheData){
    res.sendResponse = res.send
    res.send = (body) => {
      cacheSet(cacheKey,body);
      res.sendResponse(body)
    }
    next();
  }
  res.send( getCacheData );
}
// Your custom routing
app.get('/my-page',cacheResponse, controller.index);

Issues

For any issues, concerns, features or general talk on github any time.

License

MIT