#29340: "cache.get_or_set()" extra hits on database
-------------------------------------+-------------------------------------
     Reporter:  hematinik            |                    Owner:  nobody
         Type:                       |                   Status:  closed
  Cleanup/optimization               |
    Component:  Core (Cache system)  |                  Version:  2.0
     Severity:  Normal               |               Resolution:  invalid
     Keywords:  cache                |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by hematinik):

 Replying to [comment:4 Tim Graham]:
 > The extra queries are caused by accessing `event.location` which isn't
 cached. You could cache that as well using
 `Event.objects.select_related('location')`. Next time, please use
 [wiki:TicketClosingReasons/UseSupportChannels our support channels] if you
 need help confirming whether or not Django is at fault.

 Sir I believe you didn't get my point. this two block of code should work
 just alike while they don't when certain condition meets:

 {{{
 #!python
     def get_location(self,event):

         output = cache.get('event_location_eID{}'.format(event.pk))
         if output is None:
             output = event.location
             cache.set('event_location_eID{}'.format(event.pk),output,None)

         return output
 }}}
 V.S
 {{{
 #!python
     def get_location(self,event):

         output = cache.get_or_set(
             'event_location_eID{}'.format(event.pk),
             event.location,
             None)

         return output
 }}}

 I've already cached the values of **event.location** so {{{get_or_set()}}}
 shouldn't make any sql queries. both of the variations are working fine
 outside of the loop but they behave differently when used inside a
 {{{for}}} loop. In sorry for the inconvenience if I'm mistaken.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29340#comment:5>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/067.3c1818718293568e4990065791f0c5bc%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to