2.0.1 • Published 1 year ago

@kaliber/cache v2.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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

PropertyValueDescription
allowReturnExpiredValuefalseif false the expirationTime will be leading in getting results from the cache
allowReturnExpiredValuetrueif 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.
expirationTimenumber(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.