express-expeditious
express-expeditious copied to clipboard
Flush with wildcard
Does this module have any way to flush the keys using a wildcard?
I'm caching a number of different named sections of a site using namespaces. It's easy to flush a particular section when it is updated in the db, as I know the section namespace. But I would like to flush all sections when a global piece of data has been modified. All the section namespaces start with 'section' so ideally I could flush('section*', callback) but it seems like wildcards do not work.
@etx currently the flush behaviour will depend on the engine, and is something that might need a little more work here. For example the memory engine flush just deletes everything without namespacing as seen here. It would be nice to allow some sort of namespacing with it, but I think would be hard to agree on a format for keys etc. What do you think?
The Redis engine is different it will get all keys for a given namespace string and delete them as seen here - there was a reason I did it this specific way but I can't recall it now. It looks like this would achieve what you're asking, right?
As a general overview, the flush function exposed by express-expedtious
supports a namespace
parameter as seen here. This is passed to flush in the underlying expeditious
instance here which is then passed to your storage engine (memory or redis). It looks like the passing of that namespace string is getting lost here, so that'll need fixing for engines like the Redis one to work properly.
So, with the above fix and a Redis engine would your issue be resolved?
Also, I think we'd need to update the express-expeditious
function here to concat the namespaces.
For example, if we take this code:
const getExpeditiousCache = require('express-expeditious');
const express = require('express');
const cache = getExpeditiousCache({
namespace: 'stories',
defaultTtl: '1 minute'
});
const app = express();
app.use(cache);
app.get('/stories/:category', (req, res, next) => {
stories.find({ category: req.params.category }).then(list => res.json(list))
})
app.post('/stories', bodyParser.json(), (req, res, next) => {
stories.replaceAll(req.body).then(result => res.json(result))
cache.flush() // flush all stories
})
app.post('/stories/:category', bodyParser.json(), (req, res, next) => {
stories.updateCategory(req.params.category, req.body).then(result => res.json(result))
cache.flush(req.params.category) // flush all stories in specific category
})
We'd like to flush "stories:*" for POST stories/
, but we'd like to flush "stories:some-category" for POST /stories/:category'
. This is my thinking.
Yep, exactly!
My application is doing basically this, but it listens to updates on the sections from a DB and when something changes it rebuilds the Express Router and calculates the namespace by concatenating 'section' and the name of the section from the DB. But changes to global data would require the cache be purged.
Beside completely flushing the cache I can't come up with any other way, and the cache holds pages outside of the sections system and images (now that you so quickly patched that up! :)
Just wanted to see if maybe the module had some functionality I wasn't seeing in the docs.