Giter Site home page Giter Site logo

koa-file-cache's Introduction

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

$ make test

License

MIT

koa-file-cache's People

Contributors

jonbo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.