1. Yann Malet
  2. cmsplugin-feed


Yann Malet  committed e462462

Cache the the feed to avoid fetching it each time the page is displayed

  • Participants
  • Parent commits 74d05e7
  • Branches default

Comments (0)

Files changed (6)

File cmsplugin_feed/__init__.pyc

Binary file removed.

File cmsplugin_feed/cms_plugins.py

View file
 import feedparser
 from django.utils.translation import ugettext as _
+from django.core.cache import cache
 from cms.plugin_base import CMSPluginBase
 from cms.plugin_pool import plugin_pool
 from cmsplugin_feed.models import Feed
 from cmsplugin_feed.forms import FeedForm
+def get_cached_feed(instance):
+    """
+    get the feed from cache if it exists else fetch it.
+    """
+    if not cache.has_key("feed_%s" %instance.id):
+        feed = feedparser.parse(instance.feed_url)
+        cache.set("feed_%s" %instance.id, feed, 100)
+    return cache.get("feed_%s" %instance.id)
 class FeedPlugin(CMSPluginBase):
     model = Feed
     name = _('Feed')
     render_template = 'cmsplugin_feed/feed.html'
     def render(self, context, instance, placeholder):
-        feed = feedparser.parse(instance.feed_url)
+        feed = get_cached_feed(instance)
             'object': instance,
             'feed': feed,

File cmsplugin_feed/cms_plugins.pyc

Binary file removed.

File cmsplugin_feed/forms.pyc

Binary file removed.

File cmsplugin_feed/models.py

View file
 from django.db import models
+from django.core.cache import cache
 from django.utils.translation import ugettext as _
 from cms.models import CMSPlugin
     def __unicode__(self):
         return self.name
+    def save(self, *args, **kwargs):
+        if self.id:
+            cache.delete("feed_%s" %self.id)
+        return super(Feed, self).save(*args, **kwargs)

File cmsplugin_feed/models.pyc

Binary file removed.