【发布时间】:2017-12-31 02:42:20
【问题描述】:
我需要为我的项目(为我的组织)实现缓存,我们计划在内存中进行 LRU 缓存,我有一些包,但我不确定许可条款,我发现的最好的是这个
https://www.npmjs.com/package/lru-cache
但是当我将缓存声明为
时,我遇到了一些问题 var LRU = require("lru-cache")
, options = { max: 2
, length: function (n, key) { return n * 2 + key.length }
, dispose: function (key, n) { n.close() }
, maxAge: 1000 * 60 * 60 }
, cache = LRU(options)
console.log(cache.length)
cache.set(1,1)
cache.set(2,2)
cache.set(3,3)
console.log(cache.length)
console.log(cache.get(1))
console.log(cache.get(2))
console.log(cache.get(3))
console.log(cache)
上面代码的输出是
0
NaN
1
2
3
LRUCache {}
它没有设置最大值,它似乎是无穷大 即使长度为 2,它也不会删除 LRU 元素并将所有三个元素添加到缓存中
还有其他可用的包吗?我也在考虑实现自己的缓存机制,node js的最佳实践是什么。
【问题讨论】:
-
你写哪一行代码来获得
cache length NaN?当我复制你的代码时,我得到:cache.length === 0。你能在控制台记录对象“缓存”时显示你得到的结果吗? -
@Wing-OnYuen 我修改了我的问题