@@ -1732,8 +1732,14 @@ class LRUStoreCache(MutableMapping):
1732
1732
>>> root = zarr.group(store=cache)
1733
1733
>>> z = root['foo/bar/baz']
1734
1734
>>> 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
1737
1743
1738
1744
"""
1739
1745
@@ -1760,7 +1766,7 @@ def __setstate__(self, state):
1760
1766
self ._mutex = Lock ()
1761
1767
1762
1768
def __len__ (self ):
1763
- return len (self ._store )
1769
+ return len (self ._keys () )
1764
1770
1765
1771
def __iter__ (self ):
1766
1772
return self .keys ()
@@ -1771,14 +1777,18 @@ def __contains__(self, key):
1771
1777
self ._contains_cache = set (self ._keys ())
1772
1778
return key in self ._contains_cache
1773
1779
1780
+ def clear (self ):
1781
+ self ._store .clear ()
1782
+ self .invalidate ()
1783
+
1774
1784
def keys (self ):
1775
1785
with self ._mutex :
1776
- return self ._keys ()
1786
+ return iter ( self ._keys () )
1777
1787
1778
1788
def _keys (self ):
1779
1789
if self ._keys_cache is None :
1780
1790
self ._keys_cache = list (self ._store .keys ())
1781
- return iter ( self ._keys_cache )
1791
+ return self ._keys_cache
1782
1792
1783
1793
def listdir (self , path = None ):
1784
1794
with self ._mutex :
@@ -1816,22 +1826,28 @@ def _cache_value(self, key, value):
1816
1826
self ._values_cache [key ] = value
1817
1827
self ._current_size += value_size
1818
1828
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 ):
1820
1836
"""Clear the values cache."""
1821
1837
with self ._mutex :
1822
1838
self ._values_cache .clear ()
1823
1839
1824
- def clear_keys (self ):
1840
+ def invalidate_keys (self ):
1825
1841
"""Clear the keys cache."""
1826
1842
with self ._mutex :
1827
- self ._clear_keys ()
1843
+ self ._invalidate_keys ()
1828
1844
1829
- def _clear_keys (self ):
1845
+ def _invalidate_keys (self ):
1830
1846
self ._keys_cache = None
1831
1847
self ._contains_cache = None
1832
1848
self ._listdir_cache .clear ()
1833
1849
1834
- def _clear_value (self , key ):
1850
+ def _invalidate_value (self , key ):
1835
1851
if key in self ._values_cache :
1836
1852
value = self ._values_cache .pop (key )
1837
1853
self ._current_size -= buffer_size (value )
@@ -1861,12 +1877,12 @@ def __getitem__(self, key):
1861
1877
def __setitem__ (self , key , value ):
1862
1878
self ._store [key ] = value
1863
1879
with self ._mutex :
1864
- self ._clear_keys ()
1865
- self ._clear_value (key )
1880
+ self ._invalidate_keys ()
1881
+ self ._invalidate_value (key )
1866
1882
self ._cache_value (key , value )
1867
1883
1868
1884
def __delitem__ (self , key ):
1869
1885
del self ._store [key ]
1870
1886
with self ._mutex :
1871
- self ._clear_keys ()
1872
- self ._clear_value (key )
1887
+ self ._invalidate_keys ()
1888
+ self ._invalidate_value (key )
0 commit comments