Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some improvement ideas that would be useful (expire cache immediately, and user session cache) #4

Open
skliffmueller opened this issue May 18, 2018 · 0 comments

Comments

@skliffmueller
Copy link

skliffmueller commented May 18, 2018

I have been using this module with great success in an express app. The middleware functionality does just about everything I need it to, except for two things which I will list below.

First, the ability to clear the cache based off a key would be awesome. I was able to make a quick hack to accomplish this before creating the middleware by the following (Basicly this just sets the TTL to 0):

const Cache = require('stale-multi-cache');

const opts = {
  name: 'key',
  staleTTL: 300,
  maxItems: 300,
  bypassHeader: 'bypass',
};

// Hack to force cache expire
function expireKey(key) {
  return this.get(key).then(raw => {
    let json = JSON.parse(raw);
    return this.createValueAndMultiSet(key, json.value, {
      staleTTL:0,
      expireTTL:0
    })
  })
}
Cache.prototype.expireKey = expireKey;

module.exports = new Cache([new Cache.LRUMemoryStore({max: opts.maxItems})], opts);

Next, there are situations where the cache results would be different depending on what user session is signed in. The ability to have the key generate based off a user session, or any additional key options on the middleware would be awesome.

My idea on this would be to allow a key object handler as an option on
https://github.com/kelsin/stale-multi-cache/blob/master/src/cache.js#L193
And every time getKey is called at
https://github.com/kelsin/stale-multi-cache/blob/master/src/cache.js#L209
It will check to see if handler exists, execute handler, or stick with the default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant