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

Unified 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 side-by-side diff with in-line comments
Download patch
« 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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: appengine/findit/util_scripts/local_cache.py
diff --git a/appengine/findit/util_scripts/local_cache.py b/appengine/findit/util_scripts/local_cache.py
new file mode 100644
index 0000000000000000000000000000000000000000..e81b2e8d283b9b3a8b632d9e8b50709c8ed9c78b
--- /dev/null
+++ b/appengine/findit/util_scripts/local_cache.py
@@ -0,0 +1,41 @@
+# Copyright 2015 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+import logging
+import os
+import pickle
+import threading
+import zlib
+
+from lib.cache_decorator import Cacher
+
+
+class LocalCacher(Cacher):
+ """Cacher that uses local files to cache data."""
+ lock = threading.Lock()
+
+ def __init__(self, cache_dir):
+ self.cache_dir = cache_dir
+ assert os.path.exists(cache_dir)
+
+ def Get(self, key):
+ with LocalCacher.lock:
+ path = os.path.join(self.cache_dir, key)
+ if not os.path.exists(path):
+ return None
+
+ try:
+ with open(path) as f:
+ return pickle.loads(zlib.decompress(f.read()))
+ except Exception as e: # pragma: no cover.
+ 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.
+ return None
+
+ def Set(self, key, data): # pylint: disable=W
+ with LocalCacher.lock:
+ try:
+ with open(os.path.join(self.cache_dir, key), 'wb') as f:
+ f.write(zlib.compress(pickle.dumps(data)))
+ except Exception as e: # pragma: no cover.
+ logging.error('Failed setting cache for key %s: %s', key, e)
« 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