@@ -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 )
0 commit comments