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