[geany/www.geany.org] dfea03: Cache snippet list

Enrico Tröger git-noreply at xxxxx
Wed May 1 13:07:32 UTC 2019


Branch:      refs/heads/master
Author:      Enrico Tröger <enrico.troeger at uvena.de>
Committer:   Enrico Tröger <enrico.troeger at uvena.de>
Date:        Wed, 01 May 2019 13:07:32 UTC
Commit:      dfea03b153ee3910caaf37ebffcdd266ee0d77aa
             https://github.com/geany/www.geany.org/commit/dfea03b153ee3910caaf37ebffcdd266ee0d77aa

Log Message:
-----------
Cache snippet list

And invalidate the cached contents on save and delete.


Modified Paths:
--------------
    pastebin/models.py
    pastebin/urls.py
    pastebin/views.py

Modified: pastebin/models.py
11 lines changed, 11 insertions(+), 0 deletions(-)
===================================================================
@@ -17,6 +17,7 @@
 import re
 import time
 
+from django.core.cache import cache
 from django.db import models
 from django.urls import reverse
 from django.utils import timezone
@@ -26,6 +27,8 @@
 
 
 CHARS = 'abcdefghijkmnopqrstuvwwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ1234567890'
+CACHE_KEY_SNIPPET_LIST_NO_CONTENT = 'snippet_list_no_content'
+CACHE_KEY_SNIPPET_LIST_FULL = 'snippet_list_full'
 
 
 # ----------------------------------------------------------------------
@@ -105,6 +108,14 @@ def save(self, *args, **kwargs):  # pylint: disable=arguments-differ
 
         self.content_highlighted = self.content
         super(Snippet, self).save(*args, **kwargs)
+        # invalidate cache
+        cache.delete_many([CACHE_KEY_SNIPPET_LIST_NO_CONTENT, CACHE_KEY_SNIPPET_LIST_FULL])
+
+    # ----------------------------------------------------------------------
+    def delete(self, *args, **kwargs):  # pylint: disable=arguments-differ
+        super(Snippet, self).delete(*args, **kwargs)
+        # invalidate cache
+        cache.delete_many([CACHE_KEY_SNIPPET_LIST_NO_CONTENT, CACHE_KEY_SNIPPET_LIST_FULL])
 
     # ----------------------------------------------------------------------
     def get_absolute_url(self):


Modified: pastebin/urls.py
2 lines changed, 1 insertions(+), 1 deletions(-)
===================================================================
@@ -40,7 +40,7 @@
     url(r'^api/$', never_cache(SnippetAPIView.as_view()), name='snippet_api'),
 
     url(r'^$', never_cache(SnippetNewView.as_view()), name='snippet_new'),
-    url(r'^latest/$', LatestSnippetsView.as_view(), name='snippet_list'),
+    url(r'^latest/$', never_cache(LatestSnippetsView.as_view()), name='snippet_list'),
     url(
         r'^(?P<snippet_id>[a-zA-Z0-9]+)/$',
         SnippetDetailView.as_view(),


Modified: pastebin/views.py
30 lines changed, 20 insertions(+), 10 deletions(-)
===================================================================
@@ -14,6 +14,7 @@
 
 from django.conf import settings
 from django.contrib.sites.models import Site
+from django.core.cache import cache
 from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist
 from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseRedirect
 from django.shortcuts import get_object_or_404, render
@@ -26,25 +27,34 @@
 from django.views.generic.base import TemplateView, View
 from honeypot.decorators import check_honeypot
 
+from geany.decorators import CACHE_TIMEOUT_24HOURS
 from pastebin.api.create import CreateSnippetApiController, SnippetValidationError
 from pastebin.forms import SnippetForm
-from pastebin.models import Snippet
+from pastebin.models import CACHE_KEY_SNIPPET_LIST_FULL, CACHE_KEY_SNIPPET_LIST_NO_CONTENT, Snippet
 
 
 # ----------------------------------------------------------------------
 def _get_snippet_list(no_content=False):
+    if no_content:
+        queryset = Snippet.objects.defer('content', 'content_highlighted')
+        cache_key = CACHE_KEY_SNIPPET_LIST_NO_CONTENT
+    else:
+        queryset = Snippet.objects.all()
+        cache_key = CACHE_KEY_SNIPPET_LIST_FULL
+
+    # snippet list in cache?
+    snippet_list = cache.get(cache_key, None)
+    if snippet_list is not None:
+        return snippet_list
+
+    # nothing in cache, fetch snippets from the database
     try:
         max_snippets = getattr(settings, 'MAX_SNIPPETS_PER_USER', 10)
-        # TODO cache the result set and clear it upon Snippet.save()
-        if no_content:
-            queryset = Snippet.objects.defer('content', 'content_highlighted')
-        else:
-            queryset = Snippet.objects.all()
-
-        snippet_list_ = queryset[:max_snippets]
+        snippet_list = list(queryset[:max_snippets])
+        cache.set(cache_key, snippet_list, CACHE_TIMEOUT_24HOURS)
     except ValueError:
-        snippet_list_ = list()
-    return snippet_list_
+        snippet_list = list()
+    return snippet_list
 
 
 class SnippetNewView(View):



--------------
This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).


More information about the Commits mailing list