cache.has_key() doesn't do the same thing as "key in cache"
|Reported by:||Marty Alchin||Owned by:||Marty Alchin|
|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