Commit db07ac22 by benjaoming

invalidate article cache when plugins are updated

parent 1dd47881
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic from django.contrib.contenttypes import generic
from django.contrib.auth.models import User, Group from django.contrib.auth.models import User, Group
from django.core.cache import cache
from django.db import models from django.db import models
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
...@@ -185,7 +186,21 @@ class Article(models.Model): ...@@ -185,7 +186,21 @@ class Article(models.Model):
else: else:
content = self.current_revision.content content = self.current_revision.content
return mark_safe(article_markdown(content, self)) return mark_safe(article_markdown(content, self))
def get_cache_key(self):
return "wiki:article:%d" % self.id
def get_cached_content(self):
"""Returns cached """
cache_key = self.get_cache_key()
cached_content = cache.get(cache_key)
if cached_content is None:
cached_content = self.render()
cache.set(cache_key, cached_content, settings.CACHE_TIMEOUT)
return cached_content
def clear_cache(self):
cache.delete(self.get_cache_key())
class ArticleForObject(models.Model): class ArticleForObject(models.Model):
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
from django.db import models from django.db import models
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.db.models import signals from django.db.models import signals
from django.core.cache import cache
from wiki.models.article import BaseRevisionMixin from wiki.models.article import BaseRevisionMixin
""" """
...@@ -248,4 +250,19 @@ def update_simple_plugins(**kwargs): ...@@ -248,4 +250,19 @@ def update_simple_plugins(**kwargs):
# TODO: This was breaking things. SimplePlugin doesn't have a revision? # TODO: This was breaking things. SimplePlugin doesn't have a revision?
p_revisions.update(article_revision=instance) p_revisions.update(article_revision=instance)
signals.post_save.connect(update_simple_plugins, ArticleRevision) def on_article_plugin_post_save(**kwargs):
\ No newline at end of file articleplugin = kwargs['instance']
articleplugin.article.clear_cache()
def on_reusable_plugin_post_save(**kwargs):
reusableplugin = kwargs['instance']
for article in reusableplugin.articles.all():
article.clear_cache()
def on_revision_plugin_revision_post_save(**kwargs):
revision = kwargs['instance']
revision.plugin.article.clear_cache()
signals.post_save.connect(update_simple_plugins, ArticleRevision)
signals.post_save.connect(on_article_plugin_post_save, ArticlePlugin)
signals.post_save.connect(on_reusable_plugin_post_save, ReusablePlugin)
...@@ -8,9 +8,7 @@ ...@@ -8,9 +8,7 @@
{% if not preview %} {% if not preview %}
{% if article.current_revision %} {% if article.current_revision %}
{% cache CACHE_TIMEOUT article article.current_revision.id %} {{ article.get_cached_content }}
{{ article.render }}
{% endcache %}
{% endif %} {% endif %}
{% else %} {% else %}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment