1.0.4 • Published 4 years ago

@blued-core/interval-cache v1.0.4

Weekly downloads
1
License
ISC
Repository
-
Last release
4 years ago

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)
paramtyperequireddefaultdesc
intervalnumber1000ms更新缓存的间隔(不包含代码执行的时间)
maxErrorCountnumber-1连续报错终止的次数,默认为-1表示无限制,每次出错会按照interval进行叠加时长,例如,出错三次,interval100ms,则第二次执行的时间为200ms后,第三次为300ms后。 在正确获取到结果后会清除报错次数,重新计数

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

如不填写则使用全局配置

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

4 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.0

5 years ago