#7967 closed (fixed)
cache.has_key() doesn't do the same thing as "key in cache"
| Reported by: | Marty Alchin | Owned by: | Marty Alchin |
|---|---|---|---|
| Component: | Core (Other) | Version: | dev |
| Severity: | Keywords: | ||
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
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
Attachments (1)
Change History (4)
by , 17 years ago
| Attachment: | 7967-r8075.diff added |
|---|
comment:1 by , 17 years ago
For what it's worth, I tested with with locmem, but since key in cache always calls BaseCache.__contains__, it actually doesn't matter what backend is used. There's also probably a case to be made that BaseCache.has_key() itself is buggy, since it could specify a default instead of assuming the default will always be None, but since all the backends override it, it's not a concern unless a custom backend (which is only recently possible) doesn't supply its own has_key().
comment:2 by , 17 years ago
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
Fixed
__contains__to always callhas_key()and added tests to verify they work the same way.