Skip to content

Commit

Permalink
return to 'per-article' caching schema, fix #1072
Browse files Browse the repository at this point in the history
  • Loading branch information
jenda1 committed Oct 24, 2020
1 parent a2c7c85 commit 1e4b2fd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 34 deletions.
3 changes: 1 addition & 2 deletions src/wiki/core/markdown/__init__.py
Expand Up @@ -5,13 +5,12 @@


class ArticleMarkdown(markdown.Markdown):
def __init__(self, article, preview=False, user=None, *args, **kwargs):
def __init__(self, article, preview=False, *args, **kwargs):
kwargs.update(settings.MARKDOWN_KWARGS)
kwargs["extensions"] = self.get_markdown_extensions()
super().__init__(*args, **kwargs)
self.article = article
self.preview = preview
self.user = user

def core_extensions(self):
"""List of core extensions found in the mdx folder"""
Expand Down
39 changes: 8 additions & 31 deletions src/wiki/models/article.py
Expand Up @@ -207,7 +207,7 @@ class Meta:
("grant", _("Can assign permissions to other users")),
)

def render(self, preview_content=None, user=None):
def render(self, preview_content=None):
if not self.current_revision:
return ""
if preview_content:
Expand All @@ -216,7 +216,7 @@ def render(self, preview_content=None, user=None):
content = self.current_revision.content
return mark_safe(
article_markdown(
content, self, preview=preview_content is not None, user=user
content, self, preview=preview_content is not None
)
)

Expand All @@ -230,38 +230,15 @@ def get_cache_key(self):
# https://github.com/django-wiki/django-wiki/issues/1065
return slugify(key_raw, allow_unicode=True)

def get_cache_content_key(self, user=None):
"""Returns per-article-user cache key."""
key_raw = "{key}:{user}".format(
key=self.get_cache_key(), user=user.get_username() if user else ""
)
# https://github.com/django-wiki/django-wiki/issues/1065
return slugify(key_raw, allow_unicode=True)

def get_cached_content(self, user=None):
"""Returns cached version of rendered article.
The cache contains one "per-article" entry plus multiple
"per-article-user" entries. The per-article-user entries contain the
rendered article, the per-article entry contains list of the
per-article-user keys. The rendered article in cache (per-article-user)
is used only if the key is in the per-article entry. To delete
per-article invalidates all article cache entries."""
def get_cached_content(self):
"""Returns cached version of rendered article."""

cache_key = self.get_cache_key()
cache_content_key = self.get_cache_content_key(user)

cached_items = cache.get(cache_key, list())

if cache_content_key in cached_items:
cached_content = cache.get(cache_content_key)
if cached_content is not None:
return mark_safe(cached_content)
cached_content = cache.get(cache_key)

cached_content = self.render(user=user)
cached_items.append(cache_content_key)
cache.set(cache_key, cached_items, settings.CACHE_TIMEOUT)
cache.set(cache_content_key, cached_content, settings.CACHE_TIMEOUT)
if cached_content is None:
cached_content = self.render()
cache.set(cache_key, cached_content, settings.CACHE_TIMEOUT)

return mark_safe(cached_content)

Expand Down
2 changes: 1 addition & 1 deletion src/wiki/templatetags/wiki_tags.py
Expand Up @@ -50,7 +50,7 @@ def wiki_render(context, article, preview_content=None):
if preview_content:
content = article.render(preview_content=preview_content)
elif article.current_revision:
content = article.get_cached_content(user=context.get("user"))
content = article.get_cached_content()
else:
content = None

Expand Down

0 comments on commit 1e4b2fd

Please sign in to comment.