Empty collections are valid cache data
When using Django's cache, ensure that empty collections ([], (), {}) are
treated as valid cache data.
cached = cache.get(cache_key) if cached: return cached # perform expensive operation
In the above snippet, if the call to get returns an empty collection the
cached result is ignored and the value is recalculated unnecessarily.
Avoid this by explicitly comparing the return value to None:
cached = cache.get(cache_key) if cached is not None: # much better! return cached
Django's documentation wisely advises against caching the literal value None,
and the above snippet makes it clear why this is good advice – the get method
returns None when the cache does not contain an entry for the supplied key.
Possibly related posts
- Testing Django apps using localhost subdomains
- Get attributes of Django model or instance
- Serializing Django model instances
- Freeing myself of WordPress
- Django syntax highlighting for Coda