Concept Flow - Caching strategies for cost reduction
Request comes in
Check cache for data
v
Return cached
Return data to user
This flow shows how a request first checks the cache to avoid extra API calls, reducing cost by reusing stored data.
cache = {}
def get_data(key):
if key in cache:
return cache[key]
result = fetch_api(key)
cache[key] = result
return result| Step | Action | Key | Cache Before | Cache After | Returned Value |
|---|---|---|---|---|---|
| 1 | Check cache | user123 | {} | {} | - |
| 2 | Cache miss, fetch API | user123 | {} | {} | - |
| 3 | Store in cache | user123 | {} | {'user123': 'data1'} | - |
| 4 | Return data | user123 | {'user123': 'data1'} | {'user123': 'data1'} | data1 |
| 5 | Check cache | user123 | {'user123': 'data1'} | {'user123': 'data1'} | - |
| 6 | Cache hit, return cached | user123 | {'user123': 'data1'} | {'user123': 'data1'} | data1 |
| Variable | Start | After Step 3 | After Step 6 |
|---|---|---|---|
| cache | {} | {'user123': 'data1'} | {'user123': 'data1'} |
| key | - | user123 | user123 |
| returned_value | - | - | data1 |
Caching Strategy for Cost Reduction: - Check cache before API call - If cache miss, fetch and store result - Return cached data on hits - Saves cost by reducing API calls - Simple dict cache example shown