mirror of https://github.com/django/django.git
Fixed #29864 -- Added link targets for low-level cache API.
This commit is contained in:
parent
26bb2611a5
commit
8250538bfc
|
@ -115,8 +115,8 @@ Cache
|
||||||
* The :ref:`local-memory cache backend <local-memory-caching>` now uses a
|
* The :ref:`local-memory cache backend <local-memory-caching>` now uses a
|
||||||
least-recently-used (LRU) culling strategy rather than a pseudo-random one.
|
least-recently-used (LRU) culling strategy rather than a pseudo-random one.
|
||||||
|
|
||||||
* The new ``touch()`` method of the :ref:`low-level cache API
|
* The new :meth:`~django.core.caches.cache.touch` method of the :ref:`low-level
|
||||||
<low-level-cache-api>` updates the timeout of cache keys.
|
cache API <low-level-cache-api>` updates the timeout of cache keys.
|
||||||
|
|
||||||
CSRF
|
CSRF
|
||||||
~~~~
|
~~~~
|
||||||
|
|
|
@ -788,9 +788,16 @@ Accessing the cache
|
||||||
Basic usage
|
Basic usage
|
||||||
-----------
|
-----------
|
||||||
|
|
||||||
The basic interface is ``set(key, value, timeout)`` and ``get(key)``::
|
.. currentmodule:: django.core.caches
|
||||||
|
|
||||||
|
The basic interface is:
|
||||||
|
|
||||||
|
.. method:: cache.set(key, value, timeout=DEFAULT_TIMEOUT, version=None)
|
||||||
|
|
||||||
>>> cache.set('my_key', 'hello, world!', 30)
|
>>> cache.set('my_key', 'hello, world!', 30)
|
||||||
|
|
||||||
|
.. method:: cache.get(key, default=None, version=None)
|
||||||
|
|
||||||
>>> cache.get('my_key')
|
>>> cache.get('my_key')
|
||||||
'hello, world!'
|
'hello, world!'
|
||||||
|
|
||||||
|
@ -818,6 +825,8 @@ return if the object doesn't exist in the cache::
|
||||||
>>> cache.get('my_key', 'has expired')
|
>>> cache.get('my_key', 'has expired')
|
||||||
'has expired'
|
'has expired'
|
||||||
|
|
||||||
|
.. method:: cache.add(key, value, timeout=DEFAULT_TIMEOUT, version=None)
|
||||||
|
|
||||||
To add a key only if it doesn't already exist, use the ``add()`` method.
|
To add a key only if it doesn't already exist, use the ``add()`` method.
|
||||||
It takes the same parameters as ``set()``, but it will not attempt to
|
It takes the same parameters as ``set()``, but it will not attempt to
|
||||||
update the cache if the key specified is already present::
|
update the cache if the key specified is already present::
|
||||||
|
@ -831,6 +840,8 @@ If you need to know whether ``add()`` stored a value in the cache, you can
|
||||||
check the return value. It will return ``True`` if the value was stored,
|
check the return value. It will return ``True`` if the value was stored,
|
||||||
``False`` otherwise.
|
``False`` otherwise.
|
||||||
|
|
||||||
|
.. method:: cache.get_or_set(key, default, timeout=DEFAULT_TIMEOUT, version=None)
|
||||||
|
|
||||||
If you want to get a key's value or set a value if the key isn't in the cache,
|
If you want to get a key's value or set a value if the key isn't in the cache,
|
||||||
there is the ``get_or_set()`` method. It takes the same parameters as ``get()``
|
there is the ``get_or_set()`` method. It takes the same parameters as ``get()``
|
||||||
but the default is set as the new cache value for that key, rather than simply
|
but the default is set as the new cache value for that key, rather than simply
|
||||||
|
@ -846,6 +857,8 @@ You can also pass any callable as a *default* value::
|
||||||
>>> cache.get_or_set('some-timestamp-key', datetime.datetime.now)
|
>>> cache.get_or_set('some-timestamp-key', datetime.datetime.now)
|
||||||
datetime.datetime(2014, 12, 11, 0, 15, 49, 457920)
|
datetime.datetime(2014, 12, 11, 0, 15, 49, 457920)
|
||||||
|
|
||||||
|
.. method:: cache.get_many(keys, version=None)
|
||||||
|
|
||||||
There's also a ``get_many()`` interface that only hits the cache once.
|
There's also a ``get_many()`` interface that only hits the cache once.
|
||||||
``get_many()`` returns a dictionary with all the keys you asked for that
|
``get_many()`` returns a dictionary with all the keys you asked for that
|
||||||
actually exist in the cache (and haven't expired)::
|
actually exist in the cache (and haven't expired)::
|
||||||
|
@ -856,6 +869,8 @@ actually exist in the cache (and haven't expired)::
|
||||||
>>> cache.get_many(['a', 'b', 'c'])
|
>>> cache.get_many(['a', 'b', 'c'])
|
||||||
{'a': 1, 'b': 2, 'c': 3}
|
{'a': 1, 'b': 2, 'c': 3}
|
||||||
|
|
||||||
|
.. method:: cache.set_many(dict, timeout)
|
||||||
|
|
||||||
To set multiple values more efficiently, use ``set_many()`` to pass a dictionary
|
To set multiple values more efficiently, use ``set_many()`` to pass a dictionary
|
||||||
of key-value pairs::
|
of key-value pairs::
|
||||||
|
|
||||||
|
@ -868,22 +883,32 @@ Like ``cache.set()``, ``set_many()`` takes an optional ``timeout`` parameter.
|
||||||
On supported backends (memcached), ``set_many()`` returns a list of keys that
|
On supported backends (memcached), ``set_many()`` returns a list of keys that
|
||||||
failed to be inserted.
|
failed to be inserted.
|
||||||
|
|
||||||
|
.. method:: cache.delete(key, version=None)
|
||||||
|
|
||||||
You can delete keys explicitly with ``delete()``. This is an easy way of
|
You can delete keys explicitly with ``delete()``. This is an easy way of
|
||||||
clearing the cache for a particular object::
|
clearing the cache for a particular object::
|
||||||
|
|
||||||
>>> cache.delete('a')
|
>>> cache.delete('a')
|
||||||
|
|
||||||
|
.. method:: cache.delete_many(keys, version=None)
|
||||||
|
|
||||||
If you want to clear a bunch of keys at once, ``delete_many()`` can take a list
|
If you want to clear a bunch of keys at once, ``delete_many()`` can take a list
|
||||||
of keys to be cleared::
|
of keys to be cleared::
|
||||||
|
|
||||||
>>> cache.delete_many(['a', 'b', 'c'])
|
>>> cache.delete_many(['a', 'b', 'c'])
|
||||||
|
|
||||||
|
.. method:: cache.clear()
|
||||||
|
|
||||||
Finally, if you want to delete all the keys in the cache, use
|
Finally, if you want to delete all the keys in the cache, use
|
||||||
``cache.clear()``. Be careful with this; ``clear()`` will remove *everything*
|
``cache.clear()``. Be careful with this; ``clear()`` will remove *everything*
|
||||||
from the cache, not just the keys set by your application. ::
|
from the cache, not just the keys set by your application. ::
|
||||||
|
|
||||||
>>> cache.clear()
|
>>> cache.clear()
|
||||||
|
|
||||||
|
.. method:: cache.touch(key, timeout=DEFAULT_TIMEOUT, version=None)
|
||||||
|
|
||||||
|
.. versionadded:: 2.1
|
||||||
|
|
||||||
``cache.touch()`` sets a new expiration for a key. For example, to update a key
|
``cache.touch()`` sets a new expiration for a key. For example, to update a key
|
||||||
to expire 10 seconds from now::
|
to expire 10 seconds from now::
|
||||||
|
|
||||||
|
@ -896,9 +921,8 @@ Like other methods, the ``timeout`` argument is optional and defaults to the
|
||||||
``touch()`` returns ``True`` if the key was successfully touched, ``False``
|
``touch()`` returns ``True`` if the key was successfully touched, ``False``
|
||||||
otherwise.
|
otherwise.
|
||||||
|
|
||||||
.. versionchanged:: 2.1
|
.. method:: cache.incr(key, delta=1, version=None)
|
||||||
|
.. method:: cache.decr(key, delta=1, version=None)
|
||||||
The ``cache.touch()`` method was added.
|
|
||||||
|
|
||||||
You can also increment or decrement a key that already exists using the
|
You can also increment or decrement a key that already exists using the
|
||||||
``incr()`` or ``decr()`` methods, respectively. By default, the existing cache
|
``incr()`` or ``decr()`` methods, respectively. By default, the existing cache
|
||||||
|
@ -925,6 +949,7 @@ nonexistent cache key.::
|
||||||
However, if the backend doesn't natively provide an increment/decrement
|
However, if the backend doesn't natively provide an increment/decrement
|
||||||
operation, it will be implemented using a two-step retrieve/update.
|
operation, it will be implemented using a two-step retrieve/update.
|
||||||
|
|
||||||
|
.. method:: cache.close()
|
||||||
|
|
||||||
You can close the connection to your cache with ``close()`` if implemented by
|
You can close the connection to your cache with ``close()`` if implemented by
|
||||||
the cache backend.
|
the cache backend.
|
||||||
|
|
Loading…
Reference in New Issue