Complete the code to fetch data from an API and cache it using Astro's cache API.
const response = await fetch('[1]'); const data = await response.json();
The correct API URL is 'https://api.example.com/data' to fetch the intended data.
Complete the code to cache the API response for 1 hour using Astro's cache API.
const cachedData = await Astro.cache('[1]', async () => { const response = await fetch('https://api.example.com/data'); return await response.json(); }, { ttl: 3600 });
The cache key 'cacheKey' is used to store and retrieve the cached API response.
Fix the error in the code to correctly cache the API response with a 30-minute expiration.
const data = await Astro.cache('apiData', async () => { const response = await fetch('https://api.example.com/data'); return await response.json(); }, { ttl: [1] });
The ttl (time to live) should be 1800 seconds for 30 minutes caching.
Fill both blanks to create a cache key using the API endpoint and user ID.
const cacheKey = '[1]' + '_' + [2]; const data = await Astro.cache(cacheKey, async () => { const response = await fetch('https://api.example.com/data'); return await response.json(); }, { ttl: 600 });
The cache key combines a string 'apiData' and the variable userId to uniquely identify cached data per user.
Fill all three blanks to fetch, cache, and handle errors gracefully.
try { const data = await Astro.cache([1], async () => { const response = await fetch([2]); if (!response.ok) throw new Error('Network error'); return await response.json(); }, { ttl: [3] }); } catch (error) { console.error(error); }
The cache key is 'cacheKey', the API URL is 'https://api.example.com/user', and ttl is 300 seconds for 5 minutes caching.