cache.has_key() doesn't do the same thing as "key in cache"
|Reported by:||Gulopine||Owned by:||Gulopine|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
In [source:django/trunk/django/core/cache/backends/base.py django.core.cache.backends.base.BaseCache], has_key() is defined as a method, while __contains__ is simply an alias for it. Individual backends override has_key() to be more efficient, but __contains__ is left alone, so it's still an alias for the base function, which unfortunately means that the two end up with different implementations. This is generally fine, but shows itself when None is stored in the cache, which is a potentially useful situation.
>>> from django.core.cache import cache >>> cache.set('test', None) >>> cache.has_key('test') True >>> 'test' in cache False
Change History (4)
Changed 7 years ago by Gulopine
comment:1 Changed 7 years ago by Gulopine
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
comment:2 Changed 7 years ago by mtredinnick
- Resolution set to fixed
- Status changed from new to closed