一個簡單的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
是實際類