| OLD | NEW |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 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 logging | 5 import logging |
| 6 import os | 6 import os |
| 7 import pickle | 7 import pickle |
| 8 import threading | 8 import threading |
| 9 import zlib | 9 import zlib |
| 10 | 10 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 def Get(self, key): | 26 def Get(self, key): |
| 27 with LocalCacher.lock: | 27 with LocalCacher.lock: |
| 28 path = os.path.join(self.cache_dir, key) | 28 path = os.path.join(self.cache_dir, key) |
| 29 if not os.path.exists(path): | 29 if not os.path.exists(path): |
| 30 return None | 30 return None |
| 31 | 31 |
| 32 try: | 32 try: |
| 33 with open(path) as f: | 33 with open(path) as f: |
| 34 return pickle.loads(zlib.decompress(f.read())) | 34 return pickle.loads(zlib.decompress(f.read())) |
| 35 except Exception as error: # pragma: no cover. | 35 except Exception as error: # pragma: no cover. |
| 36 logging.exception('Failed loading cache: %s', error) | 36 raise Exception('Failed loading cache: %s' % error) |
| 37 return None | |
| 38 | 37 |
| 39 def Set(self, key, data, expire_time=0): # pylint: disable=W | 38 def Set(self, key, data, expire_time=0): # pylint: disable=W |
| 40 with LocalCacher.lock: | 39 with LocalCacher.lock: |
| 41 try: | 40 try: |
| 42 with open(os.path.join(self.cache_dir, key), 'wb') as f: | 41 with open(os.path.join(self.cache_dir, key), 'wb') as f: |
| 43 f.write(zlib.compress(pickle.dumps(data))) | 42 f.write(zlib.compress(pickle.dumps(data))) |
| 44 except Exception as e: # pragma: no cover. | 43 except Exception as e: # pragma: no cover. |
| 45 logging.exception('Failed setting cache for key %s: %s', key, e) | 44 raise Exception('Failed setting cache for key %s: %s' % (key, e)) |
| OLD | NEW |