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