Skip to content

Latest commit

 

History

History

cache

Re-reselect cache objects

re-reselect cache makes use of the strategy pattern to provide custom interchangable caching implementations.

Available cache objects

re-reselect ships with 6 ready-to-use cache object constructors:

name accepted cacheKey type storage
FlatObjectCache number string flat unlimited JS object
FifoObjectCache number string first in first out JS object
LruObjectCache number string least recently used JS object
FlatMapCache any flat unlimited Map object
FifoMapCache any first in first out Map object
LruMapCache any least recently used Map object
import {createCachedSelector, LruObjectCache, LruMapCache} from 're-reselect';

createCachedSelector(
  // ...
)({
  keySelector,
  cacheObject: new LruObjectCache({cacheSize: 5}),
  // or:
  // cacheObject: new LruMapCache({cacheSize: 5}),
});

[*]ObjectCache strategy objects treat cacheKey of type number like strings, since they are used as arguments of JS objects.

[*]MapCache strategy objects need a Map objects polyfill in order to use them on non-supporting browsers.

Write your custom cache object

If none of the provided caching solutions fits your needs you can write your own cache object!

Declare a JS object adhering to the following interface and pass it to re-reselect as options.cacheObject:

interface ICacheObject {
  set(key: any, selectorFn: any): void;
  get(key: any): any;
  remove(key: any): void;
  clear(): void;
  isValidCacheKey?(key: any): boolean; // optional
}