2021-09-13 17:32:51 +00:00
|
|
|
from datetime import datetime, timezone
|
|
|
|
|
2021-12-27 18:30:17 +00:00
|
|
|
from cachetools import TTLCache
|
2021-09-13 17:32:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
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()
|
2024-05-12 14:56:05 +00:00
|
|
|
offset = ts % ttl
|
2021-09-13 17:32:51 +00:00
|
|
|
return ts - offset
|
|
|
|
|
|
|
|
# Init with smlight offset
|
2022-04-11 16:02:02 +00:00
|
|
|
super().__init__(maxsize=maxsize, ttl=ttl - 1e-5, timer=local_timer, getsizeof=getsizeof)
|