Branch: refs/heads/issue5_geany_themes_preview Author: Enrico Tröger enrico.troeger@uvena.de Committer: Enrico Tröger enrico.troeger@uvena.de Date: Sat, 29 Jun 2019 08:13:56 UTC Commit: c5a12415d902bdcb5d66538a885e1ca90ea52d88 https://github.com/geany/www.geany.org/commit/c5a12415d902bdcb5d66538a885e1c...
Log Message: ----------- Theme preview: Refetch theme index only if MD5 hash has changed
Instead of fetching the whole theme index each hour, fetch the MD5 hash and compare it to the hash of the last change and then fetch the theme index only if the hash has changed.
Modified Paths: -------------- static_docs/views.py
Modified: static_docs/views.py 45 lines changed, 40 insertions(+), 5 deletions(-) =================================================================== @@ -19,6 +19,7 @@ import re
from django.conf import settings +from django.core.cache import cache from django.http import Http404 from django.views.generic.base import TemplateView from mezzanine_pagedown.filters import plain as markdown_plain @@ -30,6 +31,9 @@ RELEASE_REGEXP = re.compile(r'^Geany (?P<version>[0-9.]+) ((?P<date>.*))$') DATE_PATTERNS_TO_BE_IGNORED = ('TBD', 'TBA', 'unreleased')
+CACHE_KEY_THEME_INDEX_MD5_HASH = 'THEME_INDEX_MD5_HASH' +CACHE_KEY_THEME_INDEX = 'THEME_INDEX' + logger = logging.getLogger(__name__) # pylint: disable=invalid-name
@@ -257,11 +261,42 @@ def get_context_data(self, **kwargs): return context
# ---------------------------------------------------------------------- - @cache_function(CACHE_TIMEOUT_1HOUR) def _get_theme_index(self): - self._fetch_file_via_github_api('index/index.json', repository='geany-themes') - return self._parse_themes_index() + """ + Refresh the theme index by: + - querying the MD5 hash from Github + - compare the freshly retrieved MD5 hash against the cached one + - load the whole theme index if: + - the MD5 hashes differ + - the MD5 hash was not retrieved yet or has been expired from cache + - the theme index was not retrieved yet or has been expired from cache + - after loading the whole theme index cache it long (24 hours) + - store the freshly retrieved MD5 hash in the cache + """ + theme_index_md5_hash = self._query_theme_index_md5_hash() + cached_theme_index_md5_hash = cache.get(CACHE_KEY_THEME_INDEX_MD5_HASH) + theme_index = cache.get(CACHE_KEY_THEME_INDEX) + + # theme index has been changed remotely? + if theme_index_md5_hash != cached_theme_index_md5_hash or theme_index is None: + logger.debug('Refresh theme index from Github (MD5: {})'.format(theme_index_md5_hash)) + # query whole theme index + theme_index = self._query_parse_themes_index() + # cache it for later + cache.set(CACHE_KEY_THEME_INDEX, theme_index, CACHE_TIMEOUT_24HOURS) + + # cache MD5 hash + cache.set(CACHE_KEY_THEME_INDEX_MD5_HASH, theme_index_md5_hash, CACHE_TIMEOUT_24HOURS) + + return theme_index
# ---------------------------------------------------------------------- - def _parse_themes_index(self): - return json.loads(self._file_contents) + def _query_theme_index_md5_hash(self): + self._fetch_file_via_github_api('index/index.json.md5', repository='geany-themes') + return self._file_contents.strip() + + # ---------------------------------------------------------------------- + def _query_parse_themes_index(self): + self._fetch_file_via_github_api('index/index.json', repository='geany-themes') + theme_index = json.loads(self._file_contents) + return theme_index
-------------- This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).