| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import os | 5 import os |
| 6 import time | 6 import time |
| 7 | 7 |
| 8 class FetcherCache(object): | 8 class FetcherCache(object): |
| 9 """A cache for fetcher objects. | 9 """A cache for fetcher objects. |
| 10 """ | 10 """ |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 def HasExpired(self): | 28 def HasExpired(self): |
| 29 return time.time() > self._expiry | 29 return time.time() > self._expiry |
| 30 | 30 |
| 31 def __init__(self, fetcher, timeout_seconds, populate_function): | 31 def __init__(self, fetcher, timeout_seconds, populate_function): |
| 32 self._fetcher = fetcher | 32 self._fetcher = fetcher |
| 33 self._timeout_seconds = timeout_seconds | 33 self._timeout_seconds = timeout_seconds |
| 34 self._populate_function = populate_function | 34 self._populate_function = populate_function |
| 35 self._cache = {} | 35 self._cache = {} |
| 36 | 36 |
| 37 def get(self, key): | 37 def _Fetch(self, fetch_func, key, optional_params=None): |
| 38 if key in self._cache: | 38 if key in self._cache: |
| 39 if self._cache[key].HasExpired(): | 39 if self._cache[key].HasExpired(): |
| 40 self._cache.pop(key) | 40 self._cache.pop(key) |
| 41 else: | 41 else: |
| 42 return self._cache[key]._cache_data | 42 return self._cache[key]._cache_data |
| 43 cache_data = self._fetcher.FetchResource(key).content | 43 if optional_params != None: |
| 44 cache_data = fetch_func(key, optional_params).content |
| 45 else: |
| 46 cache_data = fetch_func(key).content |
| 44 self._cache[key] = self._CacheEntry(self._populate_function(cache_data), | 47 self._cache[key] = self._CacheEntry(self._populate_function(cache_data), |
| 45 time.time() + self._timeout_seconds) | 48 time.time() + self._timeout_seconds) |
| 46 return self._cache[key]._cache_data | 49 return self._cache[key]._cache_data |
| 50 |
| 51 def getFromFileListing(self, path, recursive=False): |
| 52 return self._Fetch(self._fetcher.ListDirectory, path, recursive) |
| 53 |
| 54 def getFromFile(self, key): |
| 55 return self._Fetch(self._fetcher.FetchResource, key) |
| OLD | NEW |