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

kitkache

v0.4.0

Published

A basic, configurable client side cache

Downloads

2

Readme

Kitkache

Kitkache is a basic client side cache for in the browser.

Kitkache will wrap the value to be stored in an object with a timestamp. A config object defines how long the object in the localStorage will remain valid and when that period has passed it will retrieve a new value by calling the provided objectLoader callback.

Besides the period (in seconds) after which an object becomes invalid the config can also hold a function storeCondition that returns a boolean, so that the value will only be stored if the function evaluates to true. The value retrieved by the objectLoader callback is passed as an argument to storeCondition function.

Install

Using npm:

$ npm install kitkache

Using yarn:

$ yarn add kitkache

Using pnpm:

$ pnpm add kitkache

Usage

Store value of type YourCustomType for 2 hours

import {kitKache} from 'kitkache'

await kitKache<YourCustomType>(
  'key',
  {expireAfterHours: 2},
  () => {
    // logic to retrieve value
    return value
  },
)

Store string value for 5 minutes, unless the value equals 'tokyo'.

import {kitKache} from 'kitkache'

await kitKache<string>(
  'key',
  {
    expireAfterMins: 5,
    storeCondition: value => value !== 'tokyo',
  },
  () => {
    // logic to retrieve value
    return value
  },
)

The config object

The configuration to be passed to the kitKache function is simple:

{
  expireAfterSecs?: number
  expireAfterMins?: number
  expireAfterHours?: number
  storeCondition?: (value: T) => boolean
}

Setting the expiration period

You can define the period after which the cache becomes invalid (and thus a new value will be retrieved) in either seconds, minutes or hours. kitKache will always choose the most specific value. So if seconds, minutes and hours have been provided, only the value for seconds will be used. If both minutes and hours have been provided, the value for minutes will be used as the expiration period.

Providing a condition

You can also provide a condition wether a value should be stored in cache or not. The condition is a simple function that takes in a value (kitKache will automatically provide the value that is to be stored) and returns a boolean.

If the function return false, then the value will not be stored in the cache.

Imagine that the objectLoader retrieves an object from an external system, but that object should not be stored if the property 'fresh' in that object is set to 'true', then the condition should be something like the following:

(value: externalObject) => !value.fresh

The object loader

The objectLoader is an async function that returns a value. In it simplest form it could be for instance:

() => Promise.resolve('foo')

In more realistic scenarios it will most likely be a function that retrieves a value from an external system.

Project site