Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(441)

Side by Side Diff: appengine/findit/util_scripts/local_cache.py

Issue 2456603003: [Predator] Add local cache for get command output. (Closed)
Patch Set: Rebase and move LocalCacher to util_script/ Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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)
OLDNEW
« no previous file with comments | « appengine/findit/lib/cache_decorator.py ('k') | appengine/findit/util_scripts/test/local_cache_test.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698