0
0
GraphQLquery~30 mins

Cache management in GraphQL - Mini Project: Build & Apply

Choose your learning style9 modes available
Cache Management with GraphQL
📖 Scenario: You are building a simple GraphQL API for a bookstore. To improve performance, you want to manage a cache of book data so that repeated requests for the same book do not always hit the database.
🎯 Goal: Create a basic cache management system using a dictionary to store book data keyed by book ID. You will set up the initial cache, configure a cache size limit, implement logic to add books to the cache, and finally complete the cache management by removing the oldest cached book when the cache exceeds the limit.
📋 What You'll Learn
Create a dictionary called cache with initial book entries.
Add a variable called cache_limit to set the maximum number of books in the cache.
Write a function called add_to_cache that adds a book to the cache and removes the oldest book if the cache size exceeds cache_limit.
Complete the cache management by updating the cache dictionary correctly when adding new books.
💡 Why This Matters
🌍 Real World
Cache management is important in APIs and databases to speed up data retrieval and reduce load on the main database.
💼 Career
Understanding cache management helps backend developers optimize application performance and scalability.
Progress0 / 4 steps
1
Set up the initial cache dictionary
Create a dictionary called cache with these exact entries: "book1": "GraphQL Basics", "book2": "Advanced GraphQL", and "book3": "GraphQL in Practice".
GraphQL
Need a hint?

Use curly braces {} to create a dictionary and add the three key-value pairs exactly as shown.

2
Add a cache size limit variable
Add a variable called cache_limit and set it to 3 to limit the cache size to three books.
GraphQL
Need a hint?

Just create a variable named cache_limit and assign it the value 3.

3
Write the function to add books to the cache
Write a function called add_to_cache that takes book_id and book_title as parameters. It should add the book to the cache dictionary. If the cache size exceeds cache_limit, remove the oldest book (the first key in the dictionary).
GraphQL
Need a hint?

Use len(cache) to check size and next(iter(cache)) to get the oldest key.

4
Complete the cache management by updating the cache
Call the add_to_cache function to add a new book with book_id "book4" and book_title "Mastering GraphQL". This will update the cache dictionary and remove the oldest book if needed.
GraphQL
Need a hint?

Call the function with the exact arguments to add the new book.