1.0.6 • Published 2 years ago

cache-puppy v1.0.6

Weekly downloads
-
License
-
Repository
github
Last release
2 years ago

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() => voidgracfully 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
1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago