Show More
@@ -3,6 +3,7 b' from random import random' | |||||
3 | import time |
|
3 | import time | |
4 | import math |
|
4 | import math | |
5 | import re |
|
5 | import re | |
|
6 | from django.core.cache import cache | |||
6 |
|
7 | |||
7 | from django.db import models |
|
8 | from django.db import models | |
8 | from django.http import Http404 |
|
9 | from django.http import Http404 | |
@@ -10,6 +11,7 b' from django.utils import timezone' | |||||
10 | from markupfield.fields import MarkupField |
|
11 | from markupfield.fields import MarkupField | |
11 |
|
12 | |||
12 | from neboard import settings |
|
13 | from neboard import settings | |
|
14 | from boards import settings as boards_settings | |||
13 | from boards import thumbs |
|
15 | from boards import thumbs | |
14 |
|
16 | |||
15 | BAN_REASON_AUTO = 'Auto' |
|
17 | BAN_REASON_AUTO = 'Auto' | |
@@ -66,10 +68,6 b' class PostManager(models.Manager):' | |||||
66 | thread.bump() |
|
68 | thread.bump() | |
67 | thread.last_edit_time = posting_time |
|
69 | thread.last_edit_time = posting_time | |
68 | thread.save() |
|
70 | thread.save() | |
69 |
|
||||
70 | #cache_key = thread.get_cache_key() |
|
|||
71 | #cache.delete(cache_key) |
|
|||
72 |
|
||||
73 | else: |
|
71 | else: | |
74 | self._delete_old_threads() |
|
72 | self._delete_old_threads() | |
75 |
|
73 | |||
@@ -84,12 +82,12 b' class PostManager(models.Manager):' | |||||
84 | # Update thread's last edit time (used as cache key) |
|
82 | # Update thread's last edit time (used as cache key) | |
85 | thread = post.thread |
|
83 | thread = post.thread | |
86 | if thread: |
|
84 | if thread: | |
|
85 | thread.clear_cache() | |||
|
86 | ||||
87 | thread.last_edit_time = timezone.now() |
|
87 | thread.last_edit_time = timezone.now() | |
88 | thread.save() |
|
88 | thread.save() | |
89 |
|
89 | |||
90 | #cache_key = thread.get_cache_key() |
|
90 | post.clear_cache() | |
91 | #cache.delete(cache_key) |
|
|||
92 |
|
||||
93 | post.delete() |
|
91 | post.delete() | |
94 |
|
92 | |||
95 | def delete_posts_by_ip(self, ip): |
|
93 | def delete_posts_by_ip(self, ip): | |
@@ -125,16 +123,16 b' class PostManager(models.Manager):' | |||||
125 | except Post.DoesNotExist: |
|
123 | except Post.DoesNotExist: | |
126 | raise Http404 |
|
124 | raise Http404 | |
127 |
|
125 | |||
128 |
|
|
126 | cache_key = opening_post.get_cache_key() | |
129 |
|
|
127 | thread = cache.get(cache_key) | |
130 |
|
|
128 | if thread: | |
131 |
|
|
129 | return thread | |
132 |
|
130 | |||
133 | if opening_post.replies: |
|
131 | if opening_post.replies: | |
134 | thread = [opening_post] |
|
132 | thread = [opening_post] | |
135 | thread.extend(opening_post.replies.all().order_by('pub_time')) |
|
133 | thread.extend(opening_post.replies.all().order_by('pub_time')) | |
136 |
|
134 | |||
137 |
|
|
135 | cache.set(cache_key, thread, boards_settings.CACHE_TIMEOUT) | |
138 |
|
136 | |||
139 | return thread |
|
137 | return thread | |
140 |
|
138 | |||
@@ -260,12 +258,20 b' class Post(models.Model):' | |||||
260 |
|
258 | |||
261 | return post_count <= settings.MAX_POSTS_PER_THREAD |
|
259 | return post_count <= settings.MAX_POSTS_PER_THREAD | |
262 |
|
260 | |||
|
261 | def clear_cache(self): | |||
|
262 | """Remove the post from cache""" | |||
|
263 | ||||
|
264 | cache_key = self.get_cache_key() | |||
|
265 | cache.delete(cache_key) | |||
|
266 | ||||
263 | def bump(self): |
|
267 | def bump(self): | |
264 | """Bump (move to up) thread""" |
|
268 | """Bump (move to up) thread""" | |
265 |
|
269 | |||
266 | if self.can_bump(): |
|
270 | if self.can_bump(): | |
267 | self.bump_time = timezone.now() |
|
271 | self.bump_time = timezone.now() | |
268 |
|
272 | |||
|
273 | self.clear_cache() | |||
|
274 | ||||
269 | def get_last_replies(self): |
|
275 | def get_last_replies(self): | |
270 | if settings.LAST_REPLIES_COUNT > 0: |
|
276 | if settings.LAST_REPLIES_COUNT > 0: | |
271 | reply_count = self.get_reply_count() |
|
277 | reply_count = self.get_reply_count() | |
@@ -284,12 +290,10 b' class Post(models.Model):' | |||||
284 | return self.tags.order_by('name') |
|
290 | return self.tags.order_by('name') | |
285 |
|
291 | |||
286 | def get_cache_key(self): |
|
292 | def get_cache_key(self): | |
287 | return str(self.id) + str(self.last_edit_time.microsecond) |
|
293 | return 'thread_cache' + str(self.id) | |
288 |
|
294 | |||
289 | def get_sorted_referenced_posts(self): |
|
295 | def get_sorted_referenced_posts(self): | |
290 | return self.referenced_posts.order_by('id') |
|
296 | return self.referenced_posts.order_by('id') | |
291 |
|
297 | |||
292 | def is_referenced(self): |
|
298 | def is_referenced(self): | |
293 | return self.referenced_posts.count() > 0 |
|
299 | return self.referenced_posts.count() > 0 | |
294 |
|
||||
295 |
|
General Comments 0
You need to be logged in to leave comments.
Login now