pure cache
1.0.0
自信地緩存
Ultra Fast&Tiny(大約1.28kb Gzpipped )內存的JavaScript緩存,帶有接近實時的緩存到期功能
在任何JavaScript運行時工作(節點或瀏覽器)
npm install pure-cacheyarn add pure-cache < script src =" https://unpkg.com/pure-cache/dist/pure-cache.umd.js " > </ script > 導入庫
import PureCache from 'pure-cache' ;
// or const PureCache = require('pure-cache');創建cachestore實例
// Create instance of cache store and set cache expiry timeout to 500ms
const cacheStore = new PureCache ( { expiryCheckInterval : 500 } ) ;設置到期處理程序
// 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 ) ;從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 }等待到期
// 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' ) ; // null清理聽眾
// remove listeners after you are done
cacheStore . off ( 'expiry' , onExpiry ) ;
// IMPORTANT! When done, make sure you cleanup the instance
cacheStore . dispose ( ) ;檢查高級用法的API。
當添加,獲取,刪除,清除等操作在cachestore上或緩存到期時,會觸發事件。
到期
cacheStore . on ( 'expiry' , ( { key , data : { value , addedAt , expiryAt } } ) => {
// ...
} ) ;添加
cacheStore . on ( 'add' , ( { key , data : { value , addedAt , expiryAt } } ) => {
// ...
} ) ;得到
cacheStore . on ( 'get' , ( { key , data : { value , addedAt , expiryAt } } ) => {
// ...
} ) ;消除
cacheStore . on ( 'remove' , ( { key , data : { value , addedAt , expiryAt } } ) => {
// ...
} ) ;清除
cacheStore . on ( 'clear' , ( ) => {
// ...
} ) ; 感謝您抽出寶貴的時間做出貢獻,請閱讀文檔並結帳SRC了解事情的工作原理。
找到問題了?想要一個新功能嗎?首先,看看您的問題或想法是否已經報告。如果沒有,只需打開一個新的清晰和描述性問題即可。
拉請請求是最大的貢獻,因此請確保它們專注於範圍,並避免無關的提交。
git clone https://github.com/<your-username>/pure-cache >/pure-cachecd pure-cachegit checkout -b my-new-featureyarngit commit -am 'Add some feature'git push origin my-new-feature麻省理工學院許可©Ganapati vs