Merge pull request #209 from liangliangyy/dev

缓存功能优化
sh_branch
且听风吟 7 years ago committed by GitHub
commit eee552dd56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -22,7 +22,7 @@ from django.core.mail import EmailMultiAlternatives
from django.db.models.signals import post_save
from django.contrib.auth.signals import user_logged_in, user_logged_out, user_login_failed
from DjangoBlog.utils import cache, send_email, expire_view_cache, get_blog_setting, delete_view_cache
from DjangoBlog.utils import cache, send_email, expire_view_cache, delete_sidebar_cache, delete_view_cache
from DjangoBlog.spider_notify import SpiderNotify
from oauth.models import OAuthUser
from blog.models import Article, Category, Tag, Links, SideBar, BlogSettings
@ -103,7 +103,8 @@ def model_post_save_callback(sender, instance, created, raw, using, update_field
cache.delete('seo_processor')
comment_cache_key = 'article_comments_{id}'.format(id=instance.article.id)
cache.delete(comment_cache_key)
delete_view_cache(instance.author.username)
delete_sidebar_cache(instance.author.username)
delete_view_cache('article_comments', [str(instance.article.pk)])
_thread.start_new(send_comment_email, (instance,))
@ -116,5 +117,5 @@ def model_post_save_callback(sender, instance, created, raw, using, update_field
def user_auth_callback(sender, request, user, **kwargs):
if user and user.username:
logger.info(user)
delete_view_cache(user.username)
delete_sidebar_cache(user.username)
cache.clear()

@ -42,7 +42,6 @@ def get_md5(str):
def cache_decorator(expiration=3 * 60):
def wrapper(func):
def news(*args, **kwargs):
key = ''
try:
view = args[0]
key = view.get_cache_key()
@ -57,11 +56,17 @@ def cache_decorator(expiration=3 * 60):
value = cache.get(key)
if value:
# logger.info('cache_decorator get cache:%s key:%s' % (func.__name__, key))
return value
if repr(value) == 'default':
return None
else:
return value
else:
logger.info('cache_decorator set cache:%s key:%s' % (func.__name__, key))
value = func(*args, **kwargs)
cache.set(key, value, expiration)
if not value:
cache.set(key, 'default', expiration)
else:
cache.set(key, value, expiration)
return value
return news
@ -246,10 +251,16 @@ def save_user_avatar(url):
return url
def delete_view_cache(username):
def delete_sidebar_cache(username):
from django.core.cache.utils import make_template_fragment_key
from blog.models import LINK_SHOW_TYPE
keys = (make_template_fragment_key('sidebar', [username + x[0]]) for x in LINK_SHOW_TYPE)
for k in keys:
logger.info('delete sidebar key:' + k)
cache.delete(k)
def delete_view_cache(prefix, keys):
from django.core.cache.utils import make_template_fragment_key
key = make_template_fragment_key(prefix, keys)
cache.delete(key)

@ -1,7 +1,7 @@
from django.test import Client, RequestFactory, TestCase
from blog.models import Article, Category, Tag
from django.contrib.auth import get_user_model
from DjangoBlog.utils import get_current_site
from DjangoBlog.utils import delete_view_cache, delete_sidebar_cache
import datetime
from accounts.models import BlogUser
from django.urls import reverse
@ -59,7 +59,7 @@ class AccountTest(TestCase):
user.is_superuser = True
user.is_staff = True
user.save()
delete_view_cache(user.username)
delete_sidebar_cache(user.username)
category = Category()
category.name = "categoryaaa"
category.created_time = datetime.datetime.now()

@ -117,7 +117,7 @@ class Article(BaseModel):
return value
else:
comments = self.comment_set.filter(is_enable=True)
cache.set(cache_key, comments)
cache.set(cache_key, comments, 60 * 100)
logger.info('set article comments:{id}'.format(id=self.id))
return comments
@ -125,12 +125,12 @@ class Article(BaseModel):
info = (self._meta.app_label, self._meta.model_name)
return reverse('admin:%s_%s_change' % info, args=(self.pk,))
@cached_property
@cache_decorator(expiration=60 * 100)
def next_article(self):
# 下一篇
return Article.objects.filter(id__gt=self.id, status='p').order_by('id').first()
@cached_property
@cache_decorator(expiration=60 * 100)
def prev_article(self):
# 前一篇
return Article.objects.filter(id__lt=self.id, status='p').first()

@ -87,6 +87,7 @@ class IndexView(ArticleListView):
'''
首页
'''
# 友情链接类型
link_type = 'i'
def get_queryset_data(self):

@ -20,7 +20,7 @@ import requests
import json
import logging
import urllib.parse
from DjangoBlog.utils import parse_dict_to_url
from DjangoBlog.utils import parse_dict_to_url, cache_decorator
logger = logging.getLogger(__name__)
@ -426,6 +426,7 @@ class QQOauthManager(BaseOauthManager):
return user
@cache_decorator(expiration=100 * 60)
def get_oauth_apps():
configs = OAuthConfig.objects.filter(is_enable=True).all()
if not configs:
@ -438,7 +439,8 @@ def get_oauth_apps():
def get_manager_by_type(type):
applications = get_oauth_apps()
finds = list(filter(lambda x: x.ICON_NAME.lower() == type.lower(), applications))
if finds:
return finds[0]
if applications:
finds = list(filter(lambda x: x.ICON_NAME.lower() == type.lower(), applications))
if finds:
return finds[0]
return None

@ -23,12 +23,15 @@ register = template.Library()
@register.inclusion_tag('oauth/oauth_applications.html')
def load_oauth_applications(request):
applications = get_oauth_apps()
baseurl = reverse('oauth:oauthlogin')
path = request.get_full_path()
if applications:
baseurl = reverse('oauth:oauthlogin')
path = request.get_full_path()
apps = list(map(lambda x: (x.ICON_NAME,
'{baseurl}?type={type}&next_url={next}'
.format(baseurl=baseurl, type=x.ICON_NAME, next=path)), applications))
apps = list(map(lambda x: (x.ICON_NAME,
'{baseurl}?type={type}&next_url={next}'
.format(baseurl=baseurl, type=x.ICON_NAME, next=path)), applications))
else:
apps = []
return {
'apps': apps
}

@ -51,8 +51,7 @@
</div><!-- #content -->
{% if article.comment_status == "o" and OPEN_SITE_COMMENT %}
{% comment %}{% load comments_tags %}
{% load_post_comment article from %}{% endcomment %}
{% include 'comments/tags/comment_list.html' %}
{% if user.is_authenticated %}
{% include 'comments/tags/post_comment.html' %}

@ -1,4 +1,5 @@
{% load blog_tags %}
{% load cache %}
<article id="post-{{ article.pk }} "
class="post-{{ article.pk }} post type-post status-publish format-standard hentry">
<header class="entry-header">
@ -31,7 +32,9 @@
<br/>
{% if article.type == 'a' %}
{% if not isindex %}
{% load_breadcrumb article %}
{% cache 36000 breadcrumb article.pk %}
{% load_breadcrumb article %}
{% endcache %}
{% endif %}
{% endif %}
</header><!-- .entry-header -->

@ -1,29 +1,33 @@
{% load blog_tags %}
<footer class="entry-meta">
本条目发布于<a href="{{ article.get_absolute_url }}" title="{% datetimeformat article.created_time %}"
itemprop="datePublished" content="{% datetimeformat article.created_time %}"
rel="bookmark">
{% load cache %}
{% with article.id|add:user.is_authenticated as cachekey %}
{% cache 36000 metainfo cachekey %}
<footer class="entry-meta">
本条目发布于<a href="{{ article.get_absolute_url }}" title="{% datetimeformat article.created_time %}"
itemprop="datePublished" content="{% datetimeformat article.created_time %}"
rel="bookmark">
<time class="entry-date updated"
datetime="{{ article.created_time }}">
{% datetimeformat article.created_time %}</time>
</a>
{% if article.type == 'a' %}
属于<a href="{{ article.category.get_absolute_url }}" rel="category tag">{{ article.category.name }}</a>分类
{% if article.tags.all %}
被贴了
<time class="entry-date updated"
datetime="{{ article.created_time }}">
{% datetimeformat article.created_time %}</time>
</a>
{% if article.type == 'a' %}
属于<a href="{{ article.category.get_absolute_url }}" rel="category tag">{{ article.category.name }}</a>
分类
{% if article.tags.all %}
被贴了
{% for t in article.tags.all %}
<a href="{{ t.get_absolute_url }}" rel="tag">{{ t.name }}</a>
{% if t != article.tags.all.last %}
{% endif %}
{% endfor %}
{% for t in article.tags.all %}
<a href="{{ t.get_absolute_url }}" rel="tag">{{ t.name }}</a>
{% if t != article.tags.all.last %}
{% endif %}
{% endfor %}
标签
{% endif %}
{% endif %}
<span class="by-author">作者是
标签
{% endif %}
{% endif %}
<span class="by-author">作者是
<span class="author vcard">
<a class="url fn n" href="{{ article.author.get_absolute_url }}"
title="查看所有由{{ article.author.username }}发布的文章"
@ -37,8 +41,11 @@
</span>
</a>
</span>
{% if user.is_authenticated %}
<a href="{{ article.get_admin_url }}">编辑</a>
{% endif %}
{% if user.is_authenticated %}
<a href="{{ article.get_admin_url }}">编辑</a>
{% endif %}
</span>
</footer><!-- .entry-meta -->
</footer><!-- .entry-meta -->
{% endcache %}
{% endwith %}

@ -1,38 +1,39 @@
<section id="comments" class="themeform">
{% load blog_tags %}
{% load comments_tags %}
{% comment %}<h3 class="heading">{{ comment_count }} 条回复</h3>{% endcomment %}
{% load cache %}
<ul class="comment-tabs group">
<li class="active"><a href="#commentlist-container"><i
class="fa fa-comments-o"></i>评论<span>{{ comment_count }}</span></a></li>
{% comment %}<li class=""><a href="#pinglist-container"><i class="fa fa-share"></i>引用<span>0</span></a></li>{% endcomment %}
</ul>
{% if article_comments %}
<div id="commentlist-container" class="comment-tab" style="display: block;">
<ol class="commentlist">
{% query article_comments parent_comment=None as parent_comments %}
{% for comment in parent_comments %}
{% show_comment_item comment False %}
{% cache 36000 article_comments article.id %}
<div id="commentlist-container" class="comment-tab" style="display: block;">
{% parse_commenttree article_comments comment as childcomments %}
{% if childcomments %}
<ul class="children">
{% for child in childcomments %}
{% show_comment_item child True %}
<ol class="commentlist">
{% query article_comments parent_comment=None as parent_comments %}
{% for comment in parent_comments %}
{% show_comment_item comment False %}
{% endfor %}
</ul><!-- .children -->
{% endif %}
{% parse_commenttree article_comments comment as childcomments %}
{% if childcomments %}
<ul class="children">
{% for child in childcomments %}
{% show_comment_item child True %}
{% endfor %}
</ul><!-- .children -->
{% endif %}
{% endfor %}
</ol><!--/.commentlist-->
{% endfor %}
</ol><!--/.commentlist-->
</div>
</div>
{% endcache %}
{% endif %}
</section>

@ -7,18 +7,11 @@
</h3>
<form action="{% url 'comments:postcomment' article.pk %}" method="post" id="commentform"
class="comment-form">{% csrf_token %}
{% comment %}<p class="comment-notes"><span id="email-notes">电子邮件地址不会被公开</span> 必填项已用<span class="required">*</span>标注
</p>{% endcomment %}
<p class="comment-form-comment">
{{ form.body.label_tag }}
{{ form.body }}
{{ form.body.errors }}
{% comment %}<label for="comment">评论</label> <textarea id="comment" name="comment"
cols="45" rows="8"
maxlength="65525"
aria-required="true"
required="required"></textarea>{% endcomment %}
</p>
<p class="comment-form-author">
{% if not form.name.is_hidden %}
@ -26,26 +19,13 @@
{% endif %}
{{ form.name }}
{{ form.name.errors }}
{% comment %}<label for="author">姓名 <span class="required">*</span></label>
<input id="author" name="author" type="text" value="" size="30" maxlength="245" aria-required='true'
required='required'/></p>{% endcomment %}
<p class="comment-form-email">
{% comment %} <label for="email">电子邮件 <span class="required">*</span></label> <input
id="email" name="email" type="text" value="" size="30" maxlength="100"
aria-describedby="email-notes" aria-required='true' required='required'/>{% endcomment %}
{% if not form.email.is_hidden %}
{{ form.email.label_tag }}
{% endif %}
{{ form.email }}
{{ form.email.errors }}
</p>
{% comment %}<p class="comment-form-url">
{{ form.url.label_tag }}
{{ form.url }}
{{ form.url.errors }}
</p>{% endcomment %}
{{ form.parent_comment_id }}
<div class="form-submit">
<span class="comment-markdown"> 支持markdown</span>

Loading…
Cancel
Save