Skip to content

Support 'Cache-Control: max-age, s-maxage' and Expires headers #26

@eschwartz

Description

@eschwartz

I'm thinking it would be nice if Cacher supported Cache-Control: max-age, Cache-Control: s-maxage, and Expires headers out of the box.

I've added support for this in my own application by overriding the genCacheTtl function, but I could submit this feature as a pull request, if you'd like. Here's what my implementation looks like:

var parseCacheControl = require('parse-cache-control');
var cacher = new Cacher();

cacher.genCacheTtl = function(res, origTtl) {
    if (res.get('Cache-Control')) {
        var cacheControl = parseCacheControl(res.get('Cache-Control'));
        if (cacheControl['s-maxage']) {
            return parseInt(cacheControl['s-maxage']);
        }
        if (cacheControl['max-age']) {
            return parseInt(cacheControl['max-age']);
        }
    }

    if (res.get('Expires')) {
        return (Date.parse(res.get('Expires')) - Date.now()) / 1000;
    }

    return origTtl;
};

I've read just a little bit about cache control headers, and from what I understand the order of precedence goes:

  1. Cache-Control: s-maxage
  2. Cache-Control: max-age
  3. Cache-Control: Expires

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions