Use proper clock if possible
This commit is contained in:
parent
abe0b1a647
commit
f8db46b406
|
@ -1,7 +1,12 @@
|
|||
# vim:fileencoding=utf-8:noet
|
||||
|
||||
from functools import wraps
|
||||
import time
|
||||
try:
|
||||
# Python>=3.3, the only valid clock source for this job
|
||||
from time import monotonic as time
|
||||
except ImportError:
|
||||
# System time, is affected by clock updates.
|
||||
from time import time
|
||||
|
||||
|
||||
def default_cache_key(**kwargs):
|
||||
|
@ -28,10 +33,13 @@ class memoize(object):
|
|||
cached = self.cache.get(key, None)
|
||||
except TypeError:
|
||||
return func(**kwargs)
|
||||
if cached is None or time.time() - cached['time'] > self.timeout:
|
||||
# Handle case when time() appears to be less then cached['time'] due
|
||||
# to clock updates. Not applicable for monotonic clock, but this
|
||||
# case is currently rare.
|
||||
if cached is None or not (cached['time'] < time() < cached['time'] + self.timeout):
|
||||
cached = self.cache[key] = {
|
||||
'result': func(**kwargs),
|
||||
'time': time.time(),
|
||||
'time': time(),
|
||||
}
|
||||
return cached['result']
|
||||
return decorated_function
|
||||
|
|
Loading…
Reference in New Issue