cache.get_or_set : function name is misleading

25 views
Skip to first unread message

Florian Iragne

unread,
Aug 31, 2016, 5:54:21 AM8/31/16
to django...@googlegroups.com
Hi,

I wanted to refactor my code using cache.get_or_set instead of using
cache.get and then check for None and so on

However, after a few tests, it seems that get_or_set does not work as
its name suggests: it is more get_or_add than get_or_set

Looking at the source code, it indeed does a get, then a add, not a set.

To my mind, the function name is misleading, even if after a careful
reading of the doc, it explains that the value is added (not set) if not
in the cache.

I haven't found anything about this, so i may be the only one astonished
by the difference between function name and function main behaviour.
However, i think it's useful to live a comment about this for other devs.

Cheers

Florian

Tim Graham

unread,
Aug 31, 2016, 7:18:42 AM8/31/16
to Django users
You're welcome to submit a documentation patch to help clarify.

Daniel França

unread,
Aug 31, 2016, 8:19:39 AM8/31/16
to Django users
But it's okay, isn't it?
I mean, add is used to add the key if the key doesn't exist.
set on the other hand, will set the value anyway.

In the case of get_or_set the set should work like an add because it'll be called only in case the key doesn't exist.

Or am I missing something?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5b164103-dbeb-44c1-a014-24a31c1a5fcd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

florian

unread,
Aug 31, 2016, 9:36:48 AM8/31/16
to Django users


Le mercredi 31 août 2016 14:19:39 UTC+2, daniel.franca a écrit :
But it's okay, isn't it?
I mean, add is used to add the key if the key doesn't exist.
set on the other hand, will set the value anyway.

In the case of get_or_set the set should work like an add because it'll be called only in case the key doesn't exist.

Or am I missing something?

You're right on the expected behaviour. My point is that the function should have been named get_or_add since it is self explanatory. Reading the name get_or_set, i expect the function to first perform a get and then a set if the key is not there.

I'll try to see if i can submit something interesting for the doc.

thanks
Reply all
Reply to author
Forward
0 new messages