| 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 |
| 11 from lib.cache import Cache | 11 from libs.cache import Cache |
| 12 | 12 |
| 13 CACHE_DIR = os.path.join(os.path.expanduser('~'), '.predator', 'cache') | 13 CACHE_DIR = os.path.join(os.path.expanduser('~'), '.predator', 'cache') |
| 14 | 14 |
| 15 | 15 |
| 16 class LocalCache(Cache): | 16 class LocalCache(Cache): |
| 17 """Cacher that uses local files to cache data.""" | 17 """Cacher that uses local files to cache data.""" |
| 18 lock = threading.Lock() | 18 lock = threading.Lock() |
| 19 | 19 |
| 20 def __init__(self, cache_dir=CACHE_DIR): | 20 def __init__(self, cache_dir=CACHE_DIR): |
| 21 self.cache_dir = cache_dir | 21 self.cache_dir = cache_dir |
| (...skipping 14 matching lines...) Expand all Loading... |
| 36 except Exception as error: # pragma: no cover. | 36 except Exception as error: # pragma: no cover. |
| 37 raise Exception('Failed loading cache: %s' % error) | 37 raise Exception('Failed loading cache: %s' % error) |
| 38 | 38 |
| 39 def Set(self, key, data, expire_time=0): # pylint: disable=W | 39 def Set(self, key, data, expire_time=0): # pylint: disable=W |
| 40 with LocalCache.lock: | 40 with LocalCache.lock: |
| 41 try: | 41 try: |
| 42 with open(os.path.join(self.cache_dir, key), 'wb') as f: | 42 with open(os.path.join(self.cache_dir, key), 'wb') as f: |
| 43 f.write(zlib.compress(pickle.dumps(data))) | 43 f.write(zlib.compress(pickle.dumps(data))) |
| 44 except Exception as e: # pragma: no cover. | 44 except Exception as e: # pragma: no cover. |
| 45 raise Exception('Failed setting cache for key %s: %s' % (key, e)) | 45 raise Exception('Failed setting cache for key %s: %s' % (key, e)) |
| OLD | NEW |