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

Make NodeCache compliant con the Map interface on future releases #292

Open
serpi90 opened this issue Aug 12, 2022 · 0 comments
Open

Make NodeCache compliant con the Map interface on future releases #292

serpi90 opened this issue Aug 12, 2022 · 0 comments

Comments

@serpi90
Copy link

serpi90 commented Aug 12, 2022

https://262.ecma-international.org/7.0/#sec-properties-of-the-map-prototype-object

This would allow node-cache to be a drop-in replacement to things that expect a map, for example lodash's memoize which explains:

Note: The cache is exposed as the cache property on the memoized function. Its creation may be customized by replacing the _.memoize.Cache constructor with one whose instances implement the Map method interface of clear, delete, get, has, and set.

I currently have a workaround by doing:

const _ = require('lodash');
const NodeCache = require('node-cache');

// Adapter for NodeCache to have the Map interface expected by lodash.memoize
class MemoizeCache {
  constructor(args) { this.cache = new NodeCache(args); }
  clear() { this.cache.flushAll(); }
  delete(key) { return this.cache.del(key); }
  get(key) { return this.cache.get(key); }
  has(key) { return this.cache.has(key); }
  set(key, value) { this.cache.set(key, value); return this; }
}

module.exports = (fn, ttl) => {
  const memoized = _.memoize(fn);
  memoized.cache = new MemoizeCache({ stdTTL: ttl });
  return memoized;
};

But it would be nice to conform to that interface and not requiring that change.

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