A (not yet) fully featured cache-tagging solution for node servers.
Most HTTP servers rely on third-party systems for caching (such as CDNs). Even though this is probably the right approach, sometimes we need a fully featured, local, and simple caching and puring solution for a varying number of reasons. Whatever the reason is for you, we got you covered!
npm i express-cache-tags
import express from 'express'
import cache from 'express-cache-tags'
express()
.use(cache())
// .get(...) your server routes
.listen(3000)
Any request will be cached using the request URL as basic cache-tag for invalidation. If a Cache-Tags
header is present on any response, it will be considered as well.
MISSes and HITs will be logged by default.
You can purge specific tags by sending GET requests to /_cache/purge?invalidate=[TAG NAME]
. You can also purge by URL, by sending the URL as invalidate
query param.
All the usage options receive the same options object:
prop | description | default |
---|---|---|
cacheFactory | A cache-object factory | A new memory-cache instace |
generateKey | A cache-key generator |
(req) => \ ${req.method}:${rq.url}`` |
shouldCache | Predicate to decide caching | () => true // cache anything the middleware touches |
statusHeader | Hitting status header name | 'CDN-Cache' |
logger | Logging specific options | |
logger.enabled | Wheter or not to log operations | true |
logger.scope | Logging scope (see signale ) |
'CACHE' |
cacheTags | Cache-tags specific options | |
cacheTags.extract | Request cache-tags extractor | `(req, res) => (res.get('Cache-Tags') |
purger | Purging specific options | |
purger.extract | Purging tags extractor | `(req, res) => (req.query.invalidate |
A memory-cache
compatible object, with at least get
, put
, keys
, and clear
methods.