📖 Scenario: You are building a web app that fetches user profiles from a Supabase database. To improve performance and reduce repeated network calls, you want to implement a simple caching strategy in your JavaScript code.
🎯 Goal: Build a caching mechanism that stores fetched user profiles in memory. When a profile is requested, the app should first check the cache before querying Supabase. This reduces unnecessary database calls and speeds up the app.
📋 What You'll Learn
Create a cache object to store user profiles keyed by user ID
Add a configuration variable for cache expiration time in milliseconds
Write a function to fetch user profiles that checks the cache first
Update the cache with fresh data after fetching from Supabase
💡 Why This Matters
🌍 Real World
Caching user data reduces network calls and speeds up web apps, improving user experience.
💼 Career
Understanding caching strategies is essential for cloud developers to optimize app performance and resource usage.
Progress0 / 4 steps
1
Create a cache object
Create a JavaScript object called userCache that will store cached user profiles keyed by user ID.
Supabase
Hint
Think of userCache as a simple box where you keep user data for quick access.
2
Add cache expiration configuration
Add a constant called CACHE_EXPIRATION_MS and set it to 300000 (5 minutes in milliseconds) to define how long cached data is valid.
Supabase
Hint
This value controls how long you trust the cached data before fetching fresh data.
3
Write a function to fetch user profiles with cache check
Write an async function called getUserProfile that takes userId as a parameter. Inside, check if userCache[userId] exists and is not expired by comparing timestamps. If valid, return the cached profile. Otherwise, fetch the profile from Supabase using supabase.from('profiles').select('*').eq('id', userId).single() and update the cache with the new data and current timestamp.
Supabase
Hint
Check the cache first. If data is fresh, return it. Otherwise, fetch from Supabase and update the cache.
4
Complete by exporting the function
Add an export statement to export the getUserProfile function as a named export.
Supabase
Hint
Exporting the function allows other parts of your app to use it.
Practice
(1/5)
1. What is the main purpose of caching in Supabase applications?
easy
A. To store data temporarily and speed up data access
B. To permanently save all user data
C. To delete old data from the database
D. To encrypt data for security
Solution
Step 1: Understand caching basics
Caching temporarily stores data to avoid repeated database calls.
Step 2: Identify caching purpose in Supabase
It speeds up app performance by quickly providing stored data.
Final Answer:
To store data temporarily and speed up data access -> Option A
Quick Check:
Caching = Temporary storage for speed [OK]
Hint: Caching means temporary storage to speed up access [OK]
Common Mistakes:
Thinking caching permanently saves data
Confusing caching with encryption
Believing caching deletes data
2. Which of the following is the correct way to set a cache TTL (time-to-live) in Supabase client code?
easy
A. cache.set('key', value, { ttl: 300 })
B. cache.set('key', value, ttl=300)
C. cache.set('key', value, timeout: 300)
D. cache.set('key', value, expire: 300)
Solution
Step 1: Review Supabase cache TTL syntax
Supabase uses an options object with a ttl property to set time-to-live.
Step 2: Match correct syntax
cache.set('key', value, { ttl: 300 }) uses { ttl: 300 } which is the correct way to specify TTL in seconds.