Chromium Code Reviews| 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 appengine_memcache as memcache | |
| 8 | |
| 9 FS_CACHE_CRON = 'FileSystemCache.Cron' | |
| 10 FS_CACHE_RENDER = 'FileSystemCache.Render' | |
| 11 FS_CACHE_PERMS = 'FileSystemCache.Perms' | |
| 12 FS_CACHE_JSON = 'FileSystemCache.JSON' | |
| 13 FS_CACHE_IDL = 'FileSystemCache.IDL' | |
| 14 FS_CACHE_LIST = 'FileSystemCache.List' | |
| 15 FS_CACHE_ZIP = 'FileSystemCache.Zip' | |
| 16 FS_CACHE_INTRO = 'FileSystemCache.Intro' | |
| 17 FS_CACHE_EXTENSIONS = 'FileSystemCache.Extensions' | |
| 18 FS_CACHE_APPS = 'FileSystemCache.Apps' | |
| 19 FS_CACHE_STATIC = 'FileSystemCache.Static' | |
| 20 FS_CACHE_HANDLEBAR = 'FileSystemCache.Handlebar' | |
|
not at google - send to devlin
2012/08/20 05:27:10
nit: alphabet
extra points (extra nitty): align t
cduvall
2012/08/20 21:28:09
Done.
| |
| 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): |
|
not at google - send to devlin
2012/08/20 05:27:10
I reckon we should rename this (as in my email) to
| |
| 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): |
|
not at google - send to devlin
2012/08/20 05:27:10
ditto (after this patch): rename to Factory
| |
| 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, memcache): |
| 14 self._file_system = file_system | 34 self._file_system = file_system |
| 35 self._memcache = memcache | |
| 15 | 36 |
| 16 def build(self, populate_function): | 37 def build(self, populate_function, namespace): |
|
not at google - send to devlin
2012/08/20 05:27:10
ditto (after this patch): rename to Create
| |
| 17 return FileSystemCache(self._file_system, populate_function) | 38 return FileSystemCache(self._file_system, |
| 39 populate_function, | |
| 40 self._memcache, | |
| 41 namespace) | |
| 18 | 42 |
| 19 class _CacheEntry(object): | 43 def __init__(self, file_system, populate_function, memcache, 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._cache = {} |
|
not at google - send to devlin
2012/08/20 05:27:10
not used anymore?
cduvall
2012/08/20 21:28:09
Done.
| |
| 47 self._memcache = memcache | |
| 48 self._namespace = namespace | |
| 49 | |
| 50 def _MakeKey(self, key): | |
| 51 return self._namespace + '.' + key | |
| 28 | 52 |
| 29 def _RecursiveList(self, files): | 53 def _RecursiveList(self, files): |
| 30 all_files = files[:] | 54 all_files = files[:] |
| 31 dirs = {} | 55 dirs = {} |
| 32 for filename in files: | 56 for filename in files: |
| 33 if filename.endswith('/'): | 57 if filename.endswith('/'): |
| 34 all_files.remove(filename) | 58 all_files.remove(filename) |
| 35 dirs.update(self._file_system.Read([filename]).Get()) | 59 dirs.update(self._file_system.Read([filename]).Get()) |
| 36 for dir_, files in dirs.iteritems(): | 60 for dir_, files in dirs.iteritems(): |
| 37 all_files.extend(self._RecursiveList([dir_ + f for f in files])) | 61 all_files.extend(self._RecursiveList([dir_ + f for f in files])) |
| 38 return all_files | 62 return all_files |
| 39 | 63 |
| 40 def GetFromFile(self, path): | 64 def GetFromFile(self, path): |
| 41 """Calls |populate_function| on the contents of the file at |path|. | 65 """Calls |populate_function| on the contents of the file at |path|. |
| 42 """ | 66 """ |
| 43 version = self._file_system.Stat(path).version | 67 version = self._file_system.Stat(path).version |
| 44 if path in self._cache: | 68 cache_entry = self._memcache.Get(self._MakeKey(path), |
| 45 if version > self._cache[path].version: | 69 memcache.MEMCACHE_FILE_SYSTEM_CACHE, |
| 46 self._cache.pop(path) | 70 time=0) |
| 71 | |
| 72 if cache_entry is not None: | |
|
not at google - send to devlin
2012/08/20 05:27:10
nit: extra space
cduvall
2012/08/20 21:28:09
Done.
| |
| 73 if version != cache_entry.version: | |
| 74 self._memcache.Delete(self._MakeKey(path), | |
| 75 memcache.MEMCACHE_FILE_SYSTEM_CACHE) | |
|
not at google - send to devlin
2012/08/20 05:27:10
I don't think we need to Delete this, we Set it ri
cduvall
2012/08/20 21:28:09
Done.
| |
| 47 else: | 76 else: |
| 48 return self._cache[path]._cache_data | 77 return cache_entry._cache_data |
| 49 cache_data = self._file_system.ReadSingle(path) | 78 cache_data = self._populate_function(self._file_system.ReadSingle(path)) |
| 50 self._cache[path] = self._CacheEntry(self._populate_function(cache_data), | 79 self._memcache.Set(self._MakeKey(path), |
| 51 version) | 80 _CacheEntry(cache_data, version), |
| 52 return self._cache[path]._cache_data | 81 memcache.MEMCACHE_FILE_SYSTEM_CACHE, |
| 82 time=0) | |
| 83 return cache_data | |
| 53 | 84 |
| 54 def GetFromFileListing(self, path): | 85 def GetFromFileListing(self, path): |
| 55 """Calls |populate_function| on the listing of the files at |path|. | 86 """Calls |populate_function| on the listing of the files at |path|. |
| 56 Assumes that the path given is to a directory. | 87 Assumes that the path given is to a directory. |
| 57 """ | 88 """ |
| 58 if not path.endswith('/'): | 89 if not path.endswith('/'): |
| 59 path += '/' | 90 path += '/' |
| 60 version = self._file_system.Stat(path).version | 91 version = self._file_system.Stat(path).version |
| 61 if path in self._cache: | 92 cache_entry = self._memcache.Get( |
| 62 if version > self._cache[path].version: | 93 self._MakeKey(path), |
| 63 self._cache.pop(path) | 94 memcache.MEMCACHE_FILE_SYSTEM_CACHE_LISTING, |
| 95 time=0) | |
| 96 if cache_entry is not None: | |
| 97 if version != cache_entry.version: | |
| 98 self._memcache.Delete(self._MakeKey(path), | |
| 99 memcache.MEMCACHE_FILE_SYSTEM_CACHE_LISTING) | |
| 64 else: | 100 else: |
| 65 return self._cache[path]._cache_data | 101 return cache_entry._cache_data |
| 66 cache_data = self._RecursiveList( | 102 cache_data = self._populate_function(self._RecursiveList( |
| 67 [path + f for f in self._file_system.ReadSingle(path)]) | 103 [path + f for f in self._file_system.ReadSingle(path)])) |
| 68 self._cache[path] = self._CacheEntry(self._populate_function(cache_data), | 104 self._memcache.Set(self._MakeKey(path), |
| 69 version) | 105 _CacheEntry(cache_data, version), |
| 70 return self._cache[path]._cache_data | 106 memcache.MEMCACHE_FILE_SYSTEM_CACHE_LISTING, |
| 107 time=0) | |
| 108 return cache_data | |
| OLD | NEW |