cache.js 606 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. 'use strict';
  2. module.exports = class Cache {
  3. constructor() {
  4. this.cache = new Map();
  5. }
  6. set(id, value) {
  7. this.cache.set(id, value);
  8. }
  9. has(id) {
  10. return this.cache.has(id);
  11. }
  12. get(id) {
  13. return this.cache.get(id);
  14. }
  15. del(id) {
  16. this.cache.delete(id);
  17. }
  18. apply(id, value) {
  19. if (this.has(id)) return this.get(id);
  20. if (typeof value === 'function') value = value();
  21. this.set(id, value);
  22. return value;
  23. }
  24. flush() {
  25. this.cache.clear();
  26. }
  27. size() {
  28. return this.cache.size;
  29. }
  30. dump() {
  31. return Object.fromEntries(this.cache);
  32. }
  33. };