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 from request_scoped_fs_cache import RequestScopedFsCache |
6 | 7 |
7 class FileSystemCache(object): | 8 class FileSystemCache(object): |
8 """This class caches FileSystem data that has been processed. | 9 """This class caches FileSystem data that has been processed. |
9 """ | 10 """ |
10 class Builder(object): | 11 class Builder(object): |
11 """A class to build a FileSystemCache. | 12 """A class to build a FileSystemCache. |
12 """ | 13 """ |
13 def __init__(self, file_system): | 14 def __init__(self, file_system): |
14 self._file_system = file_system | 15 self._file_system = file_system |
15 | 16 |
16 def build(self, populate_function): | 17 def build(self, populate_function): |
17 return FileSystemCache(self._file_system, populate_function) | 18 return FileSystemCache(self._file_system, populate_function) |
18 | 19 |
19 class _CacheEntry(object): | 20 class _CacheEntry(object): |
20 def __init__(self, cache_data, version): | 21 def __init__(self, cache_data, version): |
21 self._cache_data = cache_data | 22 self._cache_data = cache_data |
22 self.version = version | 23 self.version = version |
23 | 24 |
24 def __init__(self, file_system, populate_function): | 25 def __init__(self, file_system, populate_function): |
25 self._file_system = file_system | 26 self._file_system = file_system |
26 self._populate_function = populate_function | 27 self._populate_function = populate_function |
27 self._cache = {} | 28 self._cache = {} |
28 | 29 |
| 30 def ScopeToRequest(self): |
| 31 return RequestScopedFsCache(self) |
| 32 |
29 def _RecursiveList(self, files): | 33 def _RecursiveList(self, files): |
30 all_files = files[:] | 34 all_files = files[:] |
31 dirs = {} | 35 dirs = {} |
32 for filename in files: | 36 for filename in files: |
33 if filename.endswith('/'): | 37 if filename.endswith('/'): |
34 all_files.remove(filename) | 38 all_files.remove(filename) |
35 dirs.update(self._file_system.Read([filename]).Get()) | 39 dirs.update(self._file_system.Read([filename]).Get()) |
36 for dir_, files in dirs.iteritems(): | 40 for dir_, files in dirs.iteritems(): |
37 all_files.extend(self._RecursiveList([dir_ + f for f in files])) | 41 all_files.extend(self._RecursiveList([dir_ + f for f in files])) |
38 return all_files | 42 return all_files |
(...skipping 22 matching lines...) Expand all Loading... |
61 if path in self._cache: | 65 if path in self._cache: |
62 if version > self._cache[path].version: | 66 if version > self._cache[path].version: |
63 self._cache.pop(path) | 67 self._cache.pop(path) |
64 else: | 68 else: |
65 return self._cache[path]._cache_data | 69 return self._cache[path]._cache_data |
66 cache_data = self._RecursiveList( | 70 cache_data = self._RecursiveList( |
67 [path + f for f in self._file_system.ReadSingle(path)]) | 71 [path + f for f in self._file_system.ReadSingle(path)]) |
68 self._cache[path] = self._CacheEntry(self._populate_function(cache_data), | 72 self._cache[path] = self._CacheEntry(self._populate_function(cache_data), |
69 version) | 73 version) |
70 return self._cache[path]._cache_data | 74 return self._cache[path]._cache_data |
OLD | NEW |