django.core.cache is Django's built-in caching framework, which provides a standardized way to cache data within a Django application.
This video shows an easy implementation of django.core.cache with database request.
This video shows an easy implementation of django.core.cache with database request.
Category
🦄
CreativityTranscript
00:00Every time a user accesses this page, posts are requested from the server.
00:12To load them faster, I'm using cache.
00:17In this video, I'll explain how.
00:21In the backend, I have created for Django a file called cache.
00:33In this file, I have created a CacheManager class to manage the cache for posts.
00:47When I create an instance for this class CacheManager,
00:57I'm providing the user ID and the page number to create a unique cache for each request.
01:12Because I need to save in cache only the user posts and only posts for a page.
01:27I'm creating a unique cache key based on user ID and page number.
01:39Next, I'm requesting the posts from the database.
01:47If posts exist, I'm saving them in a cache using this code.
01:58When the user accesses the page where are displayed the posts,
02:12I'm verifying first if the cache exists for that user and the page number.
02:23If cache exists, this means I will return this cache.
02:33In this way, I will avoid to make all these verifications and request the data from the database.
02:53In this way, I will avoid to make all these verifications and request the data from the database.
03:03In this way, I will avoid to make all these verifications and request the data from the database.