0
0
FastAPIframework~10 mins

Caching strategies in FastAPI - Interactive Code Practice

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

Complete 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'
ACacheManager
BCacheControl
CCache
DFastAPICache
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong class name like Cache or CacheControl.
Trying to import from fastapi instead of fastapi_cache.
2fill in blank
medium

Complete 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'
AInMemoryBackend
BMemoryCache
CInMemoryCache
DMemoryBackend
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'MemoryCache' or 'InMemoryCache' which do not exist.
Confusing backend with cache class names.
3fill in blank
hard

Fix 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'
A'60'
B6000
C60
D'sixty'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the number as a string like '60'.
Using a wrong number like 6000 or a word.
4fill in blank
hard

Fill 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'
Ainit
Bstart
C"fastapi-cache"
D"cache"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'start' instead of 'init'.
Using a prefix with quotes missing or wrong string.
5fill in blank
hard

Fill 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'
Acache
B120
C['data1', 'data2']
Dcached
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong decorator name like 'cached'.
Passing expiration as string instead of number.
Returning a wrong data type.