Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: sitescripts/utils.py

Issue 8625042: Reports - user usefullness (Closed)
Patch Set: Reports - user usefulness Created Oct. 29, 2012, 12:35 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « sitescripts/reports/web/updateReport.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sitescripts/utils.py
===================================================================
--- a/sitescripts/utils.py
+++ b/sitescripts/utils.py
@@ -14,31 +14,27 @@
class cached(object):
"""
Decorator that caches a function's return value for a given number of seconds.
- Note that this can only be used with functions that take no arguments.
+ Note that this only works if the string representation of the parameters is
+ always unique.
"""
-
def __init__(self, timeout):
self.timeout = timeout
- self.func = None
- self.lastUpdate = None
- self.lastResult = None
+ self.lastResult = {}
+ self.lastUpdate = {}
- def __call__(self, *args):
- if len(args) == 1:
- # We got called with the function to be decorated - remember the function
- # and return the same object again
- self.func = args[0]
- return self
- else:
+ def __call__(self, func):
+ def wrapped(*args, **kwargs):
+ key = str(kwargs)
currentTime = time()
- if self.lastUpdate == None or currentTime - self.lastUpdate > self.timeout:
- self.lastResult = self.func()
- self.lastUpdate = currentTime
- return self.lastResult
+ if (args, key) not in self.lastUpdate or currentTime - self.lastUpdate[args, key] > self.timeout:
+ self.lastResult[args, key] = func(*args, **kwargs)
+ self.lastUpdate[args, key] = currentTime
+ return self.lastResult[args, key]
+ self.func = func
+ return wrapped
def __repr__(self):
- """Return the function's docstring"""
- return self.func.__doc__
+ return repr(self.func)
@cached(3600)
def get_config():
« no previous file with comments | « sitescripts/reports/web/updateReport.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld