A simplified store for storing cached data. Useful for data that needs to keep in sync with it's source.
npm install svelte-cached
import { cached } from 'svelte-cached';
Set the initial value for the store. To keep the store in sync with it's source, provide a retrieve async function as the second argument.
TTL is set as third argument, if not provided you will need to manually call store.retrieve()
function whenever you want to sync the state.
const getLatestBitcoinPrice = async () => {
const response = await fetch(...);
// ...
return price;
};
// get a fresh price every 5 seconds
const btcPrice = cached(0, getLatestBitcoinPrice, 5000);
Use the cached store instance just like any other Svelte store.
<p>Current BTC price:{$btcPrice}</p>