一个简单的node.js内存中缓存
npm install memory-cache --save
var cache = require ( 'memory-cache' ) ;
// now just use the cache
cache . put ( 'foo' , 'bar' ) ;
console . log ( cache . get ( 'foo' ) ) ;
// that wasn't too interesting, here's the good part
cache . put ( 'houdini' , 'disappear' , 100 , function ( key , value ) {
console . log ( key + ' did ' + value ) ;
} ) ; // Time in ms
console . log ( 'Houdini will now ' + cache . get ( 'houdini' ) ) ;
setTimeout ( function ( ) {
console . log ( 'Houdini is ' + cache . get ( 'houdini' ) ) ;
} , 200 ) ;
// create new cache instance
var newCache = new cache . Cache ( ) ;
newCache . put ( 'foo' , 'newbaz' ) ;
setTimeout ( function ( ) {
console . log ( 'foo in old cache is ' + cache . get ( 'foo' ) ) ;
console . log ( 'foo in new cache is ' + newCache . get ( 'foo' ) ) ;
} , 200 ) ;
应该打印
bar
Houdini will now disappear
houdini did disappear
Houdini is null
foo in old cache is baz
foo in new cache is newbaz
setTimeout
)中删除该值function(key, value) {}
null
== size()
,除非setTimeout
删除出错export
到缓存中的所有数据import
之前的任何现有条目将保留在缓存中skipDuplicates
,否则任何重复true
键都会被覆盖options
:skipDuplicates
:如果是true
,导入它们时将忽略任何重复的键。默认为false
。require('cache')
将返回缓存的默认实例require('cache').Cache
是实际类