Commits

Andriy Kornatskyy committed 254d237

Updated tutorial due to recent refactoring.

Comments (0)

Files changed (6)

demos/guestbook/app.py

 
 
 main = WSGIApplication([
-            bootstrap_defaults(url_mapping=all_urls),
-            http_cache_middleware_factory,
-            path_routing_middleware_factory
+    bootstrap_defaults(url_mapping=all_urls),
+    http_cache_middleware_factory,
+    path_routing_middleware_factory
 ], options)
 
 

demos/guestbook/config.py

 
 import sqlite3
 
-from wheezy.caching import MemoryCache
+from wheezy.caching.memory import MemoryCache
 from wheezy.html.ext.mako import widget_preprocessor
 from wheezy.web.templates import MakoTemplate
 
 
 cache = MemoryCache()
-cache_factory = lambda: cache
 
 
 def session():
     return sqlite3.connect('guestbook.db',
-        detect_types=sqlite3.PARSE_DECLTYPES)
+                           detect_types=sqlite3.PARSE_DECLTYPES)
 
 
 options = {
-        'render_template': MakoTemplate(
-            directories=['templates'],
-            filesystem_checks=False,
-            preprocessor=[widget_preprocessor]
-        )
+    'render_template': MakoTemplate(
+        directories=['templates'],
+        filesystem_checks=False,
+        preprocessor=[widget_preprocessor]
+    )
 }
 
 # HTTPCacheMiddleware
 options.update({
-        'http_cache_factory': cache_factory
+    'http_cache': cache
 })

demos/guestbook/urls.py

 
 
 all_urls = [
-        url('', ListHandler, name='list'),
-        url('add', AddHandler, name='add'),
-        url('static/{path:any}',
-            file_handler(root='static/'),
-            name='static')
+    url('', ListHandler, name='list'),
+    url('add', AddHandler, name='add'),
+    url('static/{path:any}',
+        file_handler(root='static/'),
+        name='static')
 ]

demos/guestbook/views.py

 from wheezy.web.handlers import BaseHandler
 from wheezy.web.transforms import handler_transforms
 
-from config import cache_factory
+from config import cache
 from config import session
 from models import Greeting
 from repository import Repository
 class ListHandler(BaseHandler):
 
     @handler_cache(CacheProfile('server', duration=timedelta(minutes=15),
-            vary_environ=['HTTP_ACCEPT_ENCODING']))
+                                vary_environ=['HTTP_ACCEPT_ENCODING']))
     @handler_transforms(gzip_transform(compress_level=9, min_length=250))
     def get(self):
         with session() as db:
             repo = Repository(db)
             greetings = repo.list_greetings()
         response = self.render_response('list.html',
-                greetings=greetings)
+                                        greetings=greetings)
         response.dependency = list_cache_dependency
         return response
 
 class AddHandler(BaseHandler):
 
     @handler_cache(CacheProfile('both', duration=timedelta(hours=1),
-            vary_environ=['HTTP_ACCEPT_ENCODING']))
+                                vary_environ=['HTTP_ACCEPT_ENCODING']))
     @handler_transforms(gzip_transform(compress_level=9, min_length=250))
     def get(self, greeting=None):
         greeting = greeting or Greeting()
                 self.error('Sorry, can not add your greeting.')
                 return self.get(greeting)
             db.commit()
-        with cache_factory() as cache:
-            list_cache_dependency.delete(cache)
+        list_cache_dependency.delete(cache)
         return self.see_other_for('list')
 # (source start file, target name, title, author, documentclass
 # [howto/manual]).
 latex_documents = [
-  ('index', 'wheezy.web.tex', u'wheezy.web Documentation',
-   u'Andriy Kornatskyy', 'manual'),
+    ('index', 'wheezy.web.tex', u'wheezy.web Documentation',
+     u'Andriy Kornatskyy', 'manual'),
 ]
 
 # The name of an image file (relative to this directory) to place at the top
 
     def session():
         return sqlite3.connect('guestbook.db',
-            detect_types=sqlite3.PARSE_DECLTYPES)
+                               detect_types=sqlite3.PARSE_DECLTYPES)
 
 We have defined function ``session()`` that returns an object valid to
 issue some database related operations including query for data,
     from wheezy.web.templates import MakoTemplate
 
     options = {
-            'render_template': MakoTemplate(
-                directories=['templates'],
-                filesystem_checks=False,
-                preprocessor=[widget_preprocessor]
-            )
+        'render_template': MakoTemplate(
+            directories=['templates'],
+            filesystem_checks=False,
+            preprocessor=[widget_preprocessor]
+        )
     }
 
 Above configuration says that templates can be found in ``templates``
     from views import ListHandler
 
     all_urls = [
-            url('', ListHandler, name='list'),
-            url('add', AddHandler, name='add'),
-            url('static/{path:any}',
-                file_handler(root='static/'),
-                name='static')
+        url('', ListHandler, name='list'),
+        url('add', AddHandler, name='add'),
+        url('static/{path:any}',
+            file_handler(root='static/'),
+            name='static')
     ]
 
 Note each url mapping has unique name so it can be easily referenced by
 
 
     main = WSGIApplication([
-                bootstrap_defaults(url_mapping=all_urls),
-                path_routing_middleware_factory
+        bootstrap_defaults(url_mapping=all_urls),
+        path_routing_middleware_factory
     ], options)
 
     if __name__ == '__main__':
 
 Open ``config.py`` and add import for MemoryCache::
 
-    from wheezy.caching import MemoryCache
+    from wheezy.caching.memory import MemoryCache
 
 At the end of ``config.py`` add initialization logic for cache, cache factory
 and configuration options for HTTP cache middleware)::
 
     cache = MemoryCache()
-    cache_factory = lambda: cache
 
     # HTTPCacheMiddleware
     options.update({
-            'http_cache_factory': cache_factory
+        'http_cache': cache
     })
 
 Since we are going to use HTTP cache middleware we need instruct application
 factory::
 
     main = WSGIApplication([
-                bootstrap_defaults(url_mapping=all_urls),
-                http_cache_middleware_factory,
-                path_routing_middleware_factory
+        bootstrap_defaults(url_mapping=all_urls),
+        http_cache_middleware_factory,
+        path_routing_middleware_factory
     ], options)
 
 Finally let apply cache profile to the ListHandler. Few imports
 Finally let add a trigger that cause the invalidation to occur in cache.
 Import cache factory from config module::
 
-    from config import cache_factory
+    from config import cache
 
 Modify ``AddHandler`` so on successful commit the content cache for
 ``ListHandler`` response is invalidated::
         def post(self):
             ...
                 db.commit()
-            with cache_factory() as cache:
-                list_cache_dependency.delete(cache)
+            list_cache_dependency.delete(cache)
             return self.see_other_for('list')
 
 Try run application by issuing the following command::