Cache dengan percaya diri
Ultra Fast & Tiny ( sekitar 1.28kb gzipped ) Cache JavaScript in-memory dengan fitur Expiry Cache Near Realtime Cache
Bekerja di runtime JavaScript apa pun (node atau browser)
npm install pure-cacheyarn add pure-cache < script src =" https://unpkg.com/pure-cache/dist/pure-cache.umd.js " > </ script > Impor Perpustakaan
import PureCache from 'pure-cache' ;
// or const PureCache = require('pure-cache');Buat instance Cachestore
// Create instance of cache store and set cache expiry timeout to 500ms
const cacheStore = new PureCache ( { expiryCheckInterval : 500 } ) ;Pengaturan penangan kedaluwarsa
// Setup a expiry listener, this will be called when data expires
const onExpiry = ( { key , data : { value , expiryAt } } ) => {
// Do something with expired key
console . log ( `Key: ${ key } with value: ${ value } expired at ${ expiryAt } .` ) ;
} ;
cacheStore . on ( 'expiry' , onExpiry ) ;Letakkan/Dapatkan Data dari Cachestore
// Put 'bar' data into 'foo' key in cache and configure it to expire after 30s
cacheStore . put ( 'foo' , 'bar' , 30000 ) ;
// Get 'foo' key value from cache
cacheStore . get ( 'foo' ) ; // { value: 'bar', addedAt: 1527052395294, expiryAt: 1527052425294 }Tunggu kedaluwarsa
// Wait till expiry time(basically 30+ seconds in this case)
const wait = t => new Promise ( r => setTimeout ( r , t ) ) ;
await wait ( 31000 ) ;
// Now the cache will return null value for 'foo' key
cacheStore . get ( 'foo' ) ; // nullPendengar pembersihan
// remove listeners after you are done
cacheStore . off ( 'expiry' , onExpiry ) ;
// IMPORTANT! When done, make sure you cleanup the instance
cacheStore . dispose ( ) ;API Checkout untuk penggunaan lanjutan.
Acara dipicu saat operasi seperti Add, Get, Remeed, Clear dilakukan di Cachestore atau ketika cache berakhir.
kedaluwarsa
cacheStore . on ( 'expiry' , ( { key , data : { value , addedAt , expiryAt } } ) => {
// ...
} ) ;menambahkan
cacheStore . on ( 'add' , ( { key , data : { value , addedAt , expiryAt } } ) => {
// ...
} ) ;mendapatkan
cacheStore . on ( 'get' , ( { key , data : { value , addedAt , expiryAt } } ) => {
// ...
} ) ;menghapus
cacheStore . on ( 'remove' , ( { key , data : { value , addedAt , expiryAt } } ) => {
// ...
} ) ;jernih
cacheStore . on ( 'clear' , ( ) => {
// ...
} ) ; Terima kasih telah meluangkan waktu untuk berkontribusi, silakan baca dokumen dan checkout SRC untuk memahami cara kerja.
Menemukan masalah? Ingin fitur baru? Pertama -tama lihat apakah masalah atau ide Anda telah dilaporkan. Jika tidak, buka saja masalah baru yang jelas dan deskriptif.
Permintaan tarik adalah kontribusi terbesar, jadi pastikan mereka fokus dalam ruang lingkup, dan hindari komitmen yang tidak terkait.
git clone https://github.com/<your-username>/pure-cachecd pure-cachegit checkout -b my-new-featureyarngit commit -am 'Add some feature'git push origin my-new-featureLisensi MIT © Ganapati VS