mirror of
https://github.com/powerline/powerline.git
synced 2025-07-07 14:04:57 +02:00
Use proper clock if possible
This commit is contained in:
parent
abe0b1a647
commit
f8db46b406
@ -1,7 +1,12 @@
|
|||||||
# vim:fileencoding=utf-8:noet
|
# vim:fileencoding=utf-8:noet
|
||||||
|
|
||||||
from functools import wraps
|
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):
|
def default_cache_key(**kwargs):
|
||||||
@ -28,10 +33,13 @@ class memoize(object):
|
|||||||
cached = self.cache.get(key, None)
|
cached = self.cache.get(key, None)
|
||||||
except TypeError:
|
except TypeError:
|
||||||
return func(**kwargs)
|
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] = {
|
cached = self.cache[key] = {
|
||||||
'result': func(**kwargs),
|
'result': func(**kwargs),
|
||||||
'time': time.time(),
|
'time': time(),
|
||||||
}
|
}
|
||||||
return cached['result']
|
return cached['result']
|
||||||
return decorated_function
|
return decorated_function
|
||||||
|
Loading…
x
Reference in New Issue
Block a user