Performance: Cache framework configuration
This affects page load speed by reducing server processing time and database queries through caching.
Jump into concepts and practice - no test required
CACHES = {
'default': {
'BACKEND': 'django_redis.cache.RedisCache',
'LOCATION': 'redis://127.0.0.1:6379/1',
'OPTIONS': {
'CLIENT_CLASS': 'django_redis.client.DefaultClient'
}
}
}CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'unique-snowflake',
}
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Local memory cache (LocMemCache) | No impact | No impact | No impact | [X] Bad - limited to single process, poor scalability |
| Distributed cache (Redis/Memcached) | No impact | No impact | No impact | [OK] Good - shared cache reduces server load and speeds response |
CACHES setting in Django?CACHES configuresCACHES setting tells Django which backend to use and where to store cached data.CACHES setting?LocMemCache, which stores cache in local memory.BACKEND to django.core.cache.backends.locmem.LocMemCache without needing a LOCATION. CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.PyMemcacheCache',
'LOCATION': '127.0.0.1:11211',
}
}django.core.cache.backends.memcached.PyMemcacheCache, which uses Memcached with the PyMemcache client.127.0.0.1:11211, the default Memcached server address on localhost. CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': '/tmp/django_cache'
}
}LOCATION with a file path is invalid for LocMemCache and will cause an error.my_cache_table for caching. Which is the correct CACHES setting to achieve this?DatabaseCache.LOCATION must be the name of the database table, here my_cache_table.