Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to set a key-value pair in Redis.
Redis
redis_client.[1]('user:1', 'Alice')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'get' instead of 'set' to store data.
Using 'expire' which sets a timeout, not the value.
✗ Incorrect
The set command stores a value for a key in Redis.
2fill in blank
mediumComplete the code to retrieve a value by key from Redis.
Redis
value = redis_client.[1]('user:1')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set' which writes data instead of reading.
Using 'del' which deletes keys.
✗ Incorrect
The get command fetches the value stored at a given key.
3fill in blank
hardFix the error in the code to set a key with expiration time.
Redis
redis_client.set('session:123', 'active', [1]=3600)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'expire' as a parameter in set instead of 'ex'.
Using 'ttl' which is a command to check time left.
✗ Incorrect
The ex parameter sets the expiration time in seconds when using set.
4fill in blank
hardFill both blanks to implement a cache check and set pattern.
Redis
value = redis_client.[1]('item:42') if value is None: value = fetch_from_db('item:42') redis_client.[2]('item:42', value, ex=300)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set' to read or 'get' to write.
Using 'expire' instead of 'set' to write data.
✗ Incorrect
First, get tries to read from cache. If missing, fetch from DB and set caches it with expiration.
5fill in blank
hardFill all three blanks to implement a cache invalidation after update.
Redis
redis_client.[1]('product:99') update_database('product:99', new_data) redis_client.[2]('product:99', new_data) redis_client.[3]('product:99', 600)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not deleting old cache before updating.
Using 'get' instead of 'delete' to remove cache.
✗ Incorrect
First, delete old cache with delete. Then update DB and cache with set. Finally, set expiration with expire.