[IMP] mail: limit cache invalidation when managing messages

This commit proposes to limit cache invalidation at some cases that will
trigger some behavior change when dealing with mail messages :

 * creating messages linked to a document;
 * update model or res_id of a message;
 * updating notifications, as notified people could change some computed
   fields on the record;

This commit also invalidates only mail-related fields as updating messages
should not invalidate other things than some computed fields linked to
mail.
This commit is contained in:
Thibault Delavallée
2018-04-10 11:01:16 +02:00
parent bcc6884737
commit 9f3889eba3
2 changed files with 17 additions and 9 deletions
+12 -4
View File
@@ -717,8 +717,14 @@ class Message(models.Model):
def _invalidate_documents(self):
""" Invalidate the cache of the documents followed by ``self``. """
for record in self:
if record.model and record.res_id:
self.env[record.model].invalidate_cache(ids=[record.res_id])
if record.model and record.res_id and 'message_ids' in self.env[record.model]:
self.env[record.model].invalidate_cache(fnames=[
'message_ids',
'message_unread',
'message_unread_counter',
'message_needaction',
'message_needaction_counter',
], ids=[record.res_id])
@api.model
def create(self, values):
@@ -764,7 +770,8 @@ class Message(models.Model):
if tracking_values_cmd:
message.sudo().write({'tracking_value_ids': tracking_values_cmd})
message._invalidate_documents()
if values.get('model') and values.get('res_id'):
message._invalidate_documents()
return message
@@ -780,7 +787,8 @@ class Message(models.Model):
if 'model' in vals or 'res_id' in vals:
self._invalidate_documents()
res = super(Message, self).write(vals)
self._invalidate_documents()
if 'notification_ids' in vals or 'model' in vals or 'res_id' in vals:
self._invalidate_documents()
return res
@api.multi
+5 -5
View File
@@ -77,7 +77,7 @@ class TestMailPerformance(TransactionCase):
'partner_id': self.env.ref('base.res_partner_12').id,
})
with self.assertQueryCount(admin=7, demo=7): # test_mail only: 7 - 7
with self.assertQueryCount(admin=6, demo=6): # test_mail only: 6 - 6
record.track = 'X'
@users('admin', 'demo')
@@ -93,7 +93,7 @@ class TestMailPerformance(TransactionCase):
@warmup
def test_create_mail_with_tracking(self):
""" Create records inheriting from 'mail.thread' (with field tracking). """
with self.assertQueryCount(admin=15, demo=15): # test_mail only: 15 - 15
with self.assertQueryCount(admin=14, demo=14): # test_mail only: 14 - 14
self.env['test_performance.mail'].create({'name': 'X'})
@users('admin', 'emp')
@@ -399,7 +399,7 @@ class TestHeavyMailPerformance(TransactionCase):
record = self.umbrella.sudo(self.env.user)
template_id = self.env.ref('test_mail.mail_test_tpl').id
with self.assertQueryCount(admin=136, emp=178): # com runbot 134 - 176 // test_mail only: 132 - 174
with self.assertQueryCount(admin=133, emp=174): # com runbot 131 - 172 // test_mail only: 129 - 170
record.message_post_with_template(template_id, message_type='comment', composition_mode='comment')
self.assertEqual(record.message_ids[0].body, '<p>Adding stuff on %s</p>' % record.name)
@@ -521,7 +521,7 @@ class TestHeavyMailPerformance(TransactionCase):
})
self.assertEqual(rec.message_partner_ids, self.user_portal.partner_id | self.env.user.partner_id)
with self.assertQueryCount(admin=147, emp=172): # test_mail only: 143 - 167
with self.assertQueryCount(admin=147, emp=172): # test_mail only: 142 - 167
rec.write({
'name': 'Test2',
'umbrella_id': self.umbrella.id,
@@ -593,7 +593,7 @@ class TestHeavyMailPerformance(TransactionCase):
})
self.assertEqual(rec.message_partner_ids, self.partners | self.env.user.partner_id | self.user_portal.partner_id)
with self.assertQueryCount(admin=60, emp=83): # test_mail only: 58 - 81
with self.assertQueryCount(admin=57, emp=78): # test_mail only: 55 - 76
rec.write({
'name': 'Test2',
'customer_id': customer_id,