InMemoryLRUCache.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.InMemoryLRUCache = void 0;
  7. const lru_cache_1 = __importDefault(require("lru-cache"));
  8. class InMemoryLRUCache {
  9. constructor(lruCacheOpts) {
  10. this.cache = new lru_cache_1.default({
  11. sizeCalculation: InMemoryLRUCache.sizeCalculation,
  12. maxSize: Math.pow(2, 20) * 30,
  13. ...lruCacheOpts,
  14. });
  15. }
  16. static sizeCalculation(item) {
  17. if (typeof item === "string") {
  18. return item.length;
  19. }
  20. if (typeof item === "object") {
  21. return Buffer.byteLength(JSON.stringify(item), "utf8");
  22. }
  23. return 1;
  24. }
  25. async set(key, value, options) {
  26. if (options === null || options === void 0 ? void 0 : options.ttl) {
  27. this.cache.set(key, value, { ttl: options.ttl * 1000 });
  28. }
  29. else {
  30. this.cache.set(key, value);
  31. }
  32. }
  33. async get(key) {
  34. return this.cache.get(key);
  35. }
  36. async delete(key) {
  37. return this.cache.delete(key);
  38. }
  39. clear() {
  40. this.cache.clear();
  41. }
  42. keys() {
  43. return [...this.cache.keys()];
  44. }
  45. }
  46. exports.InMemoryLRUCache = InMemoryLRUCache;
  47. //# sourceMappingURL=InMemoryLRUCache.js.map