An ultra-simplified API/JSON response caching middleware for Express/Node using plain-english durations.
Because caching of simple data/responses should ALSO be simple.
This day and age, with less and less heavy lifting done on the server, the most common thing we find ourselves doing is letting the server power the API. Whether the data is stored in Mongo, SQL, CouchDB, or whatever - you get a request, you fetch the data, and you return the data. Sometimes these fetches are costly and you want to cache the response so the next hit doesn't hammer your server. This is why caching exists.
The problem is, with so many cache options, people are still left to fend for themselves when it comes to implementation. It often boils down to a manual process similar to the following:
You're still left wrapping the content of each request with this cache-checking mechanism.
Now it can be as simple as telling the request that you want to use a cache, and for how long results should be cached (in plain English, not milliseconds, because who really wants to calculate that each time?).
npm install apicache
To use, simply inject the middleware (example:
apicache('5 minutes')) into your routes. Everything else is automagic.
var cache = middleware;app;
apicache.clear([target])- clears cache target (key or group), or entire cache if no value passed, returns new index.
apicache.getIndex()- returns current cache index [of keys]
apicache.middleware([duration])- the actual middleware that will be used in your routes.
durationis in the following format "[length] [unit]", as in
apicache.options([options])- getter/setter for options. If used as a setter, this function is chainable, allowing you to do things such as... say... return the middleware.
debug: false|true // if true, enables console outputdefaultDuration: 3600000 // should be a number (in ms), defaults to 1 hourenabled: true|false // if false, turns off caching globally (useful on dev)redisClient: client // if provided, uses the [node-redis]() client instead of [memory-cache]()appendKey: // if you want the key (which is the URL) to be appended by something in the req object, put req properties here that point to what you want appended. I.E. req.session.id would be ['session', 'id']
Oftentimes it benefits us to group cache entries, for example, by collection (in an API). This
would enable us to clear all cached "post" requests if we updated something in the "post" collection
for instance. Adding a simple
req.apicacheGroup = [somevalue]; to your route enables this. See example below:
var apicache = ;var cache = apicachemiddleware;// GET collection/idapp;// POST collection/idapp;
Additionally, you could add manual cache control to the previous project with routes such as these:
// GET apicache index (for the curious)app;// GET apicache index (for the curious)app;
As of v0.2.0, apicache now takes advantage of the brilliant debug module for console logging. To enable, simply add 'apicache' to the DEBUG environment variable
Alternatively, the older method of passing enabling via the .options() function still works.
var cache = options debug: true middleware;
GET routes between admin and public sites, you'll likely want the
routes to be cached from your public client, but NOT cached when from the admin client. This
is achieved by sending a
"x-apicache-bypass": true header along with the requst from the admin.
The presence of this header flag will bypass the cache, ensuring you aren't looking at stale data.
apicacheis currently an in-memory cache, built upon memory-cache. It may later be expanded to allow other cache-layers.
application/json. There's a reason it's called
Special thanks to all those that use this library and report issues, but especially to the following active users that have helped add to the core functionality!