| OLD | NEW |
| (Empty) | |
| 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 |
| 3 # found in the LICENSE file. |
| 4 |
| 5 from file_system import FileSystem |
| 6 from future import Future |
| 7 import appengine_memcache as memcache |
| 8 |
| 9 class MemcacheFileSystem(FileSystem): |
| 10 """FileSystem implementation which memcaches the results of Read. |
| 11 """ |
| 12 def __init__(self, file_system, memcache): |
| 13 self._file_system = file_system |
| 14 self._memcache = memcache |
| 15 |
| 16 def Stat(self, path): |
| 17 """Stats the directory given, or if a file is given, the file's parent |
| 18 directory. |
| 19 """ |
| 20 version = self._memcache.Get(path, memcache.MEMCACHE_FILE_SYSTEM_STAT) |
| 21 if version is None: |
| 22 stat_info = self._file_system.Stat(path) |
| 23 self._memcache.Set(path, |
| 24 stat_info.version, |
| 25 memcache.MEMCACHE_FILE_SYSTEM_STAT) |
| 26 else: |
| 27 stat_info = self.StatInfo(version) |
| 28 return stat_info |
| 29 |
| 30 def Read(self, paths): |
| 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. |
| 33 """ |
| 34 result = {} |
| 35 uncached = [] |
| 36 for path in paths: |
| 37 cached_result = self._memcache.Get(path, |
| 38 memcache.MEMCACHE_FILE_SYSTEM_READ) |
| 39 if cached_result is None: |
| 40 uncached.append(path) |
| 41 continue |
| 42 data, version = cached_result |
| 43 if self.Stat(path).version > version: |
| 44 self._memcache.Delete(path, memcache.MEMCACHE_FILE_SYSTEM_READ) |
| 45 uncached.append(path) |
| 46 continue |
| 47 result[path] = data |
| 48 new_items = self._file_system.Read(uncached).Get() |
| 49 for item in new_items: |
| 50 version = self.Stat(item).version |
| 51 value = new_items[item] |
| 52 self._memcache.Set(item, |
| 53 (value, version), |
| 54 memcache.MEMCACHE_FILE_SYSTEM_READ) |
| 55 result[item] = value |
| 56 return Future(value=result) |
| OLD | NEW |