Skip to content

Commit 9d58243

Browse files
committed
fix API docs for lru store cache
1 parent 24f175f commit 9d58243

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

docs/api/storage.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ Storage (``zarr.storage``)
2323

2424
.. autoclass:: LRUStoreCache
2525

26-
.. automethod:: clear_values
27-
.. automethod:: clear_keys
26+
.. automethod:: invalidate
27+
.. automethod:: invalidate_values
28+
.. automethod:: invalidate_keys
2829

2930
.. autofunction:: init_array
3031
.. autofunction:: init_group

zarr/storage.py

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1732,8 +1732,14 @@ class LRUStoreCache(MutableMapping):
17321732
>>> root = zarr.group(store=cache)
17331733
>>> z = root['foo/bar/baz']
17341734
>>> from timeit import timeit
1735-
>>> timeit('print(z.tostring())', number=1) # first time is relatively slow
1736-
>>> timeit('print(z.tostring())', number=1) # second time is fast, uses cache
1735+
>>> # first data access is relatively slow, retrieved from store
1736+
... timeit('print(z[:].tostring())', number=1, globals=globals()) # doctest: +SKIP
1737+
b'Hello from the cloud!'
1738+
0.1081731989979744
1739+
>>> # second data access is faster, uses cache
1740+
... timeit('print(z[:].tostring())', number=1, globals=globals()) # doctest: +SKIP
1741+
b'Hello from the cloud!'
1742+
0.0009490990014455747
17371743
17381744
"""
17391745

@@ -1760,7 +1766,7 @@ def __setstate__(self, state):
17601766
self._mutex = Lock()
17611767

17621768
def __len__(self):
1763-
return len(self._store)
1769+
return len(self._keys())
17641770

17651771
def __iter__(self):
17661772
return self.keys()
@@ -1771,14 +1777,18 @@ def __contains__(self, key):
17711777
self._contains_cache = set(self._keys())
17721778
return key in self._contains_cache
17731779

1780+
def clear(self):
1781+
self._store.clear()
1782+
self.invalidate()
1783+
17741784
def keys(self):
17751785
with self._mutex:
1776-
return self._keys()
1786+
return iter(self._keys())
17771787

17781788
def _keys(self):
17791789
if self._keys_cache is None:
17801790
self._keys_cache = list(self._store.keys())
1781-
return iter(self._keys_cache)
1791+
return self._keys_cache
17821792

17831793
def listdir(self, path=None):
17841794
with self._mutex:
@@ -1816,22 +1826,28 @@ def _cache_value(self, key, value):
18161826
self._values_cache[key] = value
18171827
self._current_size += value_size
18181828

1819-
def clear_values(self):
1829+
def invalidate(self):
1830+
"""Completely clear the cache."""
1831+
with self._mutex:
1832+
self._values_cache.clear()
1833+
self._invalidate_keys()
1834+
1835+
def invalidate_values(self):
18201836
"""Clear the values cache."""
18211837
with self._mutex:
18221838
self._values_cache.clear()
18231839

1824-
def clear_keys(self):
1840+
def invalidate_keys(self):
18251841
"""Clear the keys cache."""
18261842
with self._mutex:
1827-
self._clear_keys()
1843+
self._invalidate_keys()
18281844

1829-
def _clear_keys(self):
1845+
def _invalidate_keys(self):
18301846
self._keys_cache = None
18311847
self._contains_cache = None
18321848
self._listdir_cache.clear()
18331849

1834-
def _clear_value(self, key):
1850+
def _invalidate_value(self, key):
18351851
if key in self._values_cache:
18361852
value = self._values_cache.pop(key)
18371853
self._current_size -= buffer_size(value)
@@ -1861,12 +1877,12 @@ def __getitem__(self, key):
18611877
def __setitem__(self, key, value):
18621878
self._store[key] = value
18631879
with self._mutex:
1864-
self._clear_keys()
1865-
self._clear_value(key)
1880+
self._invalidate_keys()
1881+
self._invalidate_value(key)
18661882
self._cache_value(key, value)
18671883

18681884
def __delitem__(self, key):
18691885
del self._store[key]
18701886
with self._mutex:
1871-
self._clear_keys()
1872-
self._clear_value(key)
1887+
self._invalidate_keys()
1888+
self._invalidate_value(key)

zarr/tests/test_storage.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -889,11 +889,15 @@ def test_cache_values_no_max_size(self):
889889
assert 2 == cache.hits
890890
assert 1 == cache.misses
891891

892-
# manually clear all cached values
893-
cache.clear_values()
892+
# manually invalidate all cached values
893+
cache.invalidate_values()
894894
assert b'zzz' == cache['foo']
895895
assert 2 == store.counter['__getitem__', 'foo']
896896
assert 2 == store.counter['__setitem__', 'foo']
897+
cache.invalidate()
898+
assert b'zzz' == cache['foo']
899+
assert 3 == store.counter['__getitem__', 'foo']
900+
assert 2 == store.counter['__setitem__', 'foo']
897901

898902
# test __delitem__
899903
del cache['foo']
@@ -1037,20 +1041,20 @@ def test_cache_keys(self):
10371041
assert keys == sorted(cache.keys())
10381042
assert 2 == store.counter['keys']
10391043

1040-
# manually clear keys
1041-
cache.clear_keys()
1044+
# manually invalidate keys
1045+
cache.invalidate_keys()
10421046
keys = sorted(cache.keys())
10431047
assert keys == ['bar', 'baz', 'foo']
10441048
assert 3 == store.counter['keys']
10451049
assert 0 == store.counter['__contains__', 'foo']
10461050
assert 0 == store.counter['__iter__']
1047-
cache.clear_keys()
1051+
cache.invalidate_keys()
10481052
keys = sorted(cache)
10491053
assert keys == ['bar', 'baz', 'foo']
10501054
assert 4 == store.counter['keys']
10511055
assert 0 == store.counter['__contains__', 'foo']
10521056
assert 0 == store.counter['__iter__']
1053-
cache.clear_keys()
1057+
cache.invalidate_keys()
10541058
assert 'foo' in cache
10551059
assert 5 == store.counter['keys']
10561060
assert 0 == store.counter['__contains__', 'foo']

0 commit comments

Comments
 (0)