##// END OF EJS Templates
Use cache for PPD value
neko259 -
r410:cf63309c default
parent child Browse files
Show More
@@ -5,6 +5,7 b' from random import random'
5 import time
5 import time
6 import math
6 import math
7 import re
7 import re
8 from django.core.cache import cache
8
9
9 from django.db import models
10 from django.db import models
10 from django.http import Http404
11 from django.http import Http404
@@ -14,6 +15,10 b' from markupfield.fields import MarkupFie'
14 from neboard import settings
15 from neboard import settings
15 from boards import thumbs
16 from boards import thumbs
16
17
18 APP_LABEL_BOARDS = 'boards'
19
20 CACHE_KEY_PPD = 'ppd'
21
17 POSTS_PER_DAY_RANGE = range(7)
22 POSTS_PER_DAY_RANGE = range(7)
18
23
19 BAN_REASON_AUTO = 'Auto'
24 BAN_REASON_AUTO = 'Auto'
@@ -40,6 +45,8 b' class PostManager(models.Manager):'
40
45
41 def create_post(self, title, text, image=None, thread=None,
46 def create_post(self, title, text, image=None, thread=None,
42 ip=NO_IP, tags=None, user=None):
47 ip=NO_IP, tags=None, user=None):
48 cache.delete(CACHE_KEY_PPD)
49
43 posting_time = timezone.now()
50 posting_time = timezone.now()
44 if not thread:
51 if not thread:
45 thread = Thread.objects.create(bump_time=posting_time,
52 thread = Thread.objects.create(bump_time=posting_time,
@@ -157,6 +164,10 b' class PostManager(models.Manager):'
157 def get_posts_per_day(self):
164 def get_posts_per_day(self):
158 """Get count of posts for the current day"""
165 """Get count of posts for the current day"""
159
166
167 ppd = cache.get(CACHE_KEY_PPD)
168 if ppd:
169 return ppd
170
160 today = datetime.now().date()
171 today = datetime.now().date()
161
172
162 posts_per_days = []
173 posts_per_days = []
@@ -166,11 +177,14 b' class PostManager(models.Manager):'
166 day_time_start = datetime.combine(day_start, dtime())
177 day_time_start = datetime.combine(day_start, dtime())
167 day_time_end = datetime.combine(day_end, dtime())
178 day_time_end = datetime.combine(day_end, dtime())
168
179
169 posts_per_days.append(float(self.filter(pub_time__lte=day_time_end,
180 posts_per_days.append(float(self.filter(
181 pub_time__lte=day_time_end,
170 pub_time__gte=day_time_start).count()))
182 pub_time__gte=day_time_start).count()))
171
183
172 return sum(posts_per_day for posts_per_day in posts_per_days) / \
184 ppd = (sum(posts_per_day for posts_per_day in posts_per_days) /
173 len(posts_per_days)
185 len(posts_per_days))
186 cache.set(CACHE_KEY_PPD, ppd)
187 return ppd
174
188
175
189
176 class Post(models.Model):
190 class Post(models.Model):
@@ -179,7 +193,7 b' class Post(models.Model):'
179 objects = PostManager()
193 objects = PostManager()
180
194
181 class Meta:
195 class Meta:
182 app_label = 'boards'
196 app_label = APP_LABEL_BOARDS
183
197
184 def _update_image_filename(self, filename):
198 def _update_image_filename(self, filename):
185 """Get unique image filename"""
199 """Get unique image filename"""
@@ -241,7 +255,7 b' class Post(models.Model):'
241 class Thread(models.Model):
255 class Thread(models.Model):
242
256
243 class Meta:
257 class Meta:
244 app_label = 'boards'
258 app_label = APP_LABEL_BOARDS
245
259
246 tags = models.ManyToManyField('Tag')
260 tags = models.ManyToManyField('Tag')
247 bump_time = models.DateTimeField()
261 bump_time = models.DateTimeField()
General Comments 0
You need to be logged in to leave comments. Login now