#28268: Feature: Clear cached_property on related DB operations.
-------------------------------------+-------------------------------------
     Reporter:  Özer Sahin           |                    Owner:  nobody
         Type:  New feature          |                   Status:  new
    Component:  Database layer       |                  Version:  1.11
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  cached_property      |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Özer Sahin):

 Here is a quick example to try to illustrate:

 {{{
 class ModelA(models.Model):

     @cached_property
     def related_count(self):
         return self.related.count()

     @cached_property
     def related_content(self):
         return ', '.join([rel.title for rel in self.related.all()])


 class Related(models.Model):

     model = models.ForeignKey(ModelA, related_name='related')
     title = models.CharField()


 >>> model = ModelA()
 >>> rel = Related()
 >>> rel.model = model
 >>> rel.title = "Text"
 >>> rel.save()

 >>> model.related_count()
 1  # calls first time

 >>> model.related_content()
 Text  # calls first time

 >>> rel.title = "New Text"
 >>> rel.save()
 >>> model.related_content()
 New Text  # calls, because rel changed

 >>> model.related_count()
 1  # does not call, because nothing was added

 >>> rel.delete()
 >>> model.related_count()
 0  # calls, because related model removed
 }}}


 So what I am actually looking for, are some kind of signals to attach to
 the decorator so we can input logic, when the cached_property should be
 removed so it calls it again for an updated value. We could define that it
 should delete the cached value once related objects were updated, deleted
 or created. That way we can max out the cache, but keep it always up to
 date. I think it could be a huge performance booster without the issue of
 manually deleting cached_properties so they update.

 I hope I am not too far off, awaiting your feedback!

--
Ticket URL: <https://code.djangoproject.com/ticket/28268#comment:2>
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/063.778431f9f5f92ca193ec78a53026946f%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to