Chromium Code Reviews| 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 os | |
| 6 import time | |
| 7 | |
| 8 class FetcherCache(object): | |
| 9 """A cache for a fetcher object. | |
|
not at google - send to devlin
2012/06/18 19:30:40
s/fetcher object/fetcher objects/
cduvall
2012/06/18 19:48:46
Done.
| |
| 10 """ | |
| 11 class Builder(object): | |
| 12 """A class to build a fetcher cache. | |
| 13 """ | |
| 14 def __init__(self, fetcher, timeout_seconds): | |
| 15 self._fetcher = fetcher | |
| 16 self._timeout_seconds = timeout_seconds | |
| 17 | |
| 18 def build(self, populate_function): | |
| 19 return FetcherCache(self._fetcher, | |
| 20 self._timeout_seconds, | |
| 21 populate_function) | |
| 22 | |
| 23 class _CacheEntry(object): | |
| 24 def __init__(self, cache_data, expiry): | |
| 25 self._cache_data = cache_data | |
| 26 self._expiry = expiry | |
| 27 | |
| 28 def HasExpired(self): | |
| 29 return time.time() > self._expiry | |
| 30 | |
| 31 def __init__(self, fetcher, timeout_seconds, populate_function): | |
| 32 self._fetcher = fetcher | |
| 33 self._timeout_seconds = timeout_seconds | |
| 34 self._populate_function = populate_function | |
| 35 self._cache = {} | |
| 36 | |
| 37 def get(self, key): | |
| 38 if key in self._cache: | |
| 39 if self._cache[key].HasExpired(): | |
| 40 self._cache.pop(key) | |
| 41 else: | |
| 42 return self._cache[key]._cache_data | |
| 43 cache_data = self._fetcher.FetchResource(key).content | |
| 44 self._cache[key] = self._CacheEntry(self._populate_function(cache_data), | |
| 45 time.time() + self._timeout_seconds) | |
| 46 return self._cache[key]._cache_data | |
|
not at google - send to devlin
2012/06/18 19:30:40
nice
| |
| OLD | NEW |