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

express-middleware-caching

v1.0.1

Published

A caching system for Express that saves a JSON response on disk and checks for expiration.

Downloads

5

Readme

📦 express-middleware-caching

For some of my projects I use Google Spreadsheets as an API for data. Google offers a JSON API which I request with an Express.js app so I can restructure it before using it in my application. Because the Google Spreadsheet API isn't super fast when requesting it and because I don't want to be rate limited I decided to look for caching modules that stored data on the disk. I thought flat-cache came pretty close but it doesn't have an expiration and uses a big file with a key for every request. I'd rather have multiple small files that uses an MD5 hash of the URL as a key. I also needed automatic and manual cache expiration. So that's how this module came to fruitation.

Basically you can add this as a middleware for your Express route and it takes the URL you're requesting, makes an MD5 hash of it and uses that as a filename for a cache file which contains the response. The next time you or anyone else requests the URL the cache middleware checks if the file exists on disk, checks wether it's expired or not and servers (or recreates) the cached data.

Install

Add this module to your Express project:

npm install express-middleware-caching -S

Usage

Say you have a route file that calls the index function of a controller. This function requests data from a source and you want this data cached on your server's disk. You can easily add this module as a middleware to handle the caching and expiration for you. Just add it to your GET route give it an expiration (in minutes) and optionally (but recommended) a path where the cache files should be saved (if none is supplied the cache will be saved in the module's folder under .cache/).

// cars.route.js
const path = require('path');
const express = require('express');
const cache = require('express-middleware-caching');

const router = express.Router();
const carsController = require('../controllers/cars.controller');

// Cache the data for 10 minutes and save the cache in the project's root in the .cache/ folder.
router.get('/cars/:id/', cache(10, path.resolve('./.cache')), carsController.index);

module.exports = router;

Let's say the URL for this request is http://localhost:3000/cars/391039. The caching script will make an MD5 hash of this URL to use as a filename. The next time you request this URL the cache checks if a filename with the same MD5 hash exists and hasn't expired so it can serve it. Otherwise it will (re)create it.Note: if you add parameters to the URL a new cache will be created since the MD5 hash will be different.

Clearing the cache manually

You are able to clear the cache manually. You have to add the clear_cache as a parameter to the URL you're requesting. The cache script first filters out the clear_cache so the MD5 hash won't change, then it deletes the cache file and recreates it with fresh data. Note: The clear_cache parameter always has to be at the end of the query string. Examples: http://localhost:3000/cars/391039?clear_cache http://localhost:3000/cars/391039?color=purple&clear_cache

Author

👤 Jeroen Hammann

🤝 Contributing

Contributions, issues and feature requests are welcome!Feel free to check issues page.

📝 License

Copyright © 2019 Jeroen Hammann. This project is MIT licensed.