i2mint / py2store

Tools to create simple and consistent interfaces to complicated and varied data sources.
MIT License
11 stars 2 forks source link

Caching `__iter__` #47

Closed thorwhalen closed 4 years ago

thorwhalen commented 4 years ago

Quite often we have a lot of keys, that we get from a remote data source, and don't want to have to ask for them again and again, having them be fetched, sent over the network, etc.

So we need caching.

But this caching is not the typical read caching, since it's __iter__ we want to cache, and that's a generator.

So we'll implement a store class decorator specialized for this.

The basic idea is this:

class MyStore(dict):
    _iter_cache = None
    def __iter__(self):
        if self._iter_cache is None:
            print('First time: Writing to cache')
            self._iter_cache = list(super().__iter__())
        yield from self._iter_cache

>>> class MyStore(dict):
...     _iter_cache = None
...     def __iter__(self):
...         if self._iter_cache is None:
...             print('First time: Writing to cache')
...             self._iter_cache = list(super().__iter__())
...         yield from self._iter_cache
...
>>>
>>> s = MyStore({'a': 'pi', 'b': 3.1415})
>>> t = list(s)
First time: Writing
>>> tt = list(s)  # but this won't print anything
thorwhalen commented 4 years ago

Pushed cache_iter to py2store.trans