Expiration-based caching in a REST API
📖 Scenario: You are building a simple REST API that returns data about products. To make your API faster and reduce repeated work, you want to add a cache that stores results temporarily. The cache should keep data only for a short time, then remove it automatically.
🎯 Goal: Build a basic expiration-based cache for your REST API. You will create a cache dictionary, set an expiration time, store data with timestamps, and return cached data only if it is still fresh.
📋 What You'll Learn
Create a cache dictionary to store product data and timestamps
Add a variable for cache expiration time in seconds
Write logic to check if cached data is still valid before returning it
Print the cached data or a message if cache expired
💡 Why This Matters
🌍 Real World
APIs often use caching to speed up responses and reduce repeated work. Expiration-based caching helps keep data fresh while saving resources.
💼 Career
Understanding caching is important for backend developers and API designers to build efficient and scalable web services.
Progress0 / 4 steps