]> git.llucax.com Git - software/blitiri.git/commitdiff
Add basic caching support to restructured text rendering
authorLeandro Lucarella <llucax@gmail.com>
Sun, 31 Aug 2008 17:45:41 +0000 (14:45 -0300)
committerLeandro Lucarella <llucax@gmail.com>
Sun, 31 Aug 2008 20:04:53 +0000 (17:04 -0300)
blitiri.cgi
config.py.sample

index b158380c7a1dbfcaba7e9bd5e67f00b90b17a6a7..fdefadbdcbd9fe6d9827ea5f5f81bb97ecbc5513 100755 (executable)
@@ -24,6 +24,10 @@ comments_path = "/tmp/blog/comments"
 # default templates. If they're not found, the built-in ones will be used.
 templates_path = "/tmp/blog/templates"
 
+# Path where the cache is stored (must be writeable by the web server)
+# If None is specified, cache is disabled
+cache_path = "/tmp/blog/cache"
+
 # URL to the blog, including the name. Can be a full URL or just the path.
 blog_url = "/blog/blitiri.cgi"
 
@@ -404,6 +408,22 @@ div.section h1 {
 
 """
 
+# Cache decorator
+def cached(f):
+       def decorate(obj, *args, **kwargs):
+               if cache_path is None: # cache disabled
+                       s = f(obj, *args, **kwargs)
+               else:
+                       cache_file = os.path.join(cache_path,
+                                       'blitiri.cache.%s.html' % hash(obj))
+                       try:
+                               s = open(cache_file).read()
+                       except:
+                               s = f(obj, *args, **kwargs)
+                               open(cache_file, 'w').write(s)
+               return s
+       return decorate
+
 # helper functions
 def rst_to_html(rst, secure = True):
        settings = {
@@ -417,6 +437,7 @@ def rst_to_html(rst, secure = True):
        parts = publish_parts(rst, settings_overrides = settings,
                                writer_name = "html")
        return parts['body'].encode('utf8')
+rst_to_html = cached(rst_to_html)
 
 def validate_rst(rst, secure = True):
        try:
index c3b3653c845b40e75e9f93e7e4cf10a16516d933..79efac760ff447441ff848ada21ef30b62a7b790 100644 (file)
@@ -20,6 +20,9 @@ comments_path = "/tmp/blog/comments"
 # default templates. If they're not found, the built-in ones will be used.
 templates_path = "/tmp/blog/templates"
 
+# Path where the cache is stored (must be writeable by the web server)
+cache_path = "/tmp/blog/cache"
+
 # URL to the blog, including the name. Can be a full URL or just the path.
 blog_url = "/blog/blitiri.cgi"