[3.1.x] Refs #30181 -- Corrected note about storing None in the cache.

Backport of d23dad5778 from master
This commit is contained in:
Nick Pope 2020-12-17 07:26:30 +01:00 committed by Mariusz Felisiak
parent da42df9378
commit b044b417d2
1 changed files with 15 additions and 3 deletions

View File

@ -824,9 +824,21 @@ If the object doesn't exist in the cache, ``cache.get()`` returns ``None``::
>>> cache.get('my_key')
None
We advise against storing the literal value ``None`` in the cache, because you
won't be able to distinguish between your stored ``None`` value and a cache
miss signified by a return value of ``None``.
If you need to determine whether the object exists in the cache and you have
stored a literal value ``None``, use a sentinel object as the default::
>>> sentinel = object()
>>> cache.get('my_key', sentinel) is sentinel
False
>>> # Wait 30 seconds for 'my_key' to expire...
>>> cache.get('my_key', sentinel) is sentinel
True
.. admonition:: ``MemcachedCache``
Due to a ``python-memcached`` limitation, it's not possible to distinguish
between stored ``None`` value and a cache miss signified by a return value
of ``None`` on the deprecated ``MemcachedCache`` backend.
``cache.get()`` can take a ``default`` argument. This specifies which value to
return if the object doesn't exist in the cache::