Commits

Mike Bayer committed cca2452

Added support for the ``cache_timeout`` Mako
argument to the Mako plugin, which will pass
the value to the ``expiration_time`` argument
of :meth:`.CacheRegion.get_or_create`.

Comments (0)

Files changed (4)

docs/build/changelog.rst

     :version: 0.4.3
 
     .. change::
+        :tags: bug
+
+      Added support for the ``cache_timeout`` Mako
+      argument to the Mako plugin, which will pass
+      the value to the ``expiration_time`` argument
+      of :meth:`.CacheRegion.get_or_create`.
+
+    .. change::
         :tags: feature
         :pullreq: 13
 

dogpile/cache/plugins/mako_cache.py

             raise KeyError("No such region '%s'" % region)
 
     def get_and_replace(self, key, creation_function, **kw):
-        return self._get_region(**kw).get_or_create(key, creation_function)
+        expiration_time = kw.pop("timeout", None)
+        return self._get_region(**kw).get_or_create(key, creation_function,
+                                            expiration_time=expiration_time)
 
     def get_or_create(self, key, creation_function, **kw):
         return self.get_and_replace(key, creation_function, **kw)
         self._get_region(**kw).put(key, value)
 
     def get(self, key, **kw):
-        return self._get_region(**kw).get(key)
+        expiration_time = kw.pop("timeout", None)
+        return self._get_region(**kw).get(key, expiration_time=expiration_time)
 
     def invalidate(self, key, **kw):
         self._get_region(**kw).delete(key)

dogpile/cache/region.py

                 return self.get_or_create(key, creator, expiration_time,
                                                 should_cache_fn)
 
+
             def invalidate(*arg, **kw):
                 key = key_generator(*arg, **kw)
                 self.delete(key)

tests/cache/plugins/test_mako_cache.py

     raise SkipTest("this test suite requires mako templates")
 
 from mako.template import Template
-from dogpile.cache import make_region
 from mako.cache import register_plugin
+import mock
 
 register_plugin("dogpile.cache", "dogpile.cache.plugins.mako_cache", "MakoPlugin")
 
 class TestMakoPlugin(TestCase):
-    def _memory_fixture(self):
+
+    def _mock_fixture(self):
+        reg = mock.MagicMock()
+        reg.get_or_create.return_value = "hello world"
         my_regions = {
-            "myregion": make_region().configure(
-                        "dogpile.cache.memory",
-                    ),
+            "myregion": reg
         }
         return {
             'cache_impl': 'dogpile.cache',
             'cache_args': {'regions': my_regions}
-        }
-    def test_basic(self):
-        x = [0]
-        def nextx():
-            x[0] += 1
-            return x[0]
+        }, reg
 
-        kw = self._memory_fixture()
+    def test_basic(self):
+        kw, reg = self._mock_fixture()
         t = Template(
-            '<%page cached="True" cache_region="myregion"/>${nextx()}',
+            '<%page cached="True" cache_region="myregion"/>hi',
             **kw
         )
+        t.render()
+        eq_(reg.get_or_create.call_count, 1)
 
-        eq_(t.render(nextx=nextx), "1")
-        eq_(t.render(nextx=nextx), "1")
+    def test_timeout(self):
+        kw, reg = self._mock_fixture()
+        t = Template("""
+                <%def name="mydef()" cached="True" cache_region="myregion"
+                        cache_timeout="20">
+                    some content
+                </%def>
+                ${mydef()}
+                """, **kw)
+        t.render()
+        eq_(
+            reg.get_or_create.call_args[1],
+            {"expiration_time": 20}
+        )