Permalink
Browse files

Add simple method cache decorator to web module. Add cache of render_…

…string for home page handler (shaves 100ms of cpu time).
  • Loading branch information...
1 parent d85a0bb commit ec8004460be2bd0abe5fae296469c01eccdf9fea @docyes committed Mar 17, 2010
Showing with 19 additions and 3 deletions.
  1. +7 −2 splunkblaze/app.py
  2. +12 −1 splunkblaze/web.py
View
9 splunkblaze/app.py
@@ -55,6 +55,7 @@ def __init__(self):
ui_modules=uimodules,
xsrf_cookies=True,
)
+ self.cache = {}
self.xslt_transform = et.XSLT(et.parse(os.path.join(settings["template_path"], "modules", "raw.xslt")))
tornado.web.Application.__init__(self, handlers, **settings)
@@ -75,7 +76,11 @@ def render_string(self, template_name, **kwargs):
class HomeHandler(BaseHandler):
def get(self):
- self.render("home/index.html", enable_clear_button=options.enable_clear_button, enable_search_loader=options.enable_search_loader, search_browser_cache_ttl=options.search_browser_cache_ttl)
+ self.finish(self._render_string())
+
+ @web.cache
+ def _render_string(self):
+ return self.render_string("home/index.html", enable_clear_button=options.enable_clear_button, enable_search_loader=options.enable_search_loader, search_browser_cache_ttl=options.search_browser_cache_ttl)
class SyncSearchHandler(BaseHandler, auth.SplunkMixin):
@tornado.web.asynchronous
@@ -108,6 +113,6 @@ def main():
http_server = tornado.httpserver.HTTPServer(Application())
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
-
+
if __name__ == "__main__":
main()
View
13 splunkblaze/web.py
@@ -1,9 +1,20 @@
#!/usr/bin/env python
+import functools
def contextual_class_name(obj):
"""Takes an object reference and derives a css class name. Used primarily for deriving a contextual css selector per handler."""
return obj.__class__.__name__.lower().replace("handler", "")
+def cache(method):
+ """Simple decorator to cache the results of a method. NOTE!!! Does not memoize based on *args and **kwargs."""
+ @functools.wraps(method)
+ def wrapper(self, *args, **kwargs):
+ print method
+ if not self.application.cache.has_key(method):
+ self.application.cache[method] = method(self, *args, **kwargs)
+ return self.application.cache[method]
+ return wrapper
+
class BufferedWriter(object):
"""Convenience object to write and maintain ordering of rendered strings. Useful when dealing with multiple async requests and buffering responses."""
def __init__(self, length, callback):
@@ -19,4 +30,4 @@ def write(self, index, data):
self.buffer[index] = data
if self.buffer.count(None)==0:
string = "".join(self.buffer)
- self.callback(string)
+ self.callback(string)

0 comments on commit ec80044

Please sign in to comment.