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 | 6 |
7 import object_store | |
8 | |
9 APPS = 'Apps' | |
10 CRON = 'Cron' | |
11 EXTENSIONS = 'Extensions' | |
12 HANDLEBAR = 'Handlebar' | |
13 IDL = 'IDL' | |
14 INTRO = 'Intro' | |
15 JSON = 'JSON' | |
16 LIST = 'List' | |
17 PERMS = 'Perms' | |
18 RENDER = 'Render' | |
19 STATIC = 'Static' | |
20 ZIP = 'Zip' | |
21 | |
22 class _CacheEntry(object): | |
23 def __init__(self, cache_data, version): | |
24 self._cache_data = cache_data | |
25 self.version = version | |
26 | |
7 class FileSystemCache(object): | 27 class FileSystemCache(object): |
8 """This class caches FileSystem data that has been processed. | 28 """This class caches FileSystem data that has been processed. |
9 """ | 29 """ |
10 class Builder(object): | 30 class Builder(object): |
11 """A class to build a FileSystemCache. | 31 """A class to build a FileSystemCache. |
12 """ | 32 """ |
13 def __init__(self, file_system): | 33 def __init__(self, file_system, object_store): |
14 self._file_system = file_system | 34 self._file_system = file_system |
35 self._object_store = object_store | |
15 | 36 |
16 def build(self, populate_function): | 37 def build(self, populate_function, namespace): |
17 return FileSystemCache(self._file_system, populate_function) | 38 return FileSystemCache(self._file_system, |
39 populate_function, | |
40 self._object_store, | |
41 namespace) | |
18 | 42 |
19 class _CacheEntry(object): | 43 def __init__(self, file_system, populate_function, object_store, namespace): |
20 def __init__(self, cache_data, version): | |
21 self._cache_data = cache_data | |
22 self.version = version | |
23 | |
24 def __init__(self, file_system, populate_function): | |
25 self._file_system = file_system | 44 self._file_system = file_system |
26 self._populate_function = populate_function | 45 self._populate_function = populate_function |
27 self._cache = {} | 46 self._object_store = object_store |
47 self._namespace = 'FileSystemCache.' + namespace | |
48 | |
49 def _MakeKey(self, key): | |
50 return self._namespace + '.' + key | |
28 | 51 |
29 def _RecursiveList(self, files): | 52 def _RecursiveList(self, files): |
30 all_files = files[:] | 53 all_files = files[:] |
31 dirs = {} | 54 dirs = {} |
32 for filename in files: | 55 for filename in files: |
33 if filename.endswith('/'): | 56 if filename.endswith('/'): |
34 all_files.remove(filename) | 57 all_files.remove(filename) |
35 dirs.update(self._file_system.Read([filename]).Get()) | 58 dirs.update(self._file_system.Read([filename]).Get()) |
36 for dir_, files in dirs.iteritems(): | 59 for dir_, files in dirs.iteritems(): |
37 all_files.extend(self._RecursiveList([dir_ + f for f in files])) | 60 all_files.extend(self._RecursiveList([dir_ + f for f in files])) |
38 return all_files | 61 return all_files |
39 | 62 |
40 def GetFromFile(self, path): | 63 def GetFromFile(self, path): |
41 """Calls |populate_function| on the contents of the file at |path|. | 64 """Calls |populate_function| on the contents of the file at |path|. |
42 """ | 65 """ |
43 version = self._file_system.Stat(path).version | 66 version = self._file_system.Stat(path).version |
44 if path in self._cache: | 67 cache_entry = self._object_store.Get(self._MakeKey(path), |
45 if version > self._cache[path].version: | 68 object_store.FILE_SYSTEM_CACHE, |
46 self._cache.pop(path) | 69 time=0) |
47 else: | 70 |
48 return self._cache[path]._cache_data | 71 if cache_entry is not None: |
49 cache_data = self._file_system.ReadSingle(path) | 72 if version == cache_entry.version: |
not at google - send to devlin
2012/08/21 00:30:11
if (cache_entry is not None) and (version == cache
cduvall
2012/08/21 01:33:33
Done.
| |
50 self._cache[path] = self._CacheEntry(self._populate_function(cache_data), | 73 return cache_entry._cache_data |
51 version) | 74 cache_data = self._populate_function(self._file_system.ReadSingle(path)) |
52 return self._cache[path]._cache_data | 75 self._object_store.Set(self._MakeKey(path), |
76 _CacheEntry(cache_data, version), | |
77 object_store.FILE_SYSTEM_CACHE, | |
78 time=0) | |
79 return cache_data | |
53 | 80 |
54 def GetFromFileListing(self, path): | 81 def GetFromFileListing(self, path): |
55 """Calls |populate_function| on the listing of the files at |path|. | 82 """Calls |populate_function| on the listing of the files at |path|. |
56 Assumes that the path given is to a directory. | 83 Assumes that the path given is to a directory. |
57 """ | 84 """ |
58 if not path.endswith('/'): | 85 if not path.endswith('/'): |
59 path += '/' | 86 path += '/' |
60 version = self._file_system.Stat(path).version | 87 version = self._file_system.Stat(path).version |
61 if path in self._cache: | 88 cache_entry = self._object_store.Get( |
62 if version > self._cache[path].version: | 89 self._MakeKey(path), |
63 self._cache.pop(path) | 90 object_store.FILE_SYSTEM_CACHE_LISTING, |
91 time=0) | |
92 if cache_entry is not None: | |
93 if version != cache_entry.version: | |
94 self._object_store.Delete(self._MakeKey(path), | |
not at google - send to devlin
2012/08/21 00:30:11
same deal here, don't need to Delete since it's se
cduvall
2012/08/21 01:33:33
Done.
| |
95 object_store.FILE_SYSTEM_CACHE_LISTING) | |
64 else: | 96 else: |
65 return self._cache[path]._cache_data | 97 return cache_entry._cache_data |
66 cache_data = self._RecursiveList( | 98 cache_data = self._populate_function(self._RecursiveList( |
67 [path + f for f in self._file_system.ReadSingle(path)]) | 99 [path + f for f in self._file_system.ReadSingle(path)])) |
68 self._cache[path] = self._CacheEntry(self._populate_function(cache_data), | 100 self._object_store.Set(self._MakeKey(path), |
69 version) | 101 _CacheEntry(cache_data, version), |
70 return self._cache[path]._cache_data | 102 object_store.FILE_SYSTEM_CACHE_LISTING, |
103 time=0) | |
104 return cache_data | |
OLD | NEW |