Performance: Cache invalidation strategies
HIGH IMPACT
This affects page load speed by controlling how fresh cached content is served and how often the server recomputes or fetches data.
from django.core.cache import cache def get_data(): data = cache.get('my_data') if not data: data = expensive_db_query() cache.set('my_data', data) return data def update_data(new_data): save_to_db(new_data) cache.delete('my_data') # invalidate cache immediately
from django.core.cache import cache def get_data(): data = cache.get('my_data') if not data: data = expensive_db_query() cache.set('my_data', data, timeout=3600) # fixed 1 hour timeout return data
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Fixed timeout cache invalidation | N/A | N/A | May delay fresh content load | [X] Bad |
| Manual cache key deletion | N/A | N/A | Multiple cache calls increase server load | [!] OK |
| Batch cache key deletion (delete_many) | N/A | N/A | Fewer cache calls, consistent invalidation | [OK] Good |
| Cache versioning | N/A | N/A | Automatic fresh content with minimal overhead | [OK] Good |