@blued-core/interval-cache
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

interval-cache

用于实现一些定时更新的缓存。

install

npm install @blued-core/interval-cache

usage

import IntervalCache from '@blued-core/interval-cache'

const intervalCache = new IntervalCache()

const func = intervalCache.createCache('testa', () => 'Hello World')

func() // => Hello World
func() // => Hello World

// use async cache

async function test () {
  const proFunc = intervalCache.createThenableCache('testb', async () => Promise.resolve('ping'))

  proFunc().then(console.log)   // => ping

  console.log(await proFunc())  // => ping
}

config

实例化时传入的两个参数

import IntervalCache from '@blued-core/interval-cache'

const interval = 2000
const maxErrorCount = 10

const intervalCache = new IntervalCache(interval, maxErrorCount)
param type required default desc
interval number 1000ms 更新缓存的间隔(不包含代码执行的时间)
maxErrorCount number -1 连续报错终止的次数,默认为-1表示无限制,每次出错会按照interval进行叠加时长,例如,出错三次,interval100ms,则第二次执行的时间为200ms后,第三次为300ms后。 在正确获取到结果后会清除报错次数,重新计数

以上两个参数可以通过在调用createCachecreateThenableCache时传入第三个参数进行覆盖:

如不填写则使用全局配置

intervalCache.createCache('key', () => 'get data', {
  interval: 3000,
  maxErrorCount: 50
})

Readme

Keywords

none

Package Sidebar

Install

npm i @blued-core/interval-cache

Weekly Downloads

1

Version

1.0.4

License

ISC

Unpacked Size

11 kB

Total Files

6

Last publish

Collaborators

  • jarvan8888
  • jiasm
  • ryli
  • nieweidong
  • fengcang2020