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

Side by Side Diff: appengine/findit/lib/cache_decorator.py

Issue 2432203003: [Predator] Run predator. (Closed)
Patch Set: . 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
1 # Copyright 2015 The Chromium Authors. All rights reserved. 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 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 # TODO(http://crbug.com/660466): We should try to break dependencies. 5 # TODO(http://crbug.com/660466): We should try to break dependencies.
6 6
7 """This module provides a decorator to cache the results of a function. 7 """This module provides a decorator to cache the results of a function.
8 8
9 Examples: 9 Examples:
10 1. Decorate a function: 10 1. Decorate a function:
(...skipping 30 matching lines...) Expand all
41 import hashlib 41 import hashlib
42 import inspect 42 import inspect
43 import logging 43 import logging
44 import os 44 import os
45 import pickle 45 import pickle
46 import threading 46 import threading
47 import zlib 47 import zlib
48 48
49 from google.appengine.api import memcache 49 from google.appengine.api import memcache
50 50
51 _DEFAULT_LOCAL_CACHE_DIR = os.path.join(os.path.expanduser('~'), 51 _PREDATOR_ROOT_DIR = os.path.join(os.path.dirname(__file__), os.path.pardir)
stgao 2016/11/02 01:45:48 not "PREDATOR" here, as this is for general purpos
Sharu Jiang 2016/11/11 23:10:26 Done in rebase.
52 _DEFAULT_LOCAL_CACHE_DIR = os.path.join(_PREDATOR_ROOT_DIR,
52 '.culprit_finder', 'local_cache') 53 '.culprit_finder', 'local_cache')
53 54
54 55
55 class Cacher(object): 56 class Cacher(object):
56 """An interface to cache and retrieve data. 57 """An interface to cache and retrieve data.
57 58
58 Subclasses should implement the Get/Set functions. 59 Subclasses should implement the Get/Set functions.
59 TODO: Add a Delete function (default to no-op) if needed later. 60 TODO: Add a Delete function (default to no-op) if needed later.
60 """ 61 """
61 def Get(self, key): 62 def Get(self, key):
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 if not os.path.exists(path): 157 if not os.path.exists(path):
157 return None 158 return None
158 159
159 try: 160 try:
160 with open(path) as f: 161 with open(path) as f:
161 return pickle.loads(zlib.decompress(f.read())) 162 return pickle.loads(zlib.decompress(f.read()))
162 except Exception as e: # pragma: no cover. 163 except Exception as e: # pragma: no cover.
163 logging.error('Failed loading cache: %s', e) 164 logging.error('Failed loading cache: %s', e)
164 return None 165 return None
165 166
166 def Set(self, key, data): # pylint: disable=W 167 def Set(self, key, data, expire_time=0): # pylint: disable=W
167 with LocalCacher.lock: 168 with LocalCacher.lock:
168 try: 169 try:
169 with open(os.path.join(self.cache_dir, key), 'wb') as f: 170 with open(os.path.join(self.cache_dir, key), 'wb') as f:
170 f.write(zlib.compress(pickle.dumps(data))) 171 f.write(zlib.compress(pickle.dumps(data)))
171 except Exception as e: # pragma: no cover. 172 except Exception as e: # pragma: no cover.
172 logging.error('Failed setting cache for key %s: %s', key, e) 173 logging.error('Failed setting cache for key %s: %s', key, e)
173 174
174 175
175 def _DefaultKeyGenerator(func, args, kwargs): 176 def _DefaultKeyGenerator(func, args, kwargs):
176 """Generates a key from the function and arguments passed to it. 177 """Generates a key from the function and arguments passed to it.
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 except Exception: # pragma: no cover. 259 except Exception: # pragma: no cover.
259 logging.exception( 260 logging.exception(
260 'Failed to cache data for function %s.%s, args=%s, kwargs=%s', 261 'Failed to cache data for function %s.%s, args=%s, kwargs=%s',
261 func.__module__, func.__name__, repr(args), repr(kwargs)) 262 func.__module__, func.__name__, repr(args), repr(kwargs))
262 263
263 return result 264 return result
264 265
265 return Wrapped 266 return Wrapped
266 267
267 return Decorator 268 return Decorator
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698