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 for child_path, child_version in stat_info.child_versions.iteritems(): | |
27 self._memcache.Set(path.rsplit('/', 1)[0] + '/' + child_path, | |
28 child_version, | |
29 memcache.MEMCACHE_FILE_SYSTEM_STAT) | |
26 else: | 30 else: |
27 stat_info = self.StatInfo(version) | 31 stat_info = self.StatInfo(version, {}) |
not at google - send to devlin
2012/08/13 05:34:15
memcache can store arbitrary objects, I believe. I
cduvall
2012/08/13 19:45:45
Memcache can store dictionaries, but not objects.
not at google - send to devlin
2012/08/13 23:02:14
I just checked and it looks like pickle can handle
cduvall
2012/08/14 18:15:00
Aha! The problem with pickle was nested classes. I
not at google - send to devlin
2012/08/15 05:24:05
Ok, let's un-nest StatInfo then (or figure out how
| |
28 return stat_info | 32 return stat_info |
29 | 33 |
30 def Read(self, paths, binary=False): | 34 def Read(self, paths, binary=False): |
31 """Reads a list of files. If a file is in memcache and it is not out of | 35 """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. | 36 date, it is returned. Otherwise, the file is retrieved from the file system. |
33 """ | 37 """ |
34 result = {} | 38 result = {} |
35 uncached = [] | 39 uncached = [] |
36 for path in paths: | 40 for path in paths: |
37 cached_result = self._memcache.Get(path, | 41 cached_result = self._memcache.Get(path, |
38 memcache.MEMCACHE_FILE_SYSTEM_READ) | 42 memcache.MEMCACHE_FILE_SYSTEM_READ) |
39 if cached_result is None: | 43 if cached_result is None: |
40 uncached.append(path) | 44 uncached.append(path) |
41 continue | 45 continue |
42 data, version = cached_result | 46 data, version = cached_result |
43 if self.Stat(path).version > version: | 47 if self.Stat(path).version > version: |
44 self._memcache.Delete(path, memcache.MEMCACHE_FILE_SYSTEM_READ) | 48 self._memcache.Delete(path, memcache.MEMCACHE_FILE_SYSTEM_READ) |
45 uncached.append(path) | 49 uncached.append(path) |
46 continue | 50 continue |
47 result[path] = data | 51 result[path] = data |
48 new_items = self._file_system.Read(uncached, binary=binary).Get() | 52 new_items = self._file_system.Read(uncached, binary=binary).Get() |
49 for item in new_items: | 53 for item in new_items: |
50 version = self.Stat(item).version | 54 version = self.Stat(item).version |
51 value = new_items[item] | 55 value = new_items[item] |
52 self._memcache.Set(item, | 56 self._memcache.Set(item, |
53 (value, version), | 57 (value, version), |
54 memcache.MEMCACHE_FILE_SYSTEM_READ) | 58 memcache.MEMCACHE_FILE_SYSTEM_READ) |
55 result[item] = value | 59 result[item] = value |
56 return Future(value=result) | 60 return Future(value=result) |
OLD | NEW |