##// END OF EJS Templates
Cache post's threads count, don't load all threads each time
neko259 -
r1531:2b982d03 default
parent child Browse files
Show More
@@ -2,6 +2,7 b' import logging'
2 2 import re
3 3 import uuid
4 4
5 from boards.utils import cached_result, datetime_to_epoch
5 6 from django.core.exceptions import ObjectDoesNotExist
6 7 from django.core.urlresolvers import reverse
7 8 from django.db import models
@@ -107,11 +108,6 b' class Post(models.Model, Viewable):'
107 108 def __str__(self):
108 109 return 'P#{}/{}'.format(self.id, self.get_title())
109 110
110 def get_referenced_posts(self):
111 threads = self.get_threads().all()
112 return self.referenced_posts.filter(threads__in=threads)\
113 .order_by('pub_time').distinct().all()
114
115 111 def get_title(self) -> str:
116 112 return self.title
117 113
@@ -176,6 +172,13 b' class Post(models.Model, Viewable):'
176 172
177 173 return self.threads
178 174
175 def _get_cache_key(self):
176 return [datetime_to_epoch(self.last_edit_time)]
177
178 @cached_result(key_method=_get_cache_key)
179 def get_thread_count(self):
180 return self.get_threads().count()
181
179 182 def get_view(self, *args, **kwargs) -> str:
180 183 """
181 184 Renders post's HTML view. Some of the post params can be passed over
@@ -312,7 +315,7 b' class Post(models.Model, Viewable):'
312 315 ID.
313 316 """
314 317
315 if self.get_threads().count() > 1:
318 if self.get_thread_count() > 1:
316 319 return '{}/{}'.format(self.get_thread().get_opening_post_id(), self.id)
317 320 else:
318 321 return str(self.id)
@@ -181,7 +181,7 b' class Thread(models.Model):'
181 181 """
182 182
183 183 query = self.multi_replies.order_by('pub_time').prefetch_related(
184 'images', 'thread', 'threads', 'attachments')
184 'images', 'thread', 'attachments')
185 185 if view_fields_only:
186 186 query = query.defer('poster_ip')
187 187 return query
General Comments 0
You need to be logged in to leave comments. Login now