diff --git a/boards/models/post.py b/boards/models/post.py --- a/boards/models/post.py +++ b/boards/models/post.py @@ -3,6 +3,7 @@ from random import random import time import math import re +from django.core.cache import cache from django.db import models from django.http import Http404 @@ -10,6 +11,7 @@ from django.utils import timezone from markupfield.fields import MarkupField from neboard import settings +from boards import settings as boards_settings from boards import thumbs BAN_REASON_AUTO = 'Auto' @@ -66,10 +68,6 @@ class PostManager(models.Manager): thread.bump() thread.last_edit_time = posting_time thread.save() - - #cache_key = thread.get_cache_key() - #cache.delete(cache_key) - else: self._delete_old_threads() @@ -84,12 +82,12 @@ class PostManager(models.Manager): # Update thread's last edit time (used as cache key) thread = post.thread if thread: + thread.clear_cache() + thread.last_edit_time = timezone.now() thread.save() - #cache_key = thread.get_cache_key() - #cache.delete(cache_key) - + post.clear_cache() post.delete() def delete_posts_by_ip(self, ip): @@ -125,16 +123,16 @@ class PostManager(models.Manager): except Post.DoesNotExist: raise Http404 - #cache_key = opening_post.get_cache_key() - #thread = cache.get(cache_key) - #if thread: - # return thread + cache_key = opening_post.get_cache_key() + thread = cache.get(cache_key) + if thread: + return thread if opening_post.replies: thread = [opening_post] thread.extend(opening_post.replies.all().order_by('pub_time')) - #cache.set(cache_key, thread, board_settings.CACHE_TIMEOUT) + cache.set(cache_key, thread, boards_settings.CACHE_TIMEOUT) return thread @@ -260,12 +258,20 @@ class Post(models.Model): return post_count <= settings.MAX_POSTS_PER_THREAD + def clear_cache(self): + """Remove the post from cache""" + + cache_key = self.get_cache_key() + cache.delete(cache_key) + def bump(self): """Bump (move to up) thread""" if self.can_bump(): self.bump_time = timezone.now() + self.clear_cache() + def get_last_replies(self): if settings.LAST_REPLIES_COUNT > 0: reply_count = self.get_reply_count() @@ -284,12 +290,10 @@ class Post(models.Model): return self.tags.order_by('name') def get_cache_key(self): - return str(self.id) + str(self.last_edit_time.microsecond) + return 'thread_cache' + str(self.id) def get_sorted_referenced_posts(self): return self.referenced_posts.order_by('id') def is_referenced(self): return self.referenced_posts.count() > 0 - -