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 from file_system import FileSystem | 5 from file_system import FileSystem |
6 from future import Future | 6 from future import Future |
7 import appengine_memcache as memcache | 7 import appengine_memcache as memcache |
8 | 8 |
9 class MemcacheFileSystem(FileSystem): | 9 class MemcacheFileSystem(FileSystem): |
10 """FileSystem implementation which memcaches the results of Read. | 10 """FileSystem implementation which memcaches the results of Read. |
11 """ | 11 """ |
12 def __init__(self, file_system, memcache): | 12 def __init__(self, file_system, memcache): |
13 self._file_system = file_system | 13 self._file_system = file_system |
14 self._memcache = memcache | 14 self._memcache = memcache |
15 | 15 |
16 def Stat(self, path): | 16 def Stat(self, path): |
17 """Stats the directory given, or if a file is given, the file's parent | 17 """Stats the directory given, or if a file is given, the file's parent |
18 directory. | 18 directory. |
19 """ | 19 """ |
20 version = self._memcache.Get(path, memcache.MEMCACHE_FILE_SYSTEM_STAT) | 20 version = self._memcache.Get(path, memcache.MEMCACHE_FILE_SYSTEM_STAT) |
21 if version is None: | 21 if version is None: |
22 stat_info = self._file_system.Stat(path) | 22 stat_info = self._file_system.Stat(path) |
23 self._memcache.Set(path, | 23 self._memcache.Set(path, |
24 stat_info.version, | 24 stat_info.version, |
25 memcache.MEMCACHE_FILE_SYSTEM_STAT) | 25 memcache.MEMCACHE_FILE_SYSTEM_STAT) |
26 else: | 26 else: |
27 stat_info = self.StatInfo(version) | 27 stat_info = self.StatInfo(version) |
28 return stat_info | 28 return stat_info |
29 | 29 |
30 def Read(self, paths): | 30 def Read(self, paths, process=True): |
31 """Reads a list of files. If a file is in memcache and it is not out of | 31 """Reads a list of files. If a file is in memcache and it is not out of |
32 date, it is returned. Otherwise, the file is retrieved from the file system. | 32 date, it is returned. Otherwise, the file is retrieved from the file system. |
33 """ | 33 """ |
34 result = {} | 34 result = {} |
35 uncached = [] | 35 uncached = [] |
36 for path in paths: | 36 for path in paths: |
37 cached_result = self._memcache.Get(path, | 37 cached_result = self._memcache.Get(path, |
38 memcache.MEMCACHE_FILE_SYSTEM_READ) | 38 memcache.MEMCACHE_FILE_SYSTEM_READ) |
39 if cached_result is None: | 39 if cached_result is None: |
40 uncached.append(path) | 40 uncached.append(path) |
41 continue | 41 continue |
42 data, version = cached_result | 42 data, version = cached_result |
43 if self.Stat(path).version > version: | 43 if self.Stat(path).version > version: |
44 self._memcache.Delete(path, memcache.MEMCACHE_FILE_SYSTEM_READ) | 44 self._memcache.Delete(path, memcache.MEMCACHE_FILE_SYSTEM_READ) |
45 uncached.append(path) | 45 uncached.append(path) |
46 continue | 46 continue |
47 result[path] = data | 47 result[path] = data |
48 new_items = self._file_system.Read(uncached).Get() | 48 new_items = self._file_system.Read(uncached, process).Get() |
not at google - send to devlin
2012/07/27 04:54:36
process=process
or rather, binary=binary
cduvall
2012/07/27 18:12:28
Done.
| |
49 for item in new_items: | 49 for item in new_items: |
50 version = self.Stat(item).version | 50 version = self.Stat(item).version |
51 value = new_items[item] | 51 value = new_items[item] |
52 self._memcache.Set(item, | 52 self._memcache.Set(item, |
53 (value, version), | 53 (value, version), |
54 memcache.MEMCACHE_FILE_SYSTEM_READ) | 54 memcache.MEMCACHE_FILE_SYSTEM_READ) |
55 result[item] = value | 55 result[item] = value |
56 return Future(value=result) | 56 return Future(value=result) |
OLD | NEW |