commit
e4ec5679a1
@ -0,0 +1,19 @@
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from cachetools.ttl import TTLCache
|
||||
|
||||
|
||||
class PeriodicCache(TTLCache):
|
||||
"""
|
||||
Special cache that expires at "straight" times
|
||||
A timer with ttl of 3600 (1h) will expire at every full hour (:00).
|
||||
"""
|
||||
|
||||
def __init__(self, maxsize, ttl, getsizeof=None):
|
||||
def local_timer():
|
||||
ts = datetime.now(timezone.utc).timestamp()
|
||||
offset = (ts % ttl)
|
||||
return ts - offset
|
||||
|
||||
# Init with smlight offset
|
||||
super().__init__(maxsize=maxsize, ttl=ttl-1e-5, timer=local_timer, getsizeof=getsizeof)
|
||||
@ -0,0 +1,32 @@
|
||||
import time_machine
|
||||
|
||||
from freqtrade.configuration import PeriodicCache
|
||||
|
||||
|
||||
def test_ttl_cache():
|
||||
|
||||
with time_machine.travel("2021-09-01 05:00:00 +00:00") as t:
|
||||
|
||||
cache = PeriodicCache(5, ttl=60)
|
||||
cache1h = PeriodicCache(5, ttl=3600)
|
||||
|
||||
assert cache.timer() == 1630472400.0
|
||||
cache['a'] = 1235
|
||||
cache1h['a'] = 555123
|
||||
assert 'a' in cache
|
||||
assert 'a' in cache1h
|
||||
|
||||
t.move_to("2021-09-01 05:00:59 +00:00")
|
||||
assert 'a' in cache
|
||||
assert 'a' in cache1h
|
||||
|
||||
# Cache expired
|
||||
t.move_to("2021-09-01 05:01:00 +00:00")
|
||||
assert 'a' not in cache
|
||||
assert 'a' in cache1h
|
||||
|
||||
t.move_to("2021-09-01 05:59:59 +00:00")
|
||||
assert 'a' in cache1h
|
||||
|
||||
t.move_to("2021-09-01 06:00:00 +00:00")
|
||||
assert 'a' not in cache1h
|
||||
Loading…
Reference in new issue