Node.js 中的 LRU 缓存
LRU Cache in Node js
我需要为我的项目(为我的组织)实施缓存,我们计划使用内存中的 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 的最佳实践是什么。
让我们稍微修改一下您的代码,以便我更好地解释问题所在。
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,10) // differentiate the key and the value
cache.set(2,20)
cache.set(3,30)
console.log(cache.length)
console.log(cache.get(1))
console.log(cache.get(2))
console.log(cache.get(3))
console.log(cache)
每次在缓存中设置值时都会调用长度函数。当你调用cache.set(1,10)
时,你之前定义的函数length有参数:n(数字10)和key(数字1)。
所以你在这里看到 key.length
是未定义的,因为数字没有长度 属性,与 undefined
的总和将是 NaN
。在文档中,作者使用 属性 长度,因为通常缓存键是一个字符串。您当然可以使用数字作为键,但这就是这里的问题。
修复此问题后,您必须注意函数dispose。我引用作者:
dispose: Function that is called on items when they are dropped from the cache.
This can be handy if you want to close file descriptors or do other
cleanup tasks when items are no longer accessible.
在这个简单的例子中,我认为你不需要它,所以你可以删除它。
我需要为我的项目(为我的组织)实施缓存,我们计划使用内存中的 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 的最佳实践是什么。
让我们稍微修改一下您的代码,以便我更好地解释问题所在。
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,10) // differentiate the key and the value
cache.set(2,20)
cache.set(3,30)
console.log(cache.length)
console.log(cache.get(1))
console.log(cache.get(2))
console.log(cache.get(3))
console.log(cache)
每次在缓存中设置值时都会调用长度函数。当你调用cache.set(1,10)
时,你之前定义的函数length有参数:n(数字10)和key(数字1)。
所以你在这里看到 key.length
是未定义的,因为数字没有长度 属性,与 undefined
的总和将是 NaN
。在文档中,作者使用 属性 长度,因为通常缓存键是一个字符串。您当然可以使用数字作为键,但这就是这里的问题。
修复此问题后,您必须注意函数dispose。我引用作者:
dispose: Function that is called on items when they are dropped from the cache. This can be handy if you want to close file descriptors or do other cleanup tasks when items are no longer accessible.
在这个简单的例子中,我认为你不需要它,所以你可以删除它。