Index: chrome/common/extensions/docs/server2/template_fetcher.py |
diff --git a/chrome/common/extensions/docs/server2/template_fetcher.py b/chrome/common/extensions/docs/server2/template_fetcher.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6c0c0498a85bbb4de643f1f04266273672c3e530 |
--- /dev/null |
+++ b/chrome/common/extensions/docs/server2/template_fetcher.py |
@@ -0,0 +1,39 @@ |
+# Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+import logging |
+import os |
+import time |
+ |
+from third_party.handlebar import Handlebar |
+ |
+# Cache templates for 5 minutes. |
+CACHE_TIMEOUT = 300 |
+ |
+# Cached templates stored as (branch, path) -> (template, cache time) |
+# The template cache is global so it will not be erased each time a new |
+# TemplateFetcher is created. |
+TEMPLATE_CACHE = {} |
+ |
+class TemplateFetcher(object): |
+ def __init__(self, branch, fetcher): |
+ self._fetcher = fetcher |
+ self._branch = branch |
+ |
+ def __getitem__(self, key): |
+ if (self._branch, key) not in TEMPLATE_CACHE: |
Aaron Boodman
2012/06/05 00:50:04
Nit: Is there a reason for FetchTemplate() now? It
not at google - send to devlin
2012/06/05 00:56:10
Python style guide reasons?
cduvall
2012/06/05 01:07:37
I moved it for now we can move it back out to Fetc
|
+ self.FetchTemplate(key) |
+ return TEMPLATE_CACHE[(self._branch, key)][0] |
+ |
+ def FetchTemplate(self, path): |
+ key = (self._branch, path) |
+ # Check the template cache and whether the cache has expired. |
+ if key in TEMPLATE_CACHE: |
+ if TEMPLATE_CACHE[key][1] < CACHE_TIMEOUT: |
Aaron Boodman
2012/06/05 00:50:04
Indexing tuples by number like this always looks u
Aaron Boodman
2012/06/05 00:50:04
Does this work? Seems like you need to subtract fr
Aaron Boodman
2012/06/05 00:50:04
kalman is right that this is going to be a pain es
cduvall
2012/06/05 01:07:37
Done.
cduvall
2012/06/05 01:07:37
Done.
cduvall
2012/06/05 01:07:37
Agreed, I'll do that in the next CL.
|
+ return TEMPLATE_CACHE[key][0] |
+ logging.info('Template cache miss for: ' + path) |
+ template = self._fetcher.FetchResource(self._branch, path).content |
+ compiled_template = Handlebar(template) |
+ TEMPLATE_CACHE[key] = (compiled_template, time.clock()) |
+ return compiled_template |