forked from freqtrade/freqtrade
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request freqtrade#5574 from freqtrade/agefilter_cache
Agefilter cache
- Loading branch information
Showing
6 changed files
with
122 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |