@kaliber/cache

2.0.0 • Public • Published

Cache

Simple memory cache function.

Motivation

Some times you want to cache a value but don't need a complex caching system. This package helps you cache values in side the memory of your application.

Installation

yarn add @kaliber/cache

Usage

import fetch from 'node-fetch'
import { createCache } from '@kaliber/cache'

const cache = createCache({ allowReturnExpiredValue: false, expirationTime: 1000 })

function handleRequest(postId) {
  const data = cache({
    cacheKey: ['post', postId],
    async getValue() {
      const response = await fetch(`https://jsonplaceholder.typicode.com/posts/${postId}`)
      return await response.json()
    }
  })

  return data
}

createCache

Property Value Description
allowReturnExpiredValue false if false the expirationTime will be leading in getting results from the cache
allowReturnExpiredValue true if true the cache is in 'unsafe' safe mode this means that the cache will always return a value even if the cache time is expired. Of course the cache needs to have a value to get value from it.
expirationTime number (in milliseconds) this is the time the cache is valid.

Disclaimer

This library is intended for internal use, we provide no support, use at your own risk. It does not import React, but expects it to be provided, which @kaliber/build can handle for you.

This library is not transpiled.

Readme

Keywords

Package Sidebar

Install

npm i @kaliber/cache

Weekly Downloads

1

Version

2.0.0

License

MIT

Unpacked Size

8.89 kB

Total Files

12

Last publish

Collaborators

  • melissabos18
  • janbru
  • albertkaliber
  • jerryduijm
  • hjsielcken
  • kaliber-owner
  • daveykropf
  • eecolor
  • larsvankleef