README.md

redux-cache

This library provides an easy to use client side TTL caching strategy for redux applications.

Often, it is unnecessary to refetch data from an API if we know that it is unlikely to have changed within a certain period. By not fetching the data we are being friendly to mobile data users, limiting unnecessary API calls, and minimizing re-renders of applications due to state updates.

Installation

npm install -s redux-cache

Usage

Note: For a complete example, have a look at the example directory. You can clone this repo and actually start the example to see it working.

This will enhance your store so that whenever you dispatch the invalidateCache action, it automatically will invalidate the cache for any provided reducers. This means you do not have to bother with the boilerplate for every single reducer. Great for larger apps!

2. Set up your reducer

Included are a couple of utilities for setting up your reducer to make it "cache enabled".

Firstly, the DEFAULT_KEY is what redux-cache will use for storing and clearing cache values unless told to use an additional cache key. Secondly is the generateCacheTTL function which will create a timestamp for you.

accessStrategy - function - default: (state, reducerKey, cacheKey) => state[reducerKey][cacheKey]. Use this to overide the way in which the cacheKey is checked. This allows for greater configurability for applying the caching strategy to nested items in your reducer.

invalidateCache(reducers)

arguments

reducers - array (of strings) or single string - An array or string of reducers to invalidate.