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

keshi

v2.0.7

Published

A better in-memory cache

Downloads

10,346

Readme

Keshi

Keshi on NPM Keshi on TravisCI

Keshi is a better in-memory (or custom) cache for Node and the browser.

const createCache = require('keshi')

or

import createCache from 'keshi'
const cache = createCache()

const user = await cache.resolve('user', () => fetch('https://myapi.com/user').then(r => r.json()), '30 mins')

What this will do:

  • Fetch the user from the API as it doesn't have it in cache.
  • If called again within 30 minutes it will return the cached user.
  • If called after 30 minutes it will fetch the user again and re-cache.

You should return only the data you need to keep the cache efficient. Here's a real world example of caching repository information from GitHub:

const cache = createCache()

// In the browser
const fetchProjectMeta = (user, repo) => () =>
  fetch(`https://api.github.com/repos/${user}/${repo}`)
    .then(r => r.json())
    .then(r => ({ name: r.full_name, description: r.description }))

// ...or in Node
const fetchProjectMeta = (user, repo) => () =>
  got
    .get(`https://api.github.com/repos/${user}/${repo}`, { json: true })
    .then(r => ({ name: r.body.full_name, description: r.body.description }))

// And call it (for 1 hour it will return cached results).
const meta = await cache.resolve('myRepo', fetchProjectMeta('DominicTobias', 'keshi'), '1 hour')

Rate limited APIs (as above), saving bandwidth, dealing with poor client network speeds, returning server responses faster are some of the reasons you might consider caching requests.

Keshi will automatically keep memory low by cleaning up expired items.

cache.resolve(key, [value], [expiresIn])

key IDBValidKey

A unique key to get and set the value from the store.

value? T | (() => T | Promise)

expiresIn? number | string

A number in milliseconds or anything that ms accepts after which the value is considered expired. If not provided then the value will be set once and has no expiry.

cache.del(key, [matchStart])

key IDBValidKey

A unique key to delete the cache for OR the start of such a key (possibly matching many).

matchStart? boolean

You can also delete any that start with the key by passing true to matchStart.

cache.del(`project.${projectId}.`, true)

cache.clear()

Clear the whole cache.

cache.teardown()

A stale cache cleanup interval is running in the background unless you set createCache({ cleanupInterval: 0 }). If your cache doesn't last the lifetime of your application then you should call teardown.

The default cache is in-memory, however the storage can be anything you like. To pass in a custom storage:

const customStorage = new MyCustomStorage()
const cache = createCache({ customStorage })

Your cache must implement the following methods:

Returns the cache value given the key. Cache values must be returned as an Array of [value, <expiresIn>]. expiresIn is an ISO Date string.

This method can be async.

Values set are of type Array in the following format: [value, <expiresIn>]. expiresIn should be an ISO Date string.

This method can be async.

Removes the item specified by key from the cache.

This method can be async.

Returns an array of cache keys.

This method can be async.

Clears all items from the cache.

The clear method of the public interface will return the results of this call. You could for example return a Promise that your app can wait on before performing subsequent actions.

import { get, set, keys, del, clear } from 'idb-keyval'

const customStorage = { get, set, keys, del, clear }
const cache = createCache({ customStorage })