Commits

Carlos Daniel Ruvalcaba Valenzuela committed 47c07f0

Added support for changing the less output cache backend

Comments (0)

Files changed (3)

         return app
 
 Now you just have to put your .less files inside *public/css* and they will be served as CSS.
+
+
+Cache Backends
+----------------------------------
+
+You can change the cache backend storage into any dict like object that can serialize a dict object,
+for example, you can use beaker cache by passing the cache object to the middleware constructor::
+
+    from tgext.less import LESSMiddleware
+    from tg import cache
+
+    make_base_app = base_config.setup_tg_wsgi_app(load_environment)
+
+    def make_app(global_conf, full_stack=True, **app_conf):
+        app = make_base_app(global_conf, full_stack=True, **app_conf)
+
+        # LESS with beaker cache backend
+        app = LESSMiddleware(app, cache=cache)
+
+        return app
+
 [egg_info]
-tag_build = dev
 tag_svn_revision = true

tgext/less/middleware.py

 from webob.response import Response
 
 class LESSMiddleware(object):
-    def __init__(self, app):
+    def __init__(self, app, cache=None):
         self.app = app
-        self.cache = {}
+        if cache:
+            self.cache = cache
+        else:
+            self.cache = {}
+        
         self.static_files_path = os.path.abspath(config['pylons.paths']['static_files'])
 
         # Check if we have a local less compiler available
             start_response('304 Not Modified', (('ETag', etag_key),))
             return ['']
 
-        cached = self.cache.get(path)
+        try:
+            cached = self.cache[path]
+        except KeyError:
+            cached = None
+
         if not cached or cached['etag_key'] != etag_key:
             cached = dict(content=self.compile(full_path), etag_key=etag_key)
             self.cache[path] = cached