Complete the code to define the cache key for storing user data.
cache_key = "user_data_[1]"
The cache key should uniquely identify the user data, so using user_id is correct.
Complete the code to check if the cache has expired before fetching fresh data.
if cache_timestamp [1] current_time:
We check if the cache timestamp is less than the current time to know if it expired.
Fix the error in the code to acquire a lock before refreshing the cache.
if cache_miss: if cache_lock.[1](): refresh_cache()
To prevent multiple processes from refreshing the cache simultaneously, we must acquire a lock first.
Fill both blanks to implement cache stampede prevention using a lock and cache update.
if cache_expired: if cache_lock.[1](): data = fetch_fresh_data() cache_store.[2](cache_key, data)
First, we acquire the lock to prevent others from refreshing simultaneously. Then, we set the fresh data in the cache.
Fill all three blanks to implement a cache with expiration, lock, and fallback to stale data.
if cache_data is None or cache_timestamp [1] current_time: if cache_lock.[2](): fresh_data = fetch_data() cache_store.[3](cache_key, fresh_data) else: use_stale_cache()
We check if cache expired with '<'. Then we acquire the lock to refresh cache. Finally, we set fresh data in cache. If lock not acquired, fallback to stale cache.