npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-map-cache

v2.0.0

Published

Use JS Map object for caching for JavaScript (NodeJS and Broswers)

Downloads

66

Readme

Map Cache

status

Map Cache is a singleton class that caches the data returned from any function for a specified amount of time (defaults to 5 minutes). If the data has been cached and has not expired, Map Cache will return the cached data without calling the function, if there is no cached data, it will call the function and store its returned data.

It works both on Node.JS and browsers that support btoa. Under the hood it uses the JS Map object to store data.

Motivation

I often have the need to cache the data I retrieve through API calls, but most of the packages available are outdated, not fully typed or don't do exactly what I need.

Tooling

  • TypeScript
  • Jest
  • Babel
  • Prettier
  • Eslint

Tests

yarn test

The code has 100% coverage (tests are run both with node and jsdom to ensure compatibility with both).

Parameters

mapCache exposes the fetch, clear and size methods.

The fetch method takes as parameter an object with the following properties:

| Property | Optional | Default | Description | | ---------------- | -------- | ----------- | ---------------------------------------------------------------------- | | key | no | | An arbitrary string | | params | yes | | If the calback yelds differets data based on parameters, add them here | | callback | no | | The callback used to fetch the data you want to cache | | expiresInSeconds | yes | 300 (5 min) | How long the data will last in the cache |

Example

// <T> is the type of the data that will be returned from the callback
const data = await mapCache.fetch<T>({
  key: 'someKey', // an arbitrary string
  params: { id: 0 }, // optional
  callback: () => 'data', // the function returned data will be stored in the cache
  expiresInSeconds: 10 // optional, defaults to 5 minutes
})

The clear method clears the cache.

mapCache.clear()

The size method returns the number of entries stored in the cache.

console.log(mapCache.size())

Usage

Basic usage

import mapCache from 'ts-map-cache'

async function basicExample() {
  // The callback can be a sync or async function, but the fetch method has to be always awaited
  const someFunction = () => {
    console.log('I have been called!')
    return 'some_data'
  }

  // Since fetch is async, await it
  const data1 = await mapCache.fetch<string>({ key: 'basicFunction', callback: someFunction })
  console.log(`it called the function and returned the data: ${data1}`)

  const data2 = await mapCache.fetch<string>({ key: 'basicFunction', callback: someFunction })
  console.log(`it returned the cached data: ${data2}`)
}

basicExample()

With expiration

import mapCache from 'ts-map-cache'

async function basicExampleWithExpiration() {
  const someFunction = async () => {
    console.log('I have been called!')
    return 'some_data'
  }

  const data = await mapCache.fetch<string>({
    key: 'basicFunction',
    callback: someFunction,
    expiresInSeconds: 1
  })
  console.log(`it called the function and returned the data: ${data}`)

  setTimeout(async () => {
    const data = await mapCache.fetch<string>({ key: 'basicFunction', callback: someFunction })
    console.log(`it re fetched the data: ${data}`)
  }, 2000)
}

basicExampleWithExpiration()

With params

Params is useful when the same function returns different values based on the parameters it receives. Passing the parameters also to the fetch method will automatically build an unique id for that function/returned data. It can be useful, for example, with GraphQL query resolvers.

import mapCache from 'ts-map-cache'

async function basicExampleWithParams() {
  const someFunction = async (params: { id: number }) => {
    console.log(`I have been called with id ${params.id}!`)
    return `some_data for id ${params.id}`
  }

  const params1 = { id: 0 }
  const data1 = await mapCache.fetch<string>({
    key: 'basicFunction',
    callback: async () => someFunction(params1),
    params: params1
  })

  console.log(`it called the function and returned the data: ${data1}`)

  const params2 = { id: 1 }
  const data2 = await mapCache.fetch<string>({
    key: 'basicFunction',
    callback: async () => someFunction(params2),
    params: params2
  })

  console.log(`it called the function and returned the data: ${data2}`)
}

basicExampleWithParams()

With a network request

import mapCache from 'ts-map-cache'
import axios from 'axios'

interface IData {
  userId: number
  id: number
  title: string
  completed: boolean
}

async function main() {
  const getData = async () => {
    return await axios
      .get('https://jsonplaceholder.typicode.com/todos/1')
      .then(({ data }) => data)
      .catch((err) => console.log(err))
  }

  // This request will appear on the network tab of the dev tools
  let data = await mapCache.fetch<IData>({ key: 'fetch', callback: getData })

  // This will not since it's getting the cached data
  data = await mapCache.fetch<IData>({ key: 'fetch', callback: getData })
}

main()