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. 23, 2012, 2:15 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,25 @@
class cached(object):
"""
Decorator that caches a function's return value for a given number of seconds.
Wladimir Palant 2012/10/25 15:12:55 Still need a note here like: Note that this only
Andrey Novikov 2012/10/29 07:05:09 Done.
- Note that this can only be used with functions that take no arguments.
"""
-
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.wrapped = wrapped
+ return wrapped
def __repr__(self):
- """Return the function's docstring"""
- return self.func.__doc__
+ return repr(self.wrapped)
Wladimir Palant 2012/10/25 15:12:55 I don't think that we should forward this to our w
Andrey Novikov 2012/10/29 07:05:09 Done.
@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