x-cacher

2.0.0 • Public • Published

x-cacher

Build Status Coverage Status Greenkeeper badge npm codebeat badge

sample

import { Cacher, cacherDes } from 'x-cacher'

const cacher = new Cacher([{ type: 'memory' }])

class Person {
  constructor (name) {
    this.name = name
  }
  @cacherDes(cacher, ({ name: personName }, methodName) => personName + ':' + methodName)
  async eat () {
    return new Promise(resolve => {
      setTimeout(() => {
        resolve(this.name + ' eat: '+ Math.random())
      }, 2000)
    })
  }
}

const personA = new Person('a')

jest.setTimeout(1000 * 60)

/* eslint-env jest */
describe('cacher', () => {
  it('eat', async () => {
    let startAt = Date.now()
    let ret = await personA.eat()
    expect(ret).toMatch(/^a eat: /)
    expect(Date.now() - startAt).toBeGreaterThanOrEqual(2000)
    startAt = Date.now()
    let ret2 = await personA.eat()
    expect(ret2).toMatch(/^a eat: /)
    expect(ret).toEqual(ret2)
    expect(Date.now() - startAt).toBeLessThan(1000)
  })
})

Readme

Keywords

none

Package Sidebar

Install

npm i x-cacher

Weekly Downloads

1

Version

2.0.0

License

MIT

Unpacked Size

14 kB

Total Files

10

Last publish

Collaborators

  • sqlwwx