Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the FastAPI cache dependency.
FastAPI
from fastapi import FastAPI from fastapi_cache import [1] app = FastAPI()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong class name like Cache or CacheControl.
Trying to import from fastapi instead of fastapi_cache.
✗ Incorrect
The correct import for FastAPI caching is FastAPICache from fastapi_cache.
2fill in blank
mediumComplete the code to set up an in-memory cache backend.
FastAPI
from fastapi_cache.backends.inmemory import [1] cache_backend = [1]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'MemoryCache' or 'InMemoryCache' which do not exist.
Confusing backend with cache class names.
✗ Incorrect
The in-memory backend class is called InMemoryBackend.
3fill in blank
hardFix the error in the cache decorator to set expiration time to 60 seconds.
FastAPI
@app.get('/items') @cache(expire=[1]) async def get_items(): return ['apple', 'banana']
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the number as a string like '60'.
Using a wrong number like 6000 or a word.
✗ Incorrect
The expire parameter expects an integer number of seconds, so 60 without quotes is correct.
4fill in blank
hardFill both blanks to initialize the cache with the backend and FastAPI app.
FastAPI
from fastapi_cache import FastAPICache FastAPICache.[1](cache_backend, prefix=[2])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'start' instead of 'init'.
Using a prefix with quotes missing or wrong string.
✗ Incorrect
Use init to initialize the cache and set a prefix string like "cache".
5fill in blank
hardFill all three blanks to create a cached endpoint with a 120-second expiration.
FastAPI
@app.get('/data') @FastAPICache.[1](expire=[2]) async def get_data(): return [3]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong decorator name like 'cached'.
Passing expiration as string instead of number.
Returning a wrong data type.
✗ Incorrect
The decorator is cache, expiration is 120 seconds, and the function returns a list of data.