Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to fetch data from cache or load from database if missing.
Redis
value = redis_client.get([1]) if value is None: value = db_load(key) redis_client.set(key, value)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string literal instead of the variable key.
✗ Incorrect
The cache key variable should be passed to get to fetch the cached value.
2fill in blank
mediumComplete the code to set the cache expiration time after loading from database.
Redis
if value is None: value = db_load(key) redis_client.set(key, value, [1]=3600)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect parameter names like 'timeout' or 'ttl'.
✗ Incorrect
Redis set method uses ex to set expiration time in seconds.
3fill in blank
hardFix the error in the cache-aside pattern code to avoid race conditions.
Redis
value = redis_client.get(key) if value is None: [1] redis_client.set(key, value, ex=3600)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to get from cache again inside the miss block.
✗ Incorrect
When cache miss happens, load data from the database to avoid race conditions.
4fill in blank
hardFill both blanks to implement cache-aside with explicit cache invalidation.
Redis
def update_data(key, new_value): db_update(key, new_value) redis_client.[1](key) redis_client.[2](key, new_value, ex=3600)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
del which is not a Redis client method.✗ Incorrect
Use delete to remove old cache, then set to update cache with new value.
5fill in blank
hardFill all three blanks to implement cache-aside with fallback and expiration.
Redis
def get_data(key): value = redis_client.[1](key) if value is None: value = db_load([2]) redis_client.[3](key, value, ex=3600) return value
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong method names like 'load' or wrong variable names.
✗ Incorrect
Use get to fetch from cache, key to load from DB, and set to cache the value.