@@ -157,6 +157,13 @@ def __init__(
157157 if self .engine == "pyarrow" and find_spec ("pyarrow" ) is None :
158158 raise ImportError ("engine choice `pyarrow` is not installed." )
159159
160+ # Apply `lru_cache` decorator manually per instance.
161+ # This way `self` reference is not held on class level.
162+ # WARNING: However, this means that self and its members are not reflected
163+ # in the cache key, so we expect they won't be mutated once a value is cached.
164+ self .listdir = lru_cache ()(self .listdir )
165+ self ._key_to_record = lru_cache (maxsize = 4096 )(self ._key_to_record )
166+
160167 def __getattr__ (self , item ):
161168 if item in ("_items" , "record_size" , "zmetadata" ):
162169 self .setup ()
@@ -219,7 +226,6 @@ def create(root, storage_options=None, fs=None, record_size=10000, **kwargs):
219226 fs .pipe ("/" .join ([root , ".zmetadata" ]), json .dumps (met ).encode ())
220227 return LazyReferenceMapper (root , fs , ** kwargs )
221228
222- @lru_cache
223229 def listdir (self ):
224230 """List top-level directories"""
225231 dirs = (p .rsplit ("/" , 1 )[0 ] for p in self .zmetadata if not p .startswith (".z" ))
@@ -331,7 +337,6 @@ def _load_one_key(self, key):
331337 # URL, offset, size
332338 return selection [:3 ]
333339
334- @lru_cache (4096 )
335340 def _key_to_record (self , key ):
336341 """Details needed to construct a reference for one key"""
337342 field , chunk = key .rsplit ("/" , 1 )
0 commit comments