Commits

Yann Malet committed e462462

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

Comments (0)

Files changed (6)

Add a comment to this file

cmsplugin_feed/__init__.pyc

Binary file removed.

cmsplugin_feed/cms_plugins.py

 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)
         context.update({
             'object': instance,
             'feed': feed,
Add a comment to this file

cmsplugin_feed/cms_plugins.pyc

Binary file removed.

Add a comment to this file

cmsplugin_feed/forms.pyc

Binary file removed.

cmsplugin_feed/models.py

 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)
Add a comment to this file

cmsplugin_feed/models.pyc

Binary file removed.

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.