Django

Django Caching

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

20 면접 질문·
Mid-Level
1

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

답변

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?

답변

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?

답변

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 면접 질문

다음 면접을 위해 Django을 마스터하세요

모든 질문, flashcards, 기술 테스트, 코드 리뷰 연습, 면접 시뮬레이터에 접근하세요.

무료로 시작하기