Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
apollo-cache-persist
Advanced tools
NOTE: This repo is in 'pre-release' and not quite ready to production use yet.
Simple persistence for all Apollo Cache 2.0+ implementations, including
apollo-cache-inmemory
and apollo-cache-hermes
.
Supports web and React Native. See all storage providers.
To get started, simply pass your Apollo Cache and an
underlying storage provider to persistCache
.
By default, the contents of your Apollo Cache will be immediately restored (asynchronously), and will be persisted upon every write to the cache (with a short debounce interval).
import { AsyncStorage } from 'react-native';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { persistCache } from 'apollo-cache-persist';
const cache = new InMemoryCache({...});
persistCache({
cache,
storage: AsyncStorage,
});
// Continue setting up Apollo as usual.
import { InMemoryCache } from 'apollo-cache-inmemory';
import { persistCache } from 'apollo-cache-persist';
const cache = new InMemoryCache({...});
persistCache({
cache,
storage: document.localStorage,
});
// Continue setting up Apollo as usual.
persistCache
and the constructor for CachePersistor
accept the following
options:
persistCache({
/**
* Required
*/
cache, // Reference to your Apollo Cache.
storage, // Reference to your storage provider.
/**
* Trigger options
*/
trigger: 'write', // Persist upon every write to the store. Default.
trigger: 'background', // Persist when your app moves to the background. React Native only.
// Debounce interval (in ms) between persists.
// Defaults to 1000 for 'write' and 0 for 'background'.
debounce: 1000,
/**
* Storage options
*/
// Key to use with the storage provider.
key: 'apollo-cache-persist',
// Whether to JSON serialize before/after persisting.
serialize: true,
/**
* Miscellaneous
*/
// Purges storage & pauses persistence if trigger causes cache size to pass a threshold (in bytes). App will start up cold.
maxSize?: number,
// Enable console logging.
debug: false,
});
Instead of using persistCache
, you can instantiate a CachePersistor
, which
will give you fine-grained control of persistence.
CachePersistor
accepts the same options as persistCache
and returns an
object with the following methods:
const persistor = new CachePersistor({...});
persistor.restore(); // Immediately restore the cache. Returns a Promise.
persistor.persist(); // Immediately persist the cache. Returns a Promise.
persistor.purge(); // Immediately purge the stored cache. Returns a Promise.
persistor.pause(); // Pause persistence. Triggers are ignored while paused.
persistor.resume(); // Resume persistence.
persistor.remove(); // Remove the persistence trigger. Manual persistence required after calling this.
// Obtain the most recent 30 persistor loglines.
// `print: true` will print them to the console; `false` will return an array.
persistor.getLogs(print);
// Obtain the current persisted cache size in bytes. Returns a Promise.
// Resolves to 0 for empty and `null` when `serialize: true` is in use.
persistor.getSize();
The following storage providers work 'out of the box', with no additional dependencies:
AsyncStorage
on React Nativedocument.localStorage
on webdocument.sessionStorage
on weblocalForage
on webapollo-cache-persist
uses the same storage provider API as
redux-persist
, so you can also make
use of the providers listed here, including:
FAQs
Simple persistence for all Apollo cache implementations
The npm package apollo-cache-persist receives a total of 14,729 weekly downloads. As such, apollo-cache-persist popularity was classified as popular.
We found that apollo-cache-persist demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
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.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.