diff options
author | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2012-02-27 01:31:04 -0300 |
---|---|---|
committer | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2012-02-27 01:31:04 -0300 |
commit | 3cf65e8aa33271e06d6e9d9f564f5595dcbd6fc4 (patch) | |
tree | fbf4cd55246a6a72cc73e12dca77970531dd5379 | |
parent | dd7373e74f2097868eb2125e1a19a04f5bfb9002 (diff) | |
download | askbot-3cf65e8aa33271e06d6e9d9f564f5595dcbd6fc4.tar.gz askbot-3cf65e8aa33271e06d6e9d9f564f5595dcbd6fc4.tar.bz2 askbot-3cf65e8aa33271e06d6e9d9f564f5595dcbd6fc4.zip |
cached answer list query which saves about 15% on the question page generation
-rw-r--r-- | askbot/const/__init__.py | 2 | ||||
-rw-r--r-- | askbot/models/question.py | 50 | ||||
-rw-r--r-- | askbot/views/readers.py | 10 |
3 files changed, 32 insertions, 30 deletions
diff --git a/askbot/const/__init__.py b/askbot/const/__init__.py index 6fef38d7..ddbff836 100644 --- a/askbot/const/__init__.py +++ b/askbot/const/__init__.py @@ -18,6 +18,8 @@ CLOSE_REASONS = ( (9, _('too localized')), ) +LONG_TIME = 60*60*24*30 #30 days is a lot of time + TYPE_REPUTATION = ( (1, 'gain_by_upvoted'), (2, 'gain_by_answer_accepted'), diff --git a/askbot/models/question.py b/askbot/models/question.py index 2bb4d6a0..65dd6db0 100644 --- a/askbot/models/question.py +++ b/askbot/models/question.py @@ -296,6 +296,7 @@ class ThreadManager(models.Manager): class Thread(models.Model): SUMMARY_CACHE_KEY_TPL = 'thread-question-summary-%d' + ANSWER_LIST_KEY_TPL = 'thread-answer-list-%d' title = models.CharField(max_length=300) @@ -420,6 +421,25 @@ class Thread(models.Model): | models.Q(deleted_by = user) ) + def get_cached_answer_list(self, sort_method = None): + """get all answer posts as a list for the Thread, and a given + user. This list is cached.""" + key = self.ANSWER_LIST_KEY_TPL % self.id + answer_list = cache.cache.get(key) + if not answer_list: + answers = self.get_answers() + answers = answers.select_related('thread', 'author', 'last_edited_by') + answers = answers.order_by( + { + "latest":"-added_at", + "oldest":"added_at", + "votes":"-score" + }[sort_method] + ) + answer_list = list(answers) + cache.cache.set(key, answer_list) + return answer_list + def get_similarity(self, other_thread = None): """return number of tags in the other question @@ -659,36 +679,16 @@ class Thread(models.Model): # * We probably don't need to pollute the cache with threads older than 30 days # * Additionally, Memcached treats timeouts > 30day as dates (https://code.djangoproject.com/browser/django/tags/releases/1.3/django/core/cache/backends/memcached.py#L36), # which probably doesn't break anything but if we can stick to 30 days then let's stick to it - cache.cache.set(self.SUMMARY_CACHE_KEY_TPL % self.id, html, timeout=60*60*24*30) + cache.cache.set( + self.SUMMARY_CACHE_KEY_TPL % self.id, + html, + timeout=const.LONG_TIME + ) return html def summary_html_cached(self): return cache.cache.has_key(self.SUMMARY_CACHE_KEY_TPL % self.id) - -#class Question(content.Content): -# post_type = 'question' -# thread = models.ForeignKey('Thread', unique=True, related_name='questions') -# -# objects = QuestionManager() -# -# class Meta(content.Content.Meta): -# db_table = u'question' -# -# TODO: Add sphinx_search() to Post model -# -#if getattr(settings, 'USE_SPHINX_SEARCH', False): -# from djangosphinx.models import SphinxSearch -# Question.add_to_class( -# 'sphinx_search', -# SphinxSearch( -# index = settings.ASKBOT_SPHINX_SEARCH_INDEX, -# mode = 'SPH_MATCH_ALL' -# ) -# ) - - - class QuestionView(models.Model): question = models.ForeignKey(Post, related_name='viewed') who = models.ForeignKey(User, related_name='question_views') diff --git a/askbot/views/readers.py b/askbot/views/readers.py index c14d8008..8dd49a0b 100644 --- a/askbot/views/readers.py +++ b/askbot/views/readers.py @@ -307,6 +307,7 @@ def question(request, id):#refactor - long subroutine. display question body, an """ #process url parameters #todo: fix inheritance of sort method from questions + before = datetime.datetime.now() default_sort_method = request.session.get('questions_sort_method', 'votes') form = ShowQuestionForm(request.GET, default_sort_method) form.full_clean()#always valid @@ -422,10 +423,7 @@ def question(request, id):#refactor - long subroutine. display question body, an logging.debug('answer_sort_method=' + unicode(answer_sort_method)) #load answers - answers = thread.get_answers(user = request.user) - answers = answers.select_related('thread', 'author', 'last_edited_by') - answers = answers.order_by({"latest":"-added_at", "oldest":"added_at", "votes":"-score" }[answer_sort_method]) - answers = list(answers) + answers = thread.get_cached_answer_list(sort_method = answer_sort_method) # TODO: Add unit test to catch the bug where precache_comments() is called above (before) reordering the accepted answer to the top #Post.objects.precache_comments(for_posts=[question_post] + answers, visitor=request.user) @@ -531,7 +529,9 @@ def question(request, id):#refactor - long subroutine. display question body, an 'show_comment_position': show_comment_position, } - return render_into_skin('question.html', data, request) + result = render_into_skin('question.html', data, request) + print datetime.datetime.now() - before + return result def revisions(request, id, object_name=None): if object_name == 'Question': |