lru.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * This file is part of the storage node for the Joystream project.
  3. * Copyright (C) 2019 Joystream Contributors
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. 'use strict';
  19. const DEFAULT_CAPACITY = 100;
  20. const debug = require('debug')('joystream:util:lru');
  21. /*
  22. * Simple least recently used cache.
  23. */
  24. class LRUCache
  25. {
  26. constructor(capacity = DEFAULT_CAPACITY)
  27. {
  28. this.capacity = capacity;
  29. this.clear();
  30. }
  31. /*
  32. * Return the entry with the given key, and update it's usage.
  33. */
  34. get(key)
  35. {
  36. const val = this.store.get(key);
  37. if (val) {
  38. this.access.set(key, Date.now());
  39. }
  40. return val;
  41. }
  42. /*
  43. * Return true if the key is the cache, false otherwise.
  44. */
  45. has(key)
  46. {
  47. return this.store.has(key);
  48. }
  49. /*
  50. * Put a value into the cache.
  51. */
  52. put(key, value)
  53. {
  54. this.store.set(key, value);
  55. this.access.set(key, Date.now());
  56. this._prune();
  57. }
  58. /*
  59. * Delete a value from the cache.
  60. */
  61. del(key)
  62. {
  63. this.store.delete(key);
  64. this.access.delete(key);
  65. }
  66. /*
  67. * Current size of the cache
  68. */
  69. size()
  70. {
  71. return this.store.size;
  72. }
  73. /*
  74. * Clear the LRU cache entirely.
  75. */
  76. clear()
  77. {
  78. this.store = new Map();
  79. this.access = new Map();
  80. }
  81. /*
  82. * Internal pruning function.
  83. */
  84. _prune()
  85. {
  86. debug('About to prune; have', this.store.size, 'and capacity is', this.capacity);
  87. var sorted = Array.from(this.access.entries());
  88. sorted.sort((first, second) => {
  89. if (first[1] == second[1]) {
  90. return 0;
  91. }
  92. return (first[1] < second[1] ? -1 : 1);
  93. });
  94. debug('Sorted keys are:', sorted);
  95. debug('Have to prune', this.store.size - this.capacity, 'items.');
  96. var idx = 0;
  97. var to_prune = [];
  98. while (idx < sorted.length && to_prune.length < (this.store.size - this.capacity)) {
  99. to_prune.push(sorted[idx][0]);
  100. ++idx;
  101. }
  102. to_prune.forEach((key) => {
  103. this.store.delete(key);
  104. this.access.delete(key);
  105. });
  106. debug('Size after pruning', this.store.size);
  107. }
  108. }
  109. module.exports = {
  110. LRUCache: LRUCache,
  111. };