Commits

Andriy Kornatskyy committed ac1a9ff

Refactored use of cache profiles.

Comments (0)

Files changed (6)

demos/template/src/config.py

 
 import os
 
-from datetime import timedelta
-
-
 try:  # pragma: nocover
     from ConfigParser import ConfigParser
     config = ConfigParser()
 from wheezy.caching import MemoryCache
 from wheezy.core.collections import defaultdict
 from wheezy.core.i18n import TranslationsManager
-from wheezy.http import CacheProfile
 from wheezy.security.crypto import Ticket
 from wheezy.security.crypto.comp import aes128
 from wheezy.security.crypto.comp import ripemd160
     'http_cache': cache
 })
 
-# Cache Profiles
-static_cache_profile = CacheProfile(
-    'public',
-    duration=timedelta(minutes=15),
-    vary_environ=['HTTP_ACCEPT_ENCODING'],
-    namespace='static',
-    enabled=config.getboolean('cache-profile', 'static-enabled'))
-public_cache_profile = CacheProfile(
-    'server',
-    duration=timedelta(minutes=15),
-    vary_environ=['HTTP_ACCEPT_ENCODING'],
-    vary_cookies=['_a'],
-    no_store=True,
-    enabled=config.getboolean('cache-profile', 'public-enabled'))
-
 # HTTPErrorMiddleware
 options.update({
     'http_errors': defaultdict(lambda: 'http500', {

demos/template/src/membership/web/profile.py

+"""
+"""

demos/template/src/public/web/profile.py

+"""
+"""
+
+from datetime import timedelta
+
+from wheezy.http import CacheProfile
+
+from config import config
+
+
+static_cache_profile = CacheProfile(
+    'public',
+    duration=timedelta(minutes=15),
+    vary_environ=['HTTP_ACCEPT_ENCODING'],
+    namespace='static',
+    enabled=config.getboolean('cache-profile', 'static-enabled'))
+public_cache_profile = CacheProfile(
+    'server',
+    duration=timedelta(minutes=15),
+    vary_environ=['HTTP_ACCEPT_ENCODING'],
+    vary_cookies=['_a'],
+    no_store=True,
+    enabled=config.getboolean('cache-profile', 'public-enabled'))

demos/template/src/public/web/urls.py

 """
 """
 
-from wheezy.http import response_cache
-from wheezy.http.transforms import gzip_transform
-from wheezy.http.transforms import response_transforms
 from wheezy.routing import url
 
-from config import public_cache_profile
 from public.web.views import about
 from public.web.views import home
 from public.web.views import http400
 from public.web.views import http403
 from public.web.views import http404
 from public.web.views import http500
+from public.web.views import static_file
 
 
-server_cache = lambda handler: response_cache(public_cache_profile)(
-    response_transforms(gzip_transform(compress_level=9))(handler))
-
 public_urls = [
-    url('home', server_cache(home), name='home'),
-    url('about', server_cache(about), name='about'),
+    url('home', home, name='home'),
+    url('about', about, name='about'),
 ]
 
 error_urls = [
     url('404', http404, name='http404'),
     url('500', http500, name='http500'),
 ]
+
+static_urls = [
+    url('static/{path:any}', static_file, name='static'),
+    url('favicon.ico', static_file, {'path': 'img/favicon.ico'})
+]

demos/template/src/public/web/views.py

 """
 """
 
+from datetime import timedelta
+
+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
 from wheezy.web.handlers import template_handler
 
+from public.web.profile import public_cache_profile
+from public.web.profile import static_cache_profile
+
+
+wraps_handler = lambda p: lambda h: response_cache(p)(
+    response_transforms(gzip_transform(compress_level=6))(h))
+
 extra = {
     'translation_name': 'public'
 }
 
-home = template_handler('public/home.html', **extra)
-about = template_handler('public/about.html', **extra)
+w = wraps_handler(public_cache_profile)
+home = w(template_handler('public/home.html', **extra))
+about = w(template_handler('public/about.html', **extra))
 
+# cached by nginx
 http400 = template_handler('public/http400.html', status_code=400, **extra)
 http403 = template_handler('public/http403.html', status_code=403, **extra)
 http404 = template_handler('public/http404.html', status_code=404, **extra)
 http500 = template_handler('public/http500.html', status_code=500, **extra)
+
+w = wraps_handler(static_cache_profile)
+static_file = w(file_handler(root='content/static/', age=timedelta(hours=1)))

demos/template/src/urls.py

 """
 """
 
-from datetime import timedelta
+from wheezy.routing import url
 
-from wheezy.http import response_cache
-from wheezy.http.transforms import gzip_transform
-from wheezy.http.transforms import response_transforms
-from wheezy.routing import url
-from wheezy.web.handlers import file_handler
-
-from config import static_cache_profile
 from membership.web.urls import membership_urls
 from public.web.urls import error_urls
 from public.web.urls import public_urls
+from public.web.urls import static_urls
 from public.web.views import home
 
 
 locale_pattern = '{locale:(en|ru)}/'
 locale_defaults = {'locale': 'en'}
-static_files = response_cache(static_cache_profile)(
-    response_transforms(gzip_transform(compress_level=6))(
-        file_handler(
-            root='content/static/',
-            age=timedelta(hours=1))))
 
 locale_urls = public_urls + membership_urls
 locale_urls.append(('error/', error_urls))
 all_urls = [
     url('', home, locale_defaults, name='default'),
     (locale_pattern, locale_urls, locale_defaults),
-    url('static/{path:any}', static_files, name='static'),
-    url('favicon.ico', static_files, {'path': 'img/favicon.ico'})
 ]
+all_urls += static_urls
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.