changeset 1001:99d81080b98e

fixes #55 atom feed cache invalidation
author Bruno Gola <brunogola@gmail.com>
date Fri, 28 Oct 2011 20:22:01 -0200
parents 48ef9599664c
children d4c6cb532f72
files MoinMoin/apps/feed/_tests/test_feed.py MoinMoin/apps/feed/views.py
diffstat 2 files changed, 28 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/apps/feed/_tests/test_feed.py	Fri Oct 28 18:50:36 2011 -0200
+++ b/MoinMoin/apps/feed/_tests/test_feed.py	Fri Oct 28 20:22:01 2011 -0200
@@ -8,7 +8,8 @@
 from MoinMoin._tests import wikiconfig
 
 from MoinMoin.items import Item
-from MoinMoin.config import CONTENTTYPE
+from MoinMoin.config import CONTENTTYPE, COMMENT
+from MoinMoin._tests import update_item
 
 class TestFeeds(object):
     class Config(wikiconfig.Config):
@@ -26,12 +27,21 @@
             assert '</feed>' in rv.data
 
     def test_global_atom_with_an_item(self):
-        basename = u'Bar'
-        item = Item.create(basename)
-        item._save({CONTENTTYPE: u'text/plain;charset=utf-8'}, "foo data for feed item")
+        basename = u'Foo'
+        item = update_item(basename, {COMMENT: u"foo data for feed item"}, '')
         with self.app.test_client() as c:
             rv = c.get('/+feed/atom')
             assert rv.status == '200 OK'
             assert rv.headers['Content-Type'] == 'application/atom+xml'
             assert rv.data.startswith('<?xml')
             assert "foo data for feed item" in rv.data
+
+        # tests the cache invalidation
+        update_item(basename, {COMMENT: u"checking if the cache invalidation works"}, '')
+        with self.app.test_client() as c:
+            rv = c.get('/+feed/atom')
+            assert rv.status == '200 OK'
+            assert rv.headers['Content-Type'] == 'application/atom+xml'
+            assert rv.data.startswith('<?xml')
+            assert "checking if the cache invalidation works" in rv.data
+
--- a/MoinMoin/apps/feed/views.py	Fri Oct 28 18:50:36 2011 -0200
+++ b/MoinMoin/apps/feed/views.py	Fri Oct 28 20:22:01 2011 -0200
@@ -27,7 +27,7 @@
 from MoinMoin.apps.feed import feed
 from MoinMoin.config import (NAME, NAME_EXACT, WIKINAME, ACL, ACTION, ADDRESS,
                             HOSTNAME, USERID, COMMENT, MTIME, REVID, ALL_REVS,
-                            PARENTID)
+                            PARENTID, LATEST_REVS)
 from MoinMoin.themes import get_editor_info
 from MoinMoin.items import Item
 from MoinMoin.util.crypto import cache_key
@@ -41,8 +41,17 @@
     # - full item in html is nice
     # - diffs in textmode are OK, but look very simple
     # - full-item content in textmode is OK, but looks very simple
-    cid = cache_key(usage="atom", item_name=item_name)
-    content = app.cache.get(cid)
+    query = Term(WIKINAME, app.cfg.interwikiname)
+    if item_name:
+        query = And([query, Term(NAME_EXACT, item_name), ])
+    revs = list(flaskg.storage.search(query, idx_name=LATEST_REVS, sortedby=[MTIME], reverse=True, limit=1))
+    if revs:
+        rev = revs[0]
+        cid = cache_key(usage="atom", revid=rev.revid, item_name=item_name)
+        content = app.cache.get(cid)
+    else:
+        content = None
+        cid = None
     if content is None:
         title = app.cfg.sitename
         feed = AtomFeed(title=title, feed_url=request.url, url=request.host_url)
@@ -79,6 +88,7 @@
                      updated=datetime.fromtimestamp(rev.meta[MTIME]),
                     )
         content = feed.to_string()
-        app.cache.set(cid, content)
+        if cid is not None:
+            app.cache.set(cid, content)
     return Response(content, content_type='application/atom+xml')