Django

Django Caching

Cache backends, per-view cache, template fragment cache, low-level cache API, cache invalidation

20 câu hỏi phỏng vấn·
Mid-Level
1

What is the default cache backend in Django if no configuration is specified?

Câu trả lời

Django uses the 'LocMemCache' backend (local memory cache) by default which stores data in the Python process memory. This backend is simple and fast but doesn't persist between server restarts and doesn't share data across different processes. It's suitable for development but not recommended for multi-process production environments.

2

Which decorator should be used to cache the complete response of a view for 15 minutes?

Câu trả lời

The cache_page decorator from django.views.decorators.cache caches the complete HTTP response of a view. The parameter is the time in seconds (15 minutes = 900 seconds). The cache key is automatically generated from the URL and query parameters. This decorator also handles appropriate HTTP headers for caching.

3

What is the correct syntax for the template tag to cache a template fragment for 5 minutes?

Câu trả lời

The {% cache %} tag caches a template fragment. The syntax is {% cache timeout cache_name %}...{% endcache %}. The first argument is the time in seconds (300 for 5 minutes), the second is a unique name to identify this fragment. Additional arguments can be added to vary the cache (e.g., user.id).

4

How to retrieve a value from cache with a default value if the key doesn't exist?

5

Which cache backend is recommended for a Django application in production with multiple instances?

+17 câu hỏi phỏng vấn

Nắm vững Django cho lần phỏng vấn tiếp theo

Truy cập tất cả câu hỏi, flashcards, bài kiểm tra kỹ thuật, bài tập code review và mô phỏng phỏng vấn.

Bắt đầu miễn phí