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

Unified Diff: tools/telemetry/telemetry/decorators.py

Issue 425613002: [Telemetry] Fix @Cache to maintain cache for life of instance for bound methods (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 5 months 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 | « no previous file | tools/telemetry/telemetry/unittest/decorators_unittest.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/telemetry/telemetry/decorators.py
diff --git a/tools/telemetry/telemetry/decorators.py b/tools/telemetry/telemetry/decorators.py
index 228c987ddf539bdb58096561e4acf588fcff91dd..0ac140870ced95ee7ba583ec2d8e9138c3034be1 100644
--- a/tools/telemetry/telemetry/decorators.py
+++ b/tools/telemetry/telemetry/decorators.py
@@ -1,8 +1,10 @@
# Copyright 2014 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.
+# pylint: disable=W0212
import functools
+import inspect
import types
@@ -16,15 +18,18 @@ def Cache(obj):
If CreateFoo() accepts parameters, a separate cached value is maintained
for each unique parameter combination.
- """
- cache = obj.__cache = {}
+ Cached methods maintain their cache for the lifetime of the /instance/, while
+ cached functions maintain their cache for the lifetime of the /module/.
+ """
@functools.wraps(obj)
def Cacher(*args, **kwargs):
- key = str(args) + str(kwargs)
- if key not in cache:
- cache[key] = obj(*args, **kwargs)
- return cache[key]
+ cacher = args[0] if inspect.getargspec(obj).args[:1] == ['self'] else obj
+ cacher.__cache = cacher.__cache if hasattr(cacher, '__cache') else {}
+ key = str(obj) + str(args) + str(kwargs)
+ if key not in cacher.__cache:
+ cacher.__cache[key] = obj(*args, **kwargs)
+ return cacher.__cache[key]
return Cacher
@@ -87,7 +92,6 @@ def Enabled(*args):
return _Enabled
-# pylint: disable=W0212
def IsEnabled(test, possible_browser):
"""Returns True iff |test| is enabled given the |possible_browser|.
« no previous file with comments | « no previous file | tools/telemetry/telemetry/unittest/decorators_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698