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

cache-puppy

v1.0.6

Published

lightweight cache-revalidate

Downloads

3

Readme

PR's Welcome

Lightweight application memory cache-revalidate with minimal retry/fallback strategy

 

Npm installation

$ npm install cache-puppy

Basic Usage

import { CachePuppy } from 'cache-puppy';
import axios from 'axios';

const fetchPets = async (): Promise<IPet[]> => {
  return (await axios.get('www.pets/api/puppies')).data;
};

// cached pets with revalidation every 5 minutes
const cache = new CachePuppy<IPet[]>({
  initialData: fetchPets,
  revalidateFn: fetchPets,
  fallbackFn: [
    { petId: 23, name: 'bulldog' },
    { petId: 18, name: 'yorkshire terrier' },
  ],
  revalidateOpts: {
    retries: 5,
    interval: 1000 * 60 * 5, // 5 mins
  },
});

// some time later
console.log(cache.get()); // cached pets

Basic Features

  • Simple to the point interface
  • Linear and exponential retry strategies
  • Provides custom Initial data, Revalidate and Fallback resolvers
  • Allows plugging in custom cache setters/getters (can be useful for using other in memory caching connectors e.g Redis, Memcached)

API

CachePuppy(options: CacheOpts)

methods

| Property | Type | description | | ---------- | :--------------------------: | -------------------------- | | get | () => T | gets cache value | | set | (data: T) => Promise<void> | sets cache value | | revalidate | () => Promise< void > | revalidates cache | | teardown | () => void | gracfully tears down cache |

CacheOpts

An object type representing cache options

properties

| Property | Type | description | default | | --------------- | :----------------------------------: | ---------------------------------------------------- | ---------------------------------- | | initialData? | (() => T \| Promise< T >) \| T | initial cache data or a function to be resolved from | undefined | | revalidateFn? | (() => T \| Promise< T >) \| T | revalidation data or a function to be resolved from | undefined | | fallBackFn? | (() => T \| Promise< T >) \| T | fallback value or a function to be resolved from | undefined | | getterFn? | () => T \| undefined | custom cache getter function | undefined | | setterFn? | (data) => void | custom cache setter function | undefined | | revalidateOpts? | RevalidateOpts | revalidation options | defaultCacheOptions.revalidateOpts |

RevalidateOpts

An object cache revalidation options

properties

| Property | Type | description | default | | ------------------- | :---------------------------------------: | -------------------------------------------------------------- | -------------- | | strategy? | linear \| exponential | cache retry strategy | linear | | interval? | number | cache revalidation interval (ms) | 6000 (1 min) | | backOff? | number | retry backoff time (ms) | 300 | | exponentialBackoff? | number | retry exponential backoff time (ms) (for exponential strategy) | 10 | | retries | number | number of maximum retries | 3 | | onSuccess | (cache) => Promise< void > \| void | callback on revalidation success | undefined | | onRetriesReached | (cache, err) => Promise< void > \| void | callback on maximum retries reached | undefined |

Todo

  • async getterFn/setterFn
  • tests