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

@vlsergey/react-cache-context

v0.7.0

Published

React Context for caching values by key, optionally with batched values supplier

Downloads

19

Readme

@vlsergey/react-cache-context

NPM version CI Status Downloads

React Context for caching values by key.

Main features:

  • [x] Key-value cache with simple useCacheValue() hook
  • [x] Supports multiple caches, each uses it's own React context. Updating single cache will not result in whole-application rerender.
  • [x] Can be integrated with LRU / TTL map implementations with mapSupplier property.

Installation:

npm install --save @vlsergey/react-cache-context

or

npm install --save-dev @vlsergey/react-cache-context

Usage

// in application root
import {CachesStore, RegisterCache} from "@vlsergey/react-cache-context";

/* ... */
// include CachesStore inside any authorization providers/wrappers if any present,
// but outside of routing providers
return <CachesStore>
  <RegisterCache
    cacheId="groupsCache"
    getter={ async ( groupId ) => (await new Api().groups.findById( groupId )).data }>
    <RegisterCache
      cacheId="studentsCache"
      getter={ async ( studentId ) => (await new Api().students.findById( studentId )).data }>
        { /* ... */ }
    </RegisterCache>
  </RegisterCache>
</CachesStore>
// in some (function) component
import {useCacheValue} from "@vlsergey/react-cache-context";

const StudentName = ( studentId ) => {
  const student = useCacheValue( 'studentsCache', studentId );
  return (student || {}).name || studentId;
}

API

<CachesStore>

Root element, holds map of all caches contexts. All <RegisterCache> elements must be inside of <CachesStore>.

It is advised to include <CachesStore> after/inside or any authentication wrappers if any present, thus invalidating all caches if user changed due to relogin.

| Property | Data type | Default value | Description | | ----------------------- | ---------------------------------------------- | ------------- | ----------- | | caches | Record<string, CacheSpec<unknown, unknown>> | undefined | Can be used instead of multiple <RegisterCache> to register caches. CacheSpec can be either simple item getter function ((key: K) => Promise<V>) or object with getter, missingValue and mapSupplier properties from <RegisterCache> |

<RegisterCache>

Registers and holds values (errors, loading state) of single cache.

Any number or <RegisterCache> with different cacheId can be used.

Multiple elements with same cacheId can be used, but only one of them (nearest parent to current functional component) will actually provide values.

Be careful with autogenerated cacheId, because map entries from cacheId to react context type are not garbage collected.

| Property | Data type | Default value | Description | | ----------------------- | ---------------------------------------------- | ------------- | ----------- | | cacheId | string | required | Identifies cache context. Will be used by client hooks later to provide access particular context holded (provided) by current <RegisterCache> element. | | getter | (key: K) => Promise | required | Explains to library how to obtain element. May throw an error. Promise may return null or undefined and those will be remembered as results. | | mapSupplier | (cacheId: string) => CacheMap<K, T> | () => new Map() | Can be used to provide custom (LRU, TTL) map implementation to store errors/results | | missingValue | V | undefined | What should hooks return if value not available yet. |

useCache( cacheId: string ) : CacheContext<K, V>

Provide access to cache context with specified cacheId. Hook should be called inside component inside corresponding <RegisterCache> element (otherwise all calls to it's methods will result in error).

CacheContext<K, V> provides man methods to get or invalidate information from single cache:

interface CacheContext<K extends Key, V> {
  clear: () => unknown;
  delete: (key: K) => unknown;
  get: (key: K) => V;
  getPromise: (key: K) => Promise<V>;
}

useCacheValue<K, V>( cacheId: string, key: K ) : V

Returns currently stored value from cache or throws error if last call of getter with provided key resulted an error. If no value present (and loading is not in progress yet) will generate async call to getter to obtain value.

If no value and no error present in cache immediately returns missingValue property of <RegisterCache>, or undefined if such property were not set.

Current implementation will retry to obtain value if last getter returns an error.

useCachePromise<K, V>( cacheId: string, key: K ) : V

Returns currently stored value from cache (as Promise.resolve(…)). If no value present new promise is obtained from getter and returned as result. This method ignores stored errors (i.e. always repeat failed queries), but only one query is executed at the same time for any particular key.

Use as LRU (last recently used) or TTL cache

With mapSupplier property of RegisterCache component one can use LRU or TTL map implementations as internal cache store. Note, that internally two maps are used for each cache: map of values (CacheMap<K, V>) and maps of errors (CacheMap<K, unknown>).

Provided map must conform to following interface (partial interface of JavaScript Map):

interface CacheMap<K extends Key, V> {
  clear: () => unknown;
  delete: (key: K) => unknown;
  has: (key: K) => boolean;
  get: (key: K) => V;
  set: (key: K, value: V) => unknown;
}