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

koa-file-cache

v0.3.1

Published

Koa middleware to cache expensive requests (e.g. remote, nested DB, etc) to disk

Downloads

3

Readme

Koa-File-Cache (middleware)

A simple way to cache expensive requests (e.g. remote, nested DB, etc) to disk in KoaJS.

Cached files will bypass any downstream middleware and be streamed directly to the client. By default, all cache files are compressed with gzip. The library also handles some useful HTTP caching headers, to prevent sending data the client already has cached (304 Not Modified).

Example

// Dictionary API that caches all words that match a given letter, each day
// (GET /?letter=a)
var db = require(...);
var Cache = require('koa-file-cache');

// Determine the letter and set the cache file name
function* getLetter(next) {
    this.letter = (this.query.letter || "a").charAt(0);
    this.cacheName = this.letter + '.words';
    yield next;
}

var aDay = 1000*60*60*24;

// *Typically you would probably use this at the router level instead.*
var app = koa();
app.use(getLetter);
app.use(Cache({folder: 'dictionary', cacheTime: aDay}));
app.use(function*(next){
    // This will happen, at most, only once per day (per letter)
    // *Heavy DB query*
    var words = yield db.query('... WHERE letter = ?',[this.letter]);

    this.body = words;
});
app.listen();

What if you want manipulate an existing cache? Use {delegate:true}

// ...
app.use(getLetter);
app.use(Cache({folder: 'dictionary', cacheTime: aDay, delegate: true}));
app.use(function*(next){
    var cache = this.body;
    if (cache) {
        // Add the current server timestamp
        cache.timestamp = Date.now();
    }
    else {
        // This will happen, at most, only once per day
        // Heavy DB query
        var words = yield db.query('... WHERE letter = ?',[this.letter]);

        this.body = words;
    }
});
app.listen();

What if you want to disable caching for a particular request?

this.caching = false;

Setting this upstream will prevent both reading from and writing to the cache. Downstream will only prevent writing to the cache.

API

Cache([options])

var Cache = require('koa-file-cache');
app.use(Cache(options))

options

  • cacheTime {Number} Time in milliseconds the cache is valid and used. Default 1000*60 (1 min)

  • folder {String} Folder that will be used to store the cache. Default . (current dir)

    Note: Folder must already exist

  • gzip {Boolean} Store and send cache gzipped. Default true

    Note: This only applies to the cache and will not gzip the first outgoing response. Use koa-compress if you want this.

  • gzipThreshold {Number} Size in bytes the amount where should begin to store gzipped. Default 1024

  • delegate {Boolean} If true, continues downstream to let middleware execute with the cache available in this.body. Default false

    Note: This only applies when a cache exists and is not expired. Otherwise, downstream middleware always get executed.

  • type {String} Response type if cache is being sent directly. Default json

    Note: This only applies when delegate is false.

  • fileNameHash {Array[String}} Fields that will be used to generate the file name. Default ['cacheName'] (Set using this.cacheName where context this is (in) the middleware)

  • fileNameHashSep {String} String used to seperate the fileNameHash fields. Default .

    Note: This only applies when fileNameHash length > 1.

Installation

$ npm install koa-file-cache

Running tests

$ npm test

License

MIT