-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreference_cache.py
More file actions
42 lines (32 loc) · 1.55 KB
/
Copy pathreference_cache.py
File metadata and controls
42 lines (32 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""stdlib stand-in for the private project's book cache.
The trading project pushes published tops into a shared cache consumed by the
scanner. For the public stream this in-memory version keeps the exact call
contract used by KuCoinFastBookStream:
update_book(pair, asks_top, bids_top, exchange, account,
timestamp=None, _presorted=True) -> bool
invalidate_book(pair, exchange, account) -> None
and adds get_book() so examples and tests can read the latest published top.
"""
from __future__ import annotations
import time
class ReferenceCache:
__slots__ = ("_books", "invalidations")
def __init__(self) -> None:
self._books: dict = {}
# Audit trail of invalidations: (exchange, account, pair). Tests use
# it; a live process can watch it for gap storms.
self.invalidations: list = []
def update_book(self, pair, asks_top, bids_top, exchange, account,
timestamp=None, _presorted=False):
self._books[(exchange, account, pair)] = {
"asks": [(float(price), float(size)) for price, size in asks_top],
"bids": [(float(price), float(size)) for price, size in bids_top],
"timestamp": timestamp,
"updated_mono": time.monotonic(),
}
return True
def invalidate_book(self, pair, exchange, account):
self.invalidations.append((exchange, account, pair))
self._books.pop((exchange, account, pair), None)
def get_book(self, exchange, account, pair):
return self._books.get((exchange, account, pair))