Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 # Copyright 2015 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 import logging | |
| 6 import os | |
| 7 import pickle | |
| 8 import threading | |
| 9 import zlib | |
| 10 | |
| 11 from lib.cache_decorator import Cacher | |
| 12 | |
| 13 | |
| 14 class LocalCacher(Cacher): | |
| 15 """Cacher that uses local files to cache data.""" | |
| 16 lock = threading.Lock() | |
| 17 | |
| 18 def __init__(self, cache_dir): | |
| 19 self.cache_dir = cache_dir | |
| 20 assert os.path.exists(cache_dir) | |
| 21 | |
| 22 def Get(self, key): | |
| 23 with LocalCacher.lock: | |
| 24 path = os.path.join(self.cache_dir, key) | |
| 25 if not os.path.exists(path): | |
| 26 return None | |
| 27 | |
| 28 try: | |
| 29 with open(path) as f: | |
| 30 return pickle.loads(zlib.decompress(f.read())) | |
| 31 except Exception as e: # pragma: no cover. | |
| 32 logging.error('Failed loading cache: %s', e) | |
|
stgao
2016/11/08 22:00:43
Could we use logging.exception instead? Same for b
Sharu Jiang
2016/11/11 00:54:21
Done.
| |
| 33 return None | |
| 34 | |
| 35 def Set(self, key, data): # pylint: disable=W | |
| 36 with LocalCacher.lock: | |
| 37 try: | |
| 38 with open(os.path.join(self.cache_dir, key), 'wb') as f: | |
| 39 f.write(zlib.compress(pickle.dumps(data))) | |
| 40 except Exception as e: # pragma: no cover. | |
| 41 logging.error('Failed setting cache for key %s: %s', key, e) | |
| OLD | NEW |