Show More
@@ -4,7 +4,6 b' import logging' | |||
|
4 | 4 | import re |
|
5 | 5 | |
|
6 | 6 | from adjacent import Client |
|
7 | from django.core.cache import cache | |
|
8 | 7 | from django.core.urlresolvers import reverse |
|
9 | 8 | from django.db import models, transaction |
|
10 | 9 | from django.db.models import TextField |
@@ -16,7 +15,7 b' from boards.mdx_neboard import bbcode_ex' | |||
|
16 | 15 | from boards.models import PostImage |
|
17 | 16 | from boards.models.base import Viewable |
|
18 | 17 | from boards.models.thread import Thread |
|
19 | from boards.utils import datetime_to_epoch | |
|
18 | from boards.utils import datetime_to_epoch, cached_result | |
|
20 | 19 | |
|
21 | 20 | |
|
22 | 21 | WS_NOTIFICATION_TYPE_NEW_POST = 'new_post' |
@@ -26,9 +25,6 b' WS_CHANNEL_THREAD = "thread:"' | |||
|
26 | 25 | |
|
27 | 26 | APP_LABEL_BOARDS = 'boards' |
|
28 | 27 | |
|
29 | CACHE_KEY_PPD = 'ppd' | |
|
30 | CACHE_KEY_POST_URL = 'post_url' | |
|
31 | ||
|
32 | 28 | POSTS_PER_DAY_RANGE = 7 |
|
33 | 29 | |
|
34 | 30 | BAN_REASON_AUTO = 'Auto' |
@@ -157,6 +153,7 b' class PostManager(models.Manager):' | |||
|
157 | 153 | referenced_thread.last_edit_time = post.pub_time |
|
158 | 154 | referenced_thread.save(update_fields=['last_edit_time']) |
|
159 | 155 | |
|
156 | @cached_result | |
|
160 | 157 | def get_posts_per_day(self): |
|
161 | 158 | """ |
|
162 | 159 | Gets average count of posts per day for the last 7 days |
@@ -165,11 +162,6 b' class PostManager(models.Manager):' | |||
|
165 | 162 | day_end = date.today() |
|
166 | 163 | day_start = day_end - timedelta(POSTS_PER_DAY_RANGE) |
|
167 | 164 | |
|
168 | cache_key = CACHE_KEY_PPD + str(day_end) | |
|
169 | ppd = cache.get(cache_key) | |
|
170 | if ppd: | |
|
171 | return ppd | |
|
172 | ||
|
173 | 165 | day_time_start = timezone.make_aware(datetime.combine( |
|
174 | 166 | day_start, dtime()), timezone.get_current_timezone()) |
|
175 | 167 | day_time_end = timezone.make_aware(datetime.combine( |
@@ -181,7 +173,6 b' class PostManager(models.Manager):' | |||
|
181 | 173 | |
|
182 | 174 | ppd = posts_per_period / POSTS_PER_DAY_RANGE |
|
183 | 175 | |
|
184 | cache.set(cache_key, ppd) | |
|
185 | 176 | return ppd |
|
186 | 177 | |
|
187 | 178 | def _preparse_text(self, text): |
@@ -285,27 +276,22 b' class Post(models.Model, Viewable):' | |||
|
285 | 276 | thread.last_edit_time = edit_time |
|
286 | 277 | thread.save(update_fields=['last_edit_time']) |
|
287 | 278 | |
|
279 | @cached_result | |
|
288 | 280 | def get_url(self, thread=None): |
|
289 | 281 | """ |
|
290 | 282 | Gets full url to the post. |
|
291 | 283 | """ |
|
292 | 284 | |
|
293 | cache_key = CACHE_KEY_POST_URL + str(self.id) | |
|
294 | link = cache.get(cache_key) | |
|
285 | if not thread: | |
|
286 | thread = self.get_thread() | |
|
295 | 287 | |
|
296 | if not link: | |
|
297 | if not thread: | |
|
298 | thread = self.get_thread() | |
|
288 | opening_id = thread.get_opening_post_id() | |
|
299 | 289 | |
|
300 | opening_id = thread.get_opening_post_id() | |
|
301 | ||
|
302 | if self.id != opening_id: | |
|
303 | link = reverse('thread', kwargs={ | |
|
304 | 'post_id': opening_id}) + '#' + str(self.id) | |
|
305 | else: | |
|
306 | link = reverse('thread', kwargs={'post_id': self.id}) | |
|
307 | ||
|
308 | cache.set(cache_key, link) | |
|
290 | if self.id != opening_id: | |
|
291 | link = reverse('thread', kwargs={ | |
|
292 | 'post_id': opening_id}) + '#' + str(self.id) | |
|
293 | else: | |
|
294 | link = reverse('thread', kwargs={'post_id': self.id}) | |
|
309 | 295 | |
|
310 | 296 | return link |
|
311 | 297 |
@@ -1,9 +1,12 b'' | |||
|
1 | 1 | import logging |
|
2 | ||
|
2 | 3 | from django.db.models import Count, Sum |
|
3 | 4 | from django.utils import timezone |
|
4 | from django.core.cache import cache | |
|
5 | 5 | from django.db import models |
|
6 | ||
|
6 | 7 | from boards import settings |
|
8 | from boards.utils import cached_result | |
|
9 | ||
|
7 | 10 | |
|
8 | 11 | __author__ = 'neko259' |
|
9 | 12 | |
@@ -11,9 +14,6 b' from boards import settings' | |||
|
11 | 14 | logger = logging.getLogger(__name__) |
|
12 | 15 | |
|
13 | 16 | |
|
14 | CACHE_KEY_OPENING_POST = 'opening_post_id' | |
|
15 | ||
|
16 | ||
|
17 | 17 | class ThreadManager(models.Manager): |
|
18 | 18 | def process_oldest_threads(self): |
|
19 | 19 | """ |
@@ -153,18 +153,13 b' class Thread(models.Model):' | |||
|
153 | 153 | |
|
154 | 154 | return opening_post |
|
155 | 155 | |
|
156 | @cached_result | |
|
156 | 157 | def get_opening_post_id(self): |
|
157 | 158 | """ |
|
158 | 159 | Gets ID of the first thread post. |
|
159 | 160 | """ |
|
160 | 161 | |
|
161 | cache_key = CACHE_KEY_OPENING_POST + str(self.id) | |
|
162 | opening_post_id = cache.get(cache_key) | |
|
163 | if not opening_post_id: | |
|
164 | opening_post_id = self.get_opening_post(only_id=True).id | |
|
165 | cache.set(cache_key, opening_post_id) | |
|
166 | ||
|
167 | return opening_post_id | |
|
162 | return self.get_opening_post(only_id=True).id | |
|
168 | 163 | |
|
169 | 164 | def __unicode__(self): |
|
170 | 165 | return str(self.id) |
@@ -3,6 +3,8 b' This module contains helper functions an' | |||
|
3 | 3 | """ |
|
4 | 4 | import time |
|
5 | 5 | import hmac |
|
6 | from django.core.cache import cache | |
|
7 | from django.db.models import Model | |
|
6 | 8 | |
|
7 | 9 | from django.utils import timezone |
|
8 | 10 | |
@@ -40,4 +42,27 b" def get_websocket_token(user_id='', time" | |||
|
40 | 42 | sign.update(timestamp.encode()) |
|
41 | 43 | token = sign.hexdigest() |
|
42 | 44 | |
|
43 | return token No newline at end of file | |
|
45 | return token | |
|
46 | ||
|
47 | ||
|
48 | def cached_result(function): | |
|
49 | """ | |
|
50 | Caches method result in the Django's cache system, persisted by object name, | |
|
51 | object name and model id if object is a Django model. | |
|
52 | """ | |
|
53 | def inner_func(obj, *args, **kwargs): | |
|
54 | # TODO Include method arguments to the cache key | |
|
55 | cache_key = obj.__class__.__name__ + '_' + function.__name__ | |
|
56 | if isinstance(obj, Model): | |
|
57 | cache_key += '_' + str(obj.id) | |
|
58 | ||
|
59 | persisted_result = cache.get(cache_key) | |
|
60 | if persisted_result: | |
|
61 | result = persisted_result | |
|
62 | else: | |
|
63 | result = function(obj, *args, **kwargs) | |
|
64 | cache.set(cache_key, result) | |
|
65 | ||
|
66 | return result | |
|
67 | ||
|
68 | return inner_func No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now