lru-cache.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. ;(function () { // closure for web browsers
  2. if (typeof module === 'object' && module.exports) {
  3. module.exports = LRUCache
  4. } else {
  5. // just set the global for non-node platforms.
  6. this.LRUCache = LRUCache
  7. }
  8. function hOP (obj, key) {
  9. return Object.prototype.hasOwnProperty.call(obj, key)
  10. }
  11. function naiveLength () { return 1 }
  12. function LRUCache (options) {
  13. if (!(this instanceof LRUCache))
  14. return new LRUCache(options)
  15. if (typeof options === 'number')
  16. options = { max: options }
  17. if (!options)
  18. options = {}
  19. this._max = options.max
  20. // Kind of weird to have a default max of Infinity, but oh well.
  21. if (!this._max || !(typeof this._max === "number") || this._max <= 0 )
  22. this._max = Infinity
  23. this._lengthCalculator = options.length || naiveLength
  24. if (typeof this._lengthCalculator !== "function")
  25. this._lengthCalculator = naiveLength
  26. this._allowStale = options.stale || false
  27. this._maxAge = options.maxAge || null
  28. this._dispose = options.dispose
  29. this.reset()
  30. }
  31. // resize the cache when the max changes.
  32. Object.defineProperty(LRUCache.prototype, "max",
  33. { set : function (mL) {
  34. if (!mL || !(typeof mL === "number") || mL <= 0 ) mL = Infinity
  35. this._max = mL
  36. if (this._length > this._max) trim(this)
  37. }
  38. , get : function () { return this._max }
  39. , enumerable : true
  40. })
  41. // resize the cache when the lengthCalculator changes.
  42. Object.defineProperty(LRUCache.prototype, "lengthCalculator",
  43. { set : function (lC) {
  44. if (typeof lC !== "function") {
  45. this._lengthCalculator = naiveLength
  46. this._length = this._itemCount
  47. for (var key in this._cache) {
  48. this._cache[key].length = 1
  49. }
  50. } else {
  51. this._lengthCalculator = lC
  52. this._length = 0
  53. for (var key in this._cache) {
  54. this._cache[key].length = this._lengthCalculator(this._cache[key].value)
  55. this._length += this._cache[key].length
  56. }
  57. }
  58. if (this._length > this._max) trim(this)
  59. }
  60. , get : function () { return this._lengthCalculator }
  61. , enumerable : true
  62. })
  63. Object.defineProperty(LRUCache.prototype, "length",
  64. { get : function () { return this._length }
  65. , enumerable : true
  66. })
  67. Object.defineProperty(LRUCache.prototype, "itemCount",
  68. { get : function () { return this._itemCount }
  69. , enumerable : true
  70. })
  71. LRUCache.prototype.forEach = function (fn, thisp) {
  72. thisp = thisp || this
  73. var i = 0;
  74. for (var k = this._mru - 1; k >= 0 && i < this._itemCount; k--) if (this._lruList[k]) {
  75. i++
  76. var hit = this._lruList[k]
  77. if (this._maxAge && (Date.now() - hit.now > this._maxAge)) {
  78. del(this, hit)
  79. if (!this._allowStale) hit = undefined
  80. }
  81. if (hit) {
  82. fn.call(thisp, hit.value, hit.key, this)
  83. }
  84. }
  85. }
  86. LRUCache.prototype.keys = function () {
  87. var keys = new Array(this._itemCount)
  88. var i = 0
  89. for (var k = this._mru - 1; k >= 0 && i < this._itemCount; k--) if (this._lruList[k]) {
  90. var hit = this._lruList[k]
  91. keys[i++] = hit.key
  92. }
  93. return keys
  94. }
  95. LRUCache.prototype.values = function () {
  96. var values = new Array(this._itemCount)
  97. var i = 0
  98. for (var k = this._mru - 1; k >= 0 && i < this._itemCount; k--) if (this._lruList[k]) {
  99. var hit = this._lruList[k]
  100. values[i++] = hit.value
  101. }
  102. return values
  103. }
  104. LRUCache.prototype.reset = function () {
  105. if (this._dispose && this._cache) {
  106. for (var k in this._cache) {
  107. this._dispose(k, this._cache[k].value)
  108. }
  109. }
  110. this._cache = Object.create(null) // hash of items by key
  111. this._lruList = Object.create(null) // list of items in order of use recency
  112. this._mru = 0 // most recently used
  113. this._lru = 0 // least recently used
  114. this._length = 0 // number of items in the list
  115. this._itemCount = 0
  116. }
  117. // Provided for debugging/dev purposes only. No promises whatsoever that
  118. // this API stays stable.
  119. LRUCache.prototype.dump = function () {
  120. return this._cache
  121. }
  122. LRUCache.prototype.dumpLru = function () {
  123. return this._lruList
  124. }
  125. LRUCache.prototype.set = function (key, value) {
  126. if (hOP(this._cache, key)) {
  127. // dispose of the old one before overwriting
  128. if (this._dispose) this._dispose(key, this._cache[key].value)
  129. if (this._maxAge) this._cache[key].now = Date.now()
  130. this._cache[key].value = value
  131. this.get(key)
  132. return true
  133. }
  134. var len = this._lengthCalculator(value)
  135. var age = this._maxAge ? Date.now() : 0
  136. var hit = new Entry(key, value, this._mru++, len, age)
  137. // oversized objects fall out of cache automatically.
  138. if (hit.length > this._max) {
  139. if (this._dispose) this._dispose(key, value)
  140. return false
  141. }
  142. this._length += hit.length
  143. this._lruList[hit.lu] = this._cache[key] = hit
  144. this._itemCount ++
  145. if (this._length > this._max) trim(this)
  146. return true
  147. }
  148. LRUCache.prototype.has = function (key) {
  149. if (!hOP(this._cache, key)) return false
  150. var hit = this._cache[key]
  151. if (this._maxAge && (Date.now() - hit.now > this._maxAge)) {
  152. return false
  153. }
  154. return true
  155. }
  156. LRUCache.prototype.get = function (key) {
  157. return get(this, key, true)
  158. }
  159. LRUCache.prototype.peek = function (key) {
  160. return get(this, key, false)
  161. }
  162. LRUCache.prototype.pop = function () {
  163. var hit = this._lruList[this._lru]
  164. del(this, hit)
  165. return hit || null
  166. }
  167. LRUCache.prototype.del = function (key) {
  168. del(this, this._cache[key])
  169. }
  170. function get (self, key, doUse) {
  171. var hit = self._cache[key]
  172. if (hit) {
  173. if (self._maxAge && (Date.now() - hit.now > self._maxAge)) {
  174. del(self, hit)
  175. if (!self._allowStale) hit = undefined
  176. } else {
  177. if (doUse) use(self, hit)
  178. }
  179. if (hit) hit = hit.value
  180. }
  181. return hit
  182. }
  183. function use (self, hit) {
  184. shiftLU(self, hit)
  185. hit.lu = self._mru ++
  186. self._lruList[hit.lu] = hit
  187. }
  188. function trim (self) {
  189. while (self._lru < self._mru && self._length > self._max)
  190. del(self, self._lruList[self._lru])
  191. }
  192. function shiftLU (self, hit) {
  193. delete self._lruList[ hit.lu ]
  194. while (self._lru < self._mru && !self._lruList[self._lru]) self._lru ++
  195. }
  196. function del (self, hit) {
  197. if (hit) {
  198. if (self._dispose) self._dispose(hit.key, hit.value)
  199. self._length -= hit.length
  200. self._itemCount --
  201. delete self._cache[ hit.key ]
  202. shiftLU(self, hit)
  203. }
  204. }
  205. // classy, since V8 prefers predictable objects.
  206. function Entry (key, value, lu, length, now) {
  207. this.key = key
  208. this.value = value
  209. this.lu = lu
  210. this.length = length
  211. this.now = now
  212. }
  213. })()