Skip to content

Commit

Permalink
Add simple method cache decorator to web module. Add cache of render_…
Browse files Browse the repository at this point in the history
…string for home page handler (shaves 100ms of cpu time).
  • Loading branch information
docyes committed Mar 17, 2010
1 parent d85a0bb commit ec80044
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
9 changes: 7 additions & 2 deletions splunkblaze/app.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def __init__(self):
ui_modules=uimodules, ui_modules=uimodules,
xsrf_cookies=True, xsrf_cookies=True,
) )
self.cache = {}
self.xslt_transform = et.XSLT(et.parse(os.path.join(settings["template_path"], "modules", "raw.xslt"))) self.xslt_transform = et.XSLT(et.parse(os.path.join(settings["template_path"], "modules", "raw.xslt")))
tornado.web.Application.__init__(self, handlers, **settings) tornado.web.Application.__init__(self, handlers, **settings)


Expand All @@ -75,7 +76,11 @@ def render_string(self, template_name, **kwargs):


class HomeHandler(BaseHandler): class HomeHandler(BaseHandler):
def get(self): 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): class SyncSearchHandler(BaseHandler, auth.SplunkMixin):
@tornado.web.asynchronous @tornado.web.asynchronous
Expand Down Expand Up @@ -108,6 +113,6 @@ def main():
http_server = tornado.httpserver.HTTPServer(Application()) http_server = tornado.httpserver.HTTPServer(Application())
http_server.listen(options.port) http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start() tornado.ioloop.IOLoop.instance().start()

if __name__ == "__main__": if __name__ == "__main__":
main() main()
13 changes: 12 additions & 1 deletion splunkblaze/web.py
Original file line number Original file line Diff line number Diff line change
@@ -1,9 +1,20 @@
#!/usr/bin/env python #!/usr/bin/env python
import functools


def contextual_class_name(obj): 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.""" """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", "") 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): class BufferedWriter(object):
"""Convenience object to write and maintain ordering of rendered strings. Useful when dealing with multiple async requests and buffering responses.""" """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): def __init__(self, length, callback):
Expand All @@ -19,4 +30,4 @@ def write(self, index, data):
self.buffer[index] = data self.buffer[index] = data
if self.buffer.count(None)==0: if self.buffer.count(None)==0:
string = "".join(self.buffer) string = "".join(self.buffer)
self.callback(string) self.callback(string)

0 comments on commit ec80044

Please sign in to comment.