Andriy Kornatskyy committed 4e3b391

Added Cache Vary section to Tutorial.

  • Participants
  • Parent commits 882db13

Comments (0)

Files changed (3)


 from wheezy.caching import CacheDependency
 from wheezy.http import CacheProfile
+from wheezy.http.transforms import gzip_transform
 from wheezy.web import handler_cache
 from wheezy.web.handlers import BaseHandler
+from wheezy.web.transforms import handler_transforms
 from config import cache_factory
 from config import session
 class ListHandler(BaseHandler):
-    @handler_cache(CacheProfile('server', duration=timedelta(minutes=15)))
+    @handler_cache(CacheProfile(
+            'server',
+            vary_environ=['HTTP_ACCEPT_ENCODING'],
+            duration=timedelta(minutes=15)))
+    @handler_transforms(gzip_transform(compress_level=9, min_length=250))
     def get(self):
         with session() as db:
             repo = Repository(db)
 and notice that list page is not updated (it is being cached by server). Next
 we will use cache dependency to invalidate content cache.
+Take a look at `wheezy.http`_ for various options available for content 
 Cache Dependency
 Take a look at `wheezy.caching`_ for various cache implementations including
 distributed cache support.
+Cache Vary
+AJAX + JSON, content caching and cache dependency are a great way to boost
+application performance. How about content compression? That is another great
+option to save traffic. What if we were able cache compressed response thus
+we will save on server CPU as well. Let implement this use case.
+Transforms are used to manipulate handler response according to some 
+algorithm. We will use this feature to compress response right before it
+enters content cache.
+Add imports in file ````::
+    from wheezy.http.transforms import gzip_transform
+    from wheezy.web.transforms import handler_transforms
+Let apply compression to ``ListHandler``::
+    class ListHandler(BaseHandler):
+        @handler_cache(CacheProfile('server', duration=timedelta(minutes=15)))
+        @handler_transforms(gzip_transform(compress_level=9, min_length=250))
+        def get(self):
+            ...
+Notice :py:meth:`~wheezy.web.transforms.handler_transforms` decorator
+is after handler cache, this way it able compress response before it goes to
+At this point we have a single version of the cached page - compressed. What 
+about browsers that do not accept gzip content encoding? Would be good somehow
+distinguish web requests that support compression and some that do not.
+Fortunately browser sends HTTP header ``Accept-Encoding`` that serves exactly
+this purpose. All we need is instruct content cache to *vary* response
+depending on value in ``Accept-Encoding`` HTTP header.
+Instruct ``ListHandler`` cache to vary response on ``Accept-Encoding`` HTTP 
+    class ListHandler(BaseHandler):
+        @handler_cache(CacheProfile(
+                'server',
+                vary_environ=['HTTP_ACCEPT_ENCODING'], 
+                duration=timedelta(minutes=15)))
+        @handler_transforms(gzip_transform(compress_level=9, min_length=250))
+        def get(self):
+            ...
+Notice we added ``vary_environ`` and used WSGI environment variable 
+``HTTP_ACCEPT_ENCODING`` to be included into cache key used by content cache.
+Try run application by issuing the following command::
+    $ env/bin/python
+Visit http://localhost:8080/ to see your site in browser.
+Take a look at `wheezy.http`_ for various options available for content 
 .. _`wheezy.caching`:
 .. _`wheezy.html`:
+.. _`wheezy.http`:
 .. _`wheezy.validation`:
 .. _`jQuery`:


 It is recommended to use :py:meth:`~wheezy.web.handlers.file.file_handler`
-together with ``gzip_transform`` and ``httpcache``.
+together with ``gzip_transform`` and ``response_cache`` (requries HTTP cache
 Here is example from :ref:`public_demo` demo application::
-    static_files = httpcache(
-            response_transforms(gzip_transform(compress_level=6))(
-                file_handler(
-                    root='content/static/',
-                    age=timedelta(hours=1))),
-            cache_profile=static_cache_profile,
-            cache=cache)
+    from wheezy.http import response_cache
+    from wheezy.http.transforms import gzip_transform
+    from wheezy.http.transforms import response_transforms
+    from wheezy.web.handlers import file_handler
+    static_files = response_cache(static_cache_profile)(
+        response_transforms(gzip_transform(compress_level=6))(
+            file_handler(
+                root='content/static/',
+                age=timedelta(hours=1))))
     all_urls = [
         url('static/{path:any}', static_files, name='static'),
     from wheezy.http.transforms import gzip_transform
     from wheezy.web.handlers import BaseHandler
-    from wheezy.web.transforms import response_transforms
+    from wheezy.web.transforms import handler_transforms
     class MyHandler(BaseHandler):