| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 import logging | |
| 6 import os | |
| 7 import time | |
| 8 | |
| 9 from third_party.handlebar import Handlebar | |
| 10 | |
| 11 # Cache templates for 5 minutes. | |
| 12 CACHE_TIMEOUT = 300 | |
| 13 | |
| 14 # Cached templates stored as (branch, path) -> (template, cache time) | |
| 15 # The template cache is global so it will not be erased each time a new | |
| 16 # TemplateFetcher is created. | |
| 17 TEMPLATE_CACHE = {} | |
| 18 | |
| 19 class TemplateFetcher(object): | |
| 20 def __init__(self, branch, fetcher): | |
| 21 self._fetcher = fetcher | |
| 22 self._branch = branch | |
| 23 | |
| 24 def __getitem__(self, path): | |
| 25 key = (self._branch, path) | |
| 26 if key in TEMPLATE_CACHE: | |
| 27 compiled_template, compile_time = TEMPLATE_CACHE[key] | |
| 28 if (time.clock() - compile_time) > CACHE_TIMEOUT: | |
| 29 TEMPLATE_CACHE.pop(key) | |
| 30 if key not in TEMPLATE_CACHE: | |
| 31 logging.info('Template cache miss for: ' + path) | |
| 32 try: | |
| 33 template = self._fetcher.FetchResource(self._branch, path).content | |
| 34 compiled_template = Handlebar(template) | |
| 35 TEMPLATE_CACHE[key] = (compiled_template, time.clock()) | |
| 36 except: | |
| 37 return '' | |
| 38 | |
| 39 return compiled_template | |
| OLD | NEW |