diff --git a/DjangoBlog/blog_signals.py b/DjangoBlog/blog_signals.py index 1abe76b..edc0ea9 100644 --- a/DjangoBlog/blog_signals.py +++ b/DjangoBlog/blog_signals.py @@ -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() diff --git a/DjangoBlog/utils.py b/DjangoBlog/utils.py index 619db63..75555c7 100644 --- a/DjangoBlog/utils.py +++ b/DjangoBlog/utils.py @@ -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) diff --git a/accounts/tests.py b/accounts/tests.py index 7fdeaba..0d196d6 100644 --- a/accounts/tests.py +++ b/accounts/tests.py @@ -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() diff --git a/blog/models.py b/blog/models.py index a7e981d..36032b4 100644 --- a/blog/models.py +++ b/blog/models.py @@ -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() diff --git a/blog/views.py b/blog/views.py index b4205a9..b572278 100644 --- a/blog/views.py +++ b/blog/views.py @@ -87,6 +87,7 @@ class IndexView(ArticleListView): ''' 首页 ''' + # 友情链接类型 link_type = 'i' def get_queryset_data(self): diff --git a/oauth/oauthmanager.py b/oauth/oauthmanager.py index 3d25235..5d7521a 100644 --- a/oauth/oauthmanager.py +++ b/oauth/oauthmanager.py @@ -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 diff --git a/oauth/templatetags/oauth_tags.py b/oauth/templatetags/oauth_tags.py index 042fee0..8ab09d3 100644 --- a/oauth/templatetags/oauth_tags.py +++ b/oauth/templatetags/oauth_tags.py @@ -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 } diff --git a/templates/blog/article_detail.html b/templates/blog/article_detail.html index 2931b2d..f404410 100755 --- a/templates/blog/article_detail.html +++ b/templates/blog/article_detail.html @@ -51,8 +51,7 @@ {% 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' %} diff --git a/templates/blog/tags/article_info.html b/templates/blog/tags/article_info.html index 50ff92c..4f47c20 100644 --- a/templates/blog/tags/article_info.html +++ b/templates/blog/tags/article_info.html @@ -1,4 +1,5 @@ {% load blog_tags %} +{% load cache %}
@@ -31,7 +32,9 @@
{% if article.type == 'a' %} {% if not isindex %} - {% load_breadcrumb article %} + {% cache 36000 breadcrumb article.pk %} + {% load_breadcrumb article %} + {% endcache %} {% endif %} {% endif %}
diff --git a/templates/blog/tags/article_meta_info.html b/templates/blog/tags/article_meta_info.html index 92e53b4..4ea8173 100644 --- a/templates/blog/tags/article_meta_info.html +++ b/templates/blog/tags/article_meta_info.html @@ -1,29 +1,33 @@ {% load blog_tags %} - + + {% endcache %} +{% endwith %} \ No newline at end of file diff --git a/templates/comments/tags/comment_list.html b/templates/comments/tags/comment_list.html index d3dab08..44a15a8 100644 --- a/templates/comments/tags/comment_list.html +++ b/templates/comments/tags/comment_list.html @@ -1,38 +1,39 @@
{% load blog_tags %} {% load comments_tags %} - {% comment %}

{{ comment_count }} 条回复

{% endcomment %} + {% load cache %} {% if article_comments %} -
- -
    - {% 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 %} +
    - {% parse_commenttree article_comments comment as childcomments %} - {% if childcomments %} -
      - {% for child in childcomments %} - {% show_comment_item child True %} +
        + {% query article_comments parent_comment=None as parent_comments %} + {% for comment in parent_comments %} + {% show_comment_item comment False %} - {% endfor %} -
    - {% endif %} + {% parse_commenttree article_comments comment as childcomments %} + {% if childcomments %} +
      + {% for child in childcomments %} + {% show_comment_item child True %} + {% endfor %} +
    + {% endif %} - {% endfor %} -
+ {% endfor %} + -
+ + {% endcache %} {% endif %}
diff --git a/templates/comments/tags/post_comment.html b/templates/comments/tags/post_comment.html index 8f4ada2..5064f44 100644 --- a/templates/comments/tags/post_comment.html +++ b/templates/comments/tags/post_comment.html @@ -7,18 +7,11 @@
{% csrf_token %} - {% comment %}

电子邮件地址不会被公开。 必填项已用*标注 -

{% endcomment %}

{{ form.body.label_tag }} {{ form.body }} {{ form.body.errors }} - {% comment %} {% endcomment %}

{% if not form.name.is_hidden %} @@ -26,26 +19,13 @@ {% endif %} {{ form.name }} {{ form.name.errors }} - {% comment %} -

{% endcomment %} - - {% comment %}

- {{ form.url.label_tag }} - {{ form.url }} - {{ form.url.errors }} - -

{% endcomment %} {{ form.parent_comment_id }}
支持markdown