Chromium Code Reviews| Index: chrome/common/extensions/docs/server2/fetcher_cache.py |
| diff --git a/chrome/common/extensions/docs/server2/fetcher_cache.py b/chrome/common/extensions/docs/server2/fetcher_cache.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f986806a465d3f2c6f9582910464a37b0c45151b |
| --- /dev/null |
| +++ b/chrome/common/extensions/docs/server2/fetcher_cache.py |
| @@ -0,0 +1,46 @@ |
| +# 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 os |
| +import time |
| + |
| +class FetcherCache(object): |
| + """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.
|
| + """ |
| + class Builder(object): |
| + """A class to build a fetcher cache. |
| + """ |
| + def __init__(self, fetcher, timeout_seconds): |
| + self._fetcher = fetcher |
| + self._timeout_seconds = timeout_seconds |
| + |
| + def build(self, populate_function): |
| + return FetcherCache(self._fetcher, |
| + self._timeout_seconds, |
| + populate_function) |
| + |
| + class _CacheEntry(object): |
| + def __init__(self, cache_data, expiry): |
| + self._cache_data = cache_data |
| + self._expiry = expiry |
| + |
| + def HasExpired(self): |
| + return time.time() > self._expiry |
| + |
| + def __init__(self, fetcher, timeout_seconds, populate_function): |
| + self._fetcher = fetcher |
| + self._timeout_seconds = timeout_seconds |
| + self._populate_function = populate_function |
| + self._cache = {} |
| + |
| + def get(self, key): |
| + if key in self._cache: |
| + if self._cache[key].HasExpired(): |
| + self._cache.pop(key) |
| + else: |
| + return self._cache[key]._cache_data |
| + cache_data = self._fetcher.FetchResource(key).content |
| + self._cache[key] = self._CacheEntry(self._populate_function(cache_data), |
| + time.time() + self._timeout_seconds) |
| + return self._cache[key]._cache_data |
|
not at google - send to devlin
2012/06/18 19:30:40
nice
|