From 34fb909180e9df06fa6a993dd5696a49cd152a0b Mon Sep 17 00:00:00 2001 From: Berker Peksag Date: Sun, 23 Nov 2014 20:33:07 +0200 Subject: Fixed #12982 -- Added a get_or_set() method to the BaseCache backend. --- docs/releases/1.9.txt | 3 ++- docs/topics/cache.txt | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) (limited to 'docs') diff --git a/docs/releases/1.9.txt b/docs/releases/1.9.txt index e8dd955e29..1be30ae561 100644 --- a/docs/releases/1.9.txt +++ b/docs/releases/1.9.txt @@ -94,7 +94,8 @@ Minor features Cache ^^^^^ -* ... +* ``django.core.cache.backends.base.BaseCache`` now has a ``get_or_set()`` + method. Email ^^^^^ diff --git a/docs/topics/cache.txt b/docs/topics/cache.txt index 94a97d5b0e..69656534e6 100644 --- a/docs/topics/cache.txt +++ b/docs/topics/cache.txt @@ -778,6 +778,25 @@ 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, ``False`` otherwise. +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()`` +but the default is set as the new cache value for that key, rather than simply +returned:: + + >>> cache.get('my_new_key') # returns None + >>> cache.get_or_set('my_new_key', 'my new value', 100) + 'my new value' + +You can also pass any callable as a *default* value:: + + >>> import datetime + >>> cache.get_or_set('some-timestamp-key', datetime.datetime.now) + datetime.datetime(2014, 12, 11, 0, 15, 49, 457920) + +.. versionchanged:: 1.9 + + The ``get_or_set()`` method was added. + 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 actually exist in the cache (and haven't expired):: -- cgit v1.3