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

cacher

v3.0.0

Published

A memcached backed http cache in the form of express middleware

Downloads

587

Readme

Cacher

It does the hard work so you don't have to!

Travis

What is it?

HTTP Caching implemented as express middleware, with pluggable backends for support for a variety of caching servers (memcached, redis, etc)

Features

  • Set expiry times per-app or per-route. It sets proper headers for client caching
  • Avoids the thundering heard by smartly refreshing the cache
  • Emits hit or miss events so you can track your hitrate
  • Simple invalidations
  • Overrides for custom cache keys and dev mode support
  • Obeys (some) client Cache-Control headers

What does it look like?

var Cacher = require("cacher")
// use the default in memory cache
var cacher = new Cacher()
// or pass in a different cache client (in this cached memcached) for different backend support
CacherMemcached = require('cacher-memcached')
var cacher = new Cacher(new CacherMemcached('host1:12345'))

// as a global middleware
app.use(cacher.cache('seconds', 30))
// or per route
app.get("/long-cache", cacher.cache('days'), ...)
app.get("/short-cache", cacher.cache('minute'), ...)
// will set no-cache headers for routes that we explicitly want to ignore caching on
app.get("/no-cache", cacher.cache(false), ...)

// Backwards compatible with old cache definitions
app.use(cacher.cacheHourly())
app.get("/long-cache", cacher.cacheDays(2), ...)

// invalidation support
cacher.invalidate('/home')

// listen for events to track cache rate and errors
cacher.on("hit", function(key) {
  console.log("woohoo!")
})
cacher.on("miss", function(key) {
  console.log("doh!")
})
cacher.on("error", function(err) {
  console.log(err)
})

// Dev mode, quickly turn off caching when it gets in the way
var env = process.env.NODE_ENV || 'development'
if (env === 'development') {
  cacher.noCaching = true
}

// override cache key generation for finer grain control
cacher.genCacheKey = function(req) {
  return req.path + req.header('user-agent')
}

// override cache TTL based on response
cacher.genCacheTtl = function(res, origTtl) {
  if (res.statusCode >= 400) {
    return 0
  }
  return origTtl
}

Backends

Currently, Cacher comes bundled with an in-memory cache

Backends are distributed as separate modules:

  • cacher-memcached (https://github.com/addisonj/cacher-memcached)
  • cacher-redis (https://github.com/addisonj/cacher-redis)

Backend Client Api

If you want to implement your own backend, the API is really simple:

// pass whatever options are needed for connection/options
// provide defaults so a client can be fully instantiated with no parameters
function MyBackend(...) {
}

// cb is required, cb(err, cacheObject)
MyBackend.prototype.get = function(key, cb) {}

MyBackend.prototype.set = function(key, cacheObject, ttlInSeconds, [cb]) {}

MyBackend.prototype.invalidate = function(key, [cb]) {}

Testing your backend

Run unit tests using your backend by doing the following:

cd Cacher
npm link ../myBackend
CACHER_CLIENT=myBackend npm test