added CacheView (single cache per source, one view on that cache per plot)

This commit is contained in:
2021-06-04 16:00:13 +02:00
parent dcaef9506e
commit 0e25adaa18

View File

@@ -1,4 +1,5 @@
from collections import deque
import itertools
from collections import deque, defaultdict
from time import time
@@ -6,6 +7,7 @@ class Cacher:
def __init__(self):
self.caches = {}
self.views = defaultdict(list)
def add_source(self, src, size=100):
@@ -15,15 +17,18 @@ class Cacher:
caches = self.caches
try:
return caches[name]
cache = caches[name]
except KeyError:
caches[name] = cache = Cache(size=size)
src.add_callback(cache.callback_append)
return cache
view = CacheView(cache, size=size)
self.views[name].append(view)
return view
def __repr__(self):
return repr(self.caches)
return repr(self.views)
@@ -66,12 +71,40 @@ class Cache:
class CacheView:
def __init__(self, cache, size=100):
self.cache = cache
self.size = size
def __iter__(self):
return iter(self.data)
@property
def data(self):
return tuple(get_last(i, self.size) for i in self.cache.data)
def __repr__(self):
return f"CacheView ({self.size}) on {self.cache}"
def set_size(self, size):
self.size = size
if size > self.cache.size:
print("Adjust cache size to view size...")
self.cache.set_size(size)
def get_last(d, n):
stop = len(d)
start = stop - n
start = max(start, 0)
return list(itertools.islice(d, start, stop))
# need to create object here to share it between sessions
cacher = Cacher()
#TODO: copy data from existing cache for the same name but different size