0
0
Rest APIprogramming~10 mins

Cache invalidation strategies in Rest API - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set the cache expiration time to 60 seconds.

Rest API
cache.set('user_data', data, timeout=[1])
Drag options to blanks, or click blank then click option'
A600
B60
C6
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Setting timeout to 0 disables caching.
Using too large timeout causes stale data.
2fill in blank
medium

Complete the code to invalidate the cache for the key 'user_data'.

Rest API
cache.[1]('user_data')
Drag options to blanks, or click blank then click option'
Aupdate
Brefresh
Cdelete
Dset
Attempts:
3 left
💡 Hint
Common Mistakes
Using set instead of delete.
Using update which modifies but does not remove.
3fill in blank
hard

Fix the error in the code to invalidate cache after updating user info.

Rest API
def update_user(user_id, new_info):
    save_to_db(user_id, new_info)
    cache.[1](f'user_{user_id}')
Drag options to blanks, or click blank then click option'
Adelete
Bget
Cset
Dadd
Attempts:
3 left
💡 Hint
Common Mistakes
Using set which overwrites cache without new data.
Using get which only reads cache.
4fill in blank
hard

Fill both blanks to create a cache key with user ID and set cache with 120 seconds timeout.

Rest API
cache_key = f'user_[1]'
cache.set(cache_key, user_data, timeout=[2])
Drag options to blanks, or click blank then click option'
Aid
B120
Cuser_id
D60
Attempts:
3 left
💡 Hint
Common Mistakes
Using id instead of user_id which may not be defined.
Setting timeout to 60 instead of 120.
5fill in blank
hard

Fill all three blanks to implement cache invalidation after updating user profile and then set new cache.

Rest API
def update_profile(user_id, profile):
    db.update(user_id, profile)
    cache.[1](f'user_[2]')
    cache.set(f'user_[3]', profile, timeout=300)
Drag options to blanks, or click blank then click option'
Adelete
Buser_id
Cid
Dset
Attempts:
3 left
💡 Hint
Common Mistakes
Using set instead of delete to remove old cache.
Mixing id and user_id causing key mismatch.