summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThibault Delavallée <tde@odoo.com>2018-04-05 14:48:19 +0200
committerThibault Delavallée <tde@odoo.com>2018-04-10 11:01:16 +0200
commit9f3889eba39fd39497208ec0a31fe8bc010a979a (patch)
tree5e4454fa0e26f5b38374a8c0ca75ad260f36eb59
parentbcc6884737a0e172fb436dfcc199e5e1155fdc60 (diff)
[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.
-rw-r--r--addons/mail/models/mail_message.py16
-rw-r--r--addons/test_mail/tests/test_performance.py10
2 files changed, 17 insertions, 9 deletions
diff --git a/addons/mail/models/mail_message.py b/addons/mail/models/mail_message.py
index e087d745861..740425bb75e 100644
--- a/addons/mail/models/mail_message.py
+++ b/addons/mail/models/mail_message.py
@@ -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
diff --git a/addons/test_mail/tests/test_performance.py b/addons/test_mail/tests/test_performance.py
index 91c6017ef4b..08871500892 100644
--- a/addons/test_mail/tests/test_performance.py
+++ b/addons/test_mail/tests/test_performance.py
@@ -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,