New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

cache-puppy

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cache-puppy

lightweight cache-revalidate

latest
Source
npmnpm
Version
1.0.6
Version published
Maintainers
1
Created
Source
alt text

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

PropertyTypedescription
get() => Tgets 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

PropertyTypedescriptiondefault
initialData?(() => T | Promise< T >) | Tinitial cache data or a function to be resolved fromundefined
revalidateFn?(() => T | Promise< T >) | Trevalidation data or a function to be resolved fromundefined
fallBackFn?(() => T | Promise< T >) | Tfallback value or a function to be resolved fromundefined
getterFn?() => T | undefinedcustom cache getter functionundefined
setterFn?(data) => voidcustom cache setter functionundefined
revalidateOpts?RevalidateOptsrevalidation optionsdefaultCacheOptions.revalidateOpts

RevalidateOpts

An object cache revalidation options

properties

PropertyTypedescriptiondefault
strategy?linear | exponentialcache retry strategylinear
interval?numbercache revalidation interval (ms)6000 (1 min)
backOff?numberretry backoff time (ms)300
exponentialBackoff?numberretry exponential backoff time (ms) (for exponential strategy)10
retriesnumbernumber of maximum retries3
onSuccess(cache) => Promise< void > | voidcallback on revalidation successundefined
onRetriesReached(cache, err) => Promise< void > | voidcallback on maximum retries reachedundefined

Todo

  • async getterFn/setterFn
  • tests

FAQs

Package last updated on 21 Nov 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts