0
0
Rest APIprogramming~10 mins

Expiration-based caching in Rest API - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Expiration-based caching
Request received
Check cache for data
Check if [Fetch fresh data
Store data in cache with expiration
Return cached data
Send response
When a request comes, the system checks the cache. If data is there and not expired, it returns cached data. Otherwise, it fetches fresh data, stores it with an expiration time, then returns it.
Execution Sample
Rest API
cache = {}

# On request
key = 'user_123'
now = 100

if key in cache and cache[key]['expiry'] > now:
    data = cache[key]['data']
else:
    data = 'fresh_data'
    cache[key] = {'data': data, 'expiry': now + 10}
This code checks if cached data for 'user_123' exists and is not expired at time 100; if expired or missing, it fetches fresh data and caches it with expiry 110.
Execution Table
StepActionCache StateConditionResult
1Check if 'user_123' in cache{}FalseGo to fetch fresh data
2Fetch fresh data{}N/Adata = 'fresh_data'
3Store data with expiry 110{'user_123': {'data': 'fresh_data', 'expiry': 110}}N/ACache updated
4Return data{'user_123': {'data': 'fresh_data', 'expiry': 110}}N/AReturn 'fresh_data'
5Next request at time 105{'user_123': {'data': 'fresh_data', 'expiry': 110}}'expiry' > now (110 > 105)Return cached data
6Next request at time 115{'user_123': {'data': 'fresh_data', 'expiry': 110}}'expiry' > now (110 > 115) is FalseFetch fresh data again
💡 Cache returns fresh data if expired or missing; otherwise returns cached data.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5After Step 6
cache{}{}{'user_123': {'data': 'fresh_data', 'expiry': 110}}{'user_123': {'data': 'fresh_data', 'expiry': 110}}{'user_123': {'data': 'fresh_data', 'expiry': 125}}
dataN/AN/A'fresh_data''fresh_data''fresh_data'
now100100100105115
Key Moments - 3 Insights
Why do we check if the cache entry's expiry time is greater than the current time?
Because even if data is in cache, it might be old. Checking expiry ensures we only use fresh cached data. See execution_table step 5 and 6 where expiry time decides if cache is valid.
What happens if the cache is empty when a request comes in?
The code fetches fresh data and stores it with an expiry time. This is shown in execution_table step 1 (cache miss) leading to step 2 and 3.
Why do we update the cache with a new expiry time after fetching fresh data?
To make sure future requests use this fresh data until it expires. Without updating expiry, cache would never refresh. See execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5. What is the condition checked to decide if cached data is used?
A'expiry' == now (110 == 105)
B'expiry' > now (110 > 105)
C'expiry' < now (110 < 105)
DCache is empty
💡 Hint
Check the 'Condition' column in execution_table row for step 5.
At which step does the cache get updated with fresh data and expiry?
AStep 3
BStep 1
CStep 5
DStep 6
💡 Hint
Look at the 'Action' and 'Cache State' columns in execution_table for when cache changes.
If the current time 'now' was 108 at step 5, what would happen?
AFresh data is fetched again
BCache is cleared
CCache data is returned
DError occurs
💡 Hint
Refer to variable_tracker for 'now' and cache expiry comparison logic.
Concept Snapshot
Expiration-based caching:
- Store data with an expiry timestamp
- On request, check if cached data exists and expiry > current time
- If valid, return cached data
- If expired or missing, fetch fresh data and update cache
- Helps reduce repeated data fetching and keeps data fresh
Full Transcript
Expiration-based caching means saving data temporarily with a time limit. When a request comes, the system looks in the cache. If data is there and not expired, it returns that data quickly. If data is missing or expired, it fetches new data, saves it with a new expiry time, and returns it. This way, the system avoids fetching data too often but still keeps it fresh. The example code shows checking the cache, comparing expiry times, and updating the cache when needed. The execution table walks through these steps, showing when cached data is used or refreshed.