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
実際のクラスです