Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(32)

Side by Side Diff: chrome/common/extensions/docs/server2/fetcher_cache.py

Issue 10704252: Extensions Docs Server: Internal file system (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Tests Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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):
not at google - send to devlin 2012/07/18 10:39:15 probably needs a rename/redocument. also, s/fetch
cduvall 2012/07/18 21:26:10 Done.
9 """A cache for fetcher objects. 9 """A cache for fetcher objects.
10 """ 10 """
11 class Builder(object): 11 class Builder(object):
12 """A class to build a fetcher cache. 12 """A class to build a fetcher cache.
13 """ 13 """
14 def __init__(self, fetcher, timeout_seconds): 14 def __init__(self, fetcher, timeout_seconds):
15 self._fetcher = fetcher 15 self._fetcher = fetcher
16 self._timeout_seconds = timeout_seconds 16 self._timeout_seconds = timeout_seconds
17 17
18 def build(self, populate_function): 18 def build(self, populate_function):
19 return FetcherCache(self._fetcher, 19 return FetcherCache(self._fetcher,
20 self._timeout_seconds, 20 self._timeout_seconds,
21 populate_function) 21 populate_function)
22 22
23 class _CacheEntry(object): 23 class _CacheEntry(object):
not at google - send to devlin 2012/07/18 10:39:15 Here's the thing with this class now, by the way:
cduvall 2012/07/18 21:26:10 Done.
24 def __init__(self, cache_data, expiry): 24 def __init__(self, cache_data, expiry):
25 self._cache_data = cache_data 25 self._cache_data = cache_data
26 self._expiry = expiry 26 self._expiry = expiry
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 _Fetch(self, fetch_func, key, optional_params=None): 37 def _FetchFile(self, filename):
38 return self._fetcher.Read([filename])[filename]
39
40 def _RecursiveList(self, files):
41 all_files = files[:]
42 dirs = {}
43 for filename in files:
44 if filename.endswith('/'):
45 all_files.remove(filename)
46 dirs.update(self._fetcher.Read([filename]))
47 for dir_, files in dirs.iteritems():
48 all_files.extend(self._RecursiveList([dir_ + f for f in files.Get()]))
49 return all_files
50
51 def getFromFile(self, key):
not at google - send to devlin 2012/07/18 10:39:15 document and key -> path
cduvall 2012/07/18 21:26:10 Done.
38 if key in self._cache: 52 if key in self._cache:
39 if self._cache[key].HasExpired(): 53 if self._cache[key].HasExpired():
40 self._cache.pop(key) 54 self._cache.pop(key)
41 else: 55 else:
42 return self._cache[key]._cache_data 56 return self._cache[key]._cache_data
43 if optional_params != None: 57 cache_data = self._FetchFile(key).Get()
44 cache_data = fetch_func(key, optional_params).content
45 else:
46 cache_data = fetch_func(key).content
47 self._cache[key] = self._CacheEntry(self._populate_function(cache_data), 58 self._cache[key] = self._CacheEntry(self._populate_function(cache_data),
48 time.time() + self._timeout_seconds) 59 time.time() + self._timeout_seconds)
49 return self._cache[key]._cache_data 60 return self._cache[key]._cache_data
50 61
51 def getFromFileListing(self, path, recursive=False): 62 def getFromFileListing(self, key):
not at google - send to devlin 2012/07/18 10:39:15 document also key -> path btw this method should
cduvall 2012/07/18 21:26:10 Done.
52 return self._Fetch(self._fetcher.ListDirectory, path, recursive) 63 if key in self._cache:
53 64 if self._cache[key].HasExpired():
54 def getFromFile(self, key): 65 self._cache.pop(key)
55 return self._Fetch(self._fetcher.FetchResource, key) 66 else:
67 return self._cache[key]._cache_data
68 cache_data = self._RecursiveList(
69 [key + f for f in self._FetchFile(key).Get()])
70 self._cache[key] = self._CacheEntry(self._populate_function(cache_data),
71 time.time() + self._timeout_seconds)
72 return self._cache[key]._cache_data
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698