InMemoryLRUCache.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. var __importDefault = (this && this.__importDefault) || function (mod) {
  12. return (mod && mod.__esModule) ? mod : { "default": mod };
  13. };
  14. Object.defineProperty(exports, "__esModule", { value: true });
  15. exports.InMemoryLRUCache = void 0;
  16. const lru_cache_1 = __importDefault(require("lru-cache"));
  17. function defaultLengthCalculation(item) {
  18. if (Array.isArray(item) || typeof item === 'string') {
  19. return item.length;
  20. }
  21. return 1;
  22. }
  23. class InMemoryLRUCache {
  24. constructor({ maxSize = Infinity, sizeCalculator = defaultLengthCalculation, onDispose, } = {}) {
  25. this.store = new lru_cache_1.default({
  26. max: maxSize,
  27. length: sizeCalculator,
  28. dispose: onDispose,
  29. });
  30. }
  31. get(key) {
  32. return __awaiter(this, void 0, void 0, function* () {
  33. return this.store.get(key);
  34. });
  35. }
  36. set(key, value, options) {
  37. return __awaiter(this, void 0, void 0, function* () {
  38. const maxAge = options && options.ttl && options.ttl * 1000;
  39. this.store.set(key, value, maxAge);
  40. });
  41. }
  42. delete(key) {
  43. return __awaiter(this, void 0, void 0, function* () {
  44. this.store.del(key);
  45. });
  46. }
  47. flush() {
  48. return __awaiter(this, void 0, void 0, function* () {
  49. this.store.reset();
  50. });
  51. }
  52. getTotalSize() {
  53. return __awaiter(this, void 0, void 0, function* () {
  54. return this.store.length;
  55. });
  56. }
  57. }
  58. exports.InMemoryLRUCache = InMemoryLRUCache;
  59. //# sourceMappingURL=InMemoryLRUCache.js.map