แคชในหน่วยความจำที่เรียบง่ายสำหรับ 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
เป็นคลาสจริง