lru-cache.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. var itemCount = this._itemCount
  75. for (var k = this._mru - 1; k >= 0 && i < itemCount; k--) if (this._lruList[k]) {
  76. i++
  77. var hit = this._lruList[k]
  78. if (isStale(this, hit)) {
  79. del(this, hit)
  80. if (!this._allowStale) hit = undefined
  81. }
  82. if (hit) {
  83. fn.call(thisp, hit.value, hit.key, this)
  84. }
  85. }
  86. }
  87. LRUCache.prototype.keys = function () {
  88. var keys = new Array(this._itemCount)
  89. var i = 0
  90. for (var k = this._mru - 1; k >= 0 && i < this._itemCount; k--) if (this._lruList[k]) {
  91. var hit = this._lruList[k]
  92. keys[i++] = hit.key
  93. }
  94. return keys
  95. }
  96. LRUCache.prototype.values = function () {
  97. var values = new Array(this._itemCount)
  98. var i = 0
  99. for (var k = this._mru - 1; k >= 0 && i < this._itemCount; k--) if (this._lruList[k]) {
  100. var hit = this._lruList[k]
  101. values[i++] = hit.value
  102. }
  103. return values
  104. }
  105. LRUCache.prototype.reset = function () {
  106. if (this._dispose && this._cache) {
  107. for (var k in this._cache) {
  108. this._dispose(k, this._cache[k].value)
  109. }
  110. }
  111. this._cache = Object.create(null) // hash of items by key
  112. this._lruList = Object.create(null) // list of items in order of use recency
  113. this._mru = 0 // most recently used
  114. this._lru = 0 // least recently used
  115. this._length = 0 // number of items in the list
  116. this._itemCount = 0
  117. }
  118. LRUCache.prototype.dump = function () {
  119. var arr = []
  120. var i = 0
  121. for (var k = this._mru - 1; k >= 0 && i < this._itemCount; k--) if (this._lruList[k]) {
  122. var hit = this._lruList[k]
  123. if (!isStale(this, hit)) {
  124. //Do not store staled hits
  125. ++i
  126. arr.push({
  127. k: hit.key,
  128. v: hit.value,
  129. e: hit.now + (hit.maxAge || 0)
  130. });
  131. }
  132. }
  133. //arr has the most read first
  134. return arr
  135. }
  136. LRUCache.prototype.dumpLru = function () {
  137. return this._lruList
  138. }
  139. LRUCache.prototype.set = function (key, value, maxAge) {
  140. maxAge = maxAge || this._maxAge
  141. var now = maxAge ? Date.now() : 0
  142. var len = this._lengthCalculator(value)
  143. if (hOP(this._cache, key)) {
  144. if (len > this._max) {
  145. del(this, this._cache[key])
  146. return false
  147. }
  148. // dispose of the old one before overwriting
  149. if (this._dispose)
  150. this._dispose(key, this._cache[key].value)
  151. this._cache[key].now = now
  152. this._cache[key].maxAge = maxAge
  153. this._cache[key].value = value
  154. this._length += (len - this._cache[key].length)
  155. this._cache[key].length = len
  156. this.get(key)
  157. if (this._length > this._max)
  158. trim(this)
  159. return true
  160. }
  161. var hit = new Entry(key, value, this._mru++, len, now, maxAge)
  162. // oversized objects fall out of cache automatically.
  163. if (hit.length > this._max) {
  164. if (this._dispose) this._dispose(key, value)
  165. return false
  166. }
  167. this._length += hit.length
  168. this._lruList[hit.lu] = this._cache[key] = hit
  169. this._itemCount ++
  170. if (this._length > this._max)
  171. trim(this)
  172. return true
  173. }
  174. LRUCache.prototype.has = function (key) {
  175. if (!hOP(this._cache, key)) return false
  176. var hit = this._cache[key]
  177. if (isStale(this, hit)) {
  178. return false
  179. }
  180. return true
  181. }
  182. LRUCache.prototype.get = function (key) {
  183. return get(this, key, true)
  184. }
  185. LRUCache.prototype.peek = function (key) {
  186. return get(this, key, false)
  187. }
  188. LRUCache.prototype.pop = function () {
  189. var hit = this._lruList[this._lru]
  190. del(this, hit)
  191. return hit || null
  192. }
  193. LRUCache.prototype.del = function (key) {
  194. del(this, this._cache[key])
  195. }
  196. LRUCache.prototype.load = function (arr) {
  197. //reset the cache
  198. this.reset();
  199. var now = Date.now()
  200. //A previous serialized cache has the most recent items first
  201. for (var l = arr.length - 1; l >= 0; l-- ) {
  202. var hit = arr[l]
  203. var expiresAt = hit.e || 0
  204. if (expiresAt === 0) {
  205. //the item was created without expiration in a non aged cache
  206. this.set(hit.k, hit.v)
  207. } else {
  208. var maxAge = expiresAt - now
  209. //dont add already expired items
  210. if (maxAge > 0) this.set(hit.k, hit.v, maxAge)
  211. }
  212. }
  213. }
  214. function get (self, key, doUse) {
  215. var hit = self._cache[key]
  216. if (hit) {
  217. if (isStale(self, hit)) {
  218. del(self, hit)
  219. if (!self._allowStale) hit = undefined
  220. } else {
  221. if (doUse) use(self, hit)
  222. }
  223. if (hit) hit = hit.value
  224. }
  225. return hit
  226. }
  227. function isStale(self, hit) {
  228. if (!hit || (!hit.maxAge && !self._maxAge)) return false
  229. var stale = false;
  230. var diff = Date.now() - hit.now
  231. if (hit.maxAge) {
  232. stale = diff > hit.maxAge
  233. } else {
  234. stale = self._maxAge && (diff > self._maxAge)
  235. }
  236. return stale;
  237. }
  238. function use (self, hit) {
  239. shiftLU(self, hit)
  240. hit.lu = self._mru ++
  241. self._lruList[hit.lu] = hit
  242. }
  243. function trim (self) {
  244. while (self._lru < self._mru && self._length > self._max)
  245. del(self, self._lruList[self._lru])
  246. }
  247. function shiftLU (self, hit) {
  248. delete self._lruList[ hit.lu ]
  249. while (self._lru < self._mru && !self._lruList[self._lru]) self._lru ++
  250. }
  251. function del (self, hit) {
  252. if (hit) {
  253. if (self._dispose) self._dispose(hit.key, hit.value)
  254. self._length -= hit.length
  255. self._itemCount --
  256. delete self._cache[ hit.key ]
  257. shiftLU(self, hit)
  258. }
  259. }
  260. // classy, since V8 prefers predictable objects.
  261. function Entry (key, value, lu, length, now, maxAge) {
  262. this.key = key
  263. this.value = value
  264. this.lu = lu
  265. this.length = length
  266. this.now = now
  267. if (maxAge) this.maxAge = maxAge
  268. }
  269. })()