lru.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. constructor(capacity = DEFAULT_CAPACITY) {
  26. this.capacity = capacity
  27. this.clear()
  28. }
  29. /*
  30. * Return the entry with the given key, and update it's usage.
  31. */
  32. get(key) {
  33. const val = this.store.get(key)
  34. if (val) {
  35. this.access.set(key, Date.now())
  36. }
  37. return val
  38. }
  39. /*
  40. * Return true if the key is the cache, false otherwise.
  41. */
  42. has(key) {
  43. return this.store.has(key)
  44. }
  45. /*
  46. * Put a value into the cache.
  47. */
  48. put(key, value) {
  49. this.store.set(key, value)
  50. this.access.set(key, Date.now())
  51. this._prune()
  52. }
  53. /*
  54. * Delete a value from the cache.
  55. */
  56. del(key) {
  57. this.store.delete(key)
  58. this.access.delete(key)
  59. }
  60. /*
  61. * Current size of the cache
  62. */
  63. size() {
  64. return this.store.size
  65. }
  66. /*
  67. * Clear the LRU cache entirely.
  68. */
  69. clear() {
  70. this.store = new Map()
  71. this.access = new Map()
  72. }
  73. /*
  74. * Internal pruning function.
  75. */
  76. _prune() {
  77. debug('About to prune; have', this.store.size, 'and capacity is', this.capacity)
  78. const sorted = Array.from(this.access.entries())
  79. sorted.sort((first, second) => {
  80. if (first[1] === second[1]) {
  81. return 0
  82. }
  83. return first[1] < second[1] ? -1 : 1
  84. })
  85. debug('Sorted keys are:', sorted)
  86. debug('Have to prune', this.store.size - this.capacity, 'items.')
  87. let idx = 0
  88. const toPrune = []
  89. while (idx < sorted.length && toPrune.length < this.store.size - this.capacity) {
  90. toPrune.push(sorted[idx][0])
  91. ++idx
  92. }
  93. toPrune.forEach((key) => {
  94. this.store.delete(key)
  95. this.access.delete(key)
  96. })
  97. debug('Size after pruning', this.store.size)
  98. }
  99. }
  100. module.exports = {
  101. LRUCache,
  102. }