| 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_decorator import Cacher | 11 from lib.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 LocalCacher(Cacher): | 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 |
| 22 with LocalCacher.lock: | 22 try: # pragma: no cover. |
| 23 if not os.path.exists(cache_dir): # pragma: no cover. | 23 os.makedirs(cache_dir) |
| 24 os.makedirs(cache_dir) | 24 except Exception: |
| 25 pass |
| 25 | 26 |
| 26 def Get(self, key): | 27 def Get(self, key): |
| 27 with LocalCacher.lock: | 28 with LocalCache.lock: |
| 28 path = os.path.join(self.cache_dir, key) | 29 path = os.path.join(self.cache_dir, key) |
| 29 if not os.path.exists(path): | 30 if not os.path.exists(path): |
| 30 return None | 31 return None |
| 31 | 32 |
| 32 try: | 33 try: |
| 33 with open(path) as f: | 34 with open(path) as f: |
| 34 return pickle.loads(zlib.decompress(f.read())) | 35 return pickle.loads(zlib.decompress(f.read())) |
| 35 except Exception as error: # pragma: no cover. | 36 except Exception as error: # pragma: no cover. |
| 36 raise Exception('Failed loading cache: %s' % error) | 37 raise Exception('Failed loading cache: %s' % error) |
| 37 | 38 |
| 38 def Set(self, key, data, expire_time=0): # pylint: disable=W | 39 def Set(self, key, data, expire_time=0): # pylint: disable=W |
| 39 with LocalCacher.lock: | 40 with LocalCache.lock: |
| 40 try: | 41 try: |
| 41 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: |
| 42 f.write(zlib.compress(pickle.dumps(data))) | 43 f.write(zlib.compress(pickle.dumps(data))) |
| 43 except Exception as e: # pragma: no cover. | 44 except Exception as e: # pragma: no cover. |
| 44 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 |