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

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

Issue 13470005: Refactor the devserver to make it easier to control caching (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: cduvall, rebase Created 7 years, 8 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
(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 from file_system import FileSystem, StatInfo, FileNotFoundError
6 from future import Future
7 import object_store
8
9 class _AsyncUncachedFuture(object):
10 def __init__(self,
11 uncached,
12 current_result,
13 file_system,
14 object_store,
15 namespace):
16 self._uncached = uncached
17 self._current_result = current_result
18 self._file_system = file_system
19 self._object_store = object_store
20 self._namespace = namespace
21
22 def Get(self):
23 mapping = {}
24 new_items = self._uncached.Get()
25 for item in new_items:
26 version = self._file_system.Stat(item).version
27 mapping[item] = (new_items[item], version)
28 self._current_result[item] = new_items[item]
29 self._object_store.SetMulti(mapping, self._namespace, time=0)
30 return self._current_result
31
32 class MemcacheFileSystem(FileSystem):
33 """FileSystem implementation which memcaches the results of Read.
34 """
35 def __init__(self, file_system, object_store):
36 self._file_system = file_system
37 self._object_store = object_store
38
39 def Stat(self, path, stats=None):
40 """Stats the directory given, or if a file is given, stats the files parent
41 directory to get info about the file.
42 """
43 # TODO(kalman): store the whole stat info, not just the version.
44 version = self._object_store.Get(path, object_store.FILE_SYSTEM_STAT).Get()
45 if version is not None:
46 return StatInfo(version)
47
48 # Always stat the parent directory, since it will have the stat of the child
49 # anyway, and this gives us an entire directory's stat info at once.
50 if path.endswith('/'):
51 dir_path = path
52 else:
53 dir_path = path.rsplit('/', 1)[0] + '/'
54
55 dir_stat = self._file_system.Stat(dir_path)
56 if path == dir_path:
57 version = dir_stat.version
58 else:
59 version = dir_stat.child_versions.get(path.split('/')[-1], None)
60 if version is None:
61 raise FileNotFoundError(path)
62 mapping = { path: version }
63
64 for child_path, child_version in dir_stat.child_versions.iteritems():
65 child_path = dir_path + child_path
66 mapping[child_path] = child_version
67 self._object_store.SetMulti(mapping, object_store.FILE_SYSTEM_STAT)
68 return StatInfo(version)
69
70 def Read(self, paths, binary=False):
71 """Reads a list of files. If a file is in memcache and it is not out of
72 date, it is returned. Otherwise, the file is retrieved from the file system.
73 """
74 result = {}
75 uncached = []
76 namespace = object_store.FILE_SYSTEM_READ
77 if binary:
78 namespace = '%s.binary' % namespace
79 results = self._object_store.GetMulti(paths, namespace, time=0).Get()
80 result_values = [x[1] for x in sorted(results.iteritems())]
81 stats = self._object_store.GetMulti(paths,
82 object_store.FILE_SYSTEM_STAT).Get()
83 stat_values = [x[1] for x in sorted(stats.iteritems())]
84 for path, cached_result, stat in zip(sorted(paths),
85 result_values,
86 stat_values):
87 if cached_result is None:
88 uncached.append(path)
89 continue
90 data, version = cached_result
91 # TODO(cduvall): Make this use a multi stat.
92 if stat is None:
93 stat = self.Stat(path).version
94 if stat != version:
95 uncached.append(path)
96 continue
97 result[path] = data
98
99 if not uncached:
100 return Future(value=result)
101 return Future(delegate=_AsyncUncachedFuture(
102 self._file_system.Read(uncached, binary=binary),
103 result,
104 self,
105 self._object_store,
106 namespace))
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698