@@ -128,6 +128,11 @@ def __init__(
128
128
self .expiry = expiry_time
129
129
self .compression = compression
130
130
131
+ # Size of cache in bytes. If None then the size is unknown and will be
132
+ # recalculated the next time cache_size() is called. On writes to the
133
+ # cache this is reset to None.
134
+ self ._cache_size = None
135
+
131
136
if same_names is not None and cache_mapper is not None :
132
137
raise ValueError (
133
138
"Cannot specify both same_names and cache_mapper in "
@@ -165,6 +170,17 @@ def _remove_tempdir(tempdir):
165
170
def _mkcache (self ):
166
171
os .makedirs (self .storage [- 1 ], exist_ok = True )
167
172
173
+ def cache_size (self ):
174
+ """Return size of cache in bytes.
175
+
176
+ If more than one cache directory is in use, only the size of the last
177
+ one (the writable cache directory) is returned.
178
+ """
179
+ if self ._cache_size is None :
180
+ cache_dir = self .storage [- 1 ]
181
+ self ._cache_size = filesystem ("file" ).du (cache_dir , withdirs = True )
182
+ return self ._cache_size
183
+
168
184
def load_cache (self ):
169
185
"""Read set of stored blocks from file"""
170
186
self ._metadata .load ()
@@ -176,6 +192,7 @@ def save_cache(self):
176
192
self ._mkcache ()
177
193
self ._metadata .save ()
178
194
self .last_cache = time .time ()
195
+ self ._cache_size = None
179
196
180
197
def _check_cache (self ):
181
198
"""Reload caches if time elapsed or any disappeared"""
@@ -202,6 +219,7 @@ def clear_cache(self):
202
219
"""
203
220
rmtree (self .storage [- 1 ])
204
221
self .load_cache ()
222
+ self ._cache_size = None
205
223
206
224
def clear_expired_cache (self , expiry_time = None ):
207
225
"""Remove all expired files and metadata from the cache
@@ -231,6 +249,8 @@ def clear_expired_cache(self, expiry_time=None):
231
249
rmtree (self .storage [- 1 ])
232
250
self .load_cache ()
233
251
252
+ self ._cache_size = None
253
+
234
254
def pop_from_cache (self , path ):
235
255
"""Remove cached version of given file
236
256
@@ -242,6 +262,7 @@ def pop_from_cache(self, path):
242
262
fn = self ._metadata .pop_file (path )
243
263
if fn is not None :
244
264
os .remove (fn )
265
+ self ._cache_size = None
245
266
246
267
def _open (
247
268
self ,
@@ -389,6 +410,7 @@ def __getattribute__(self, item):
389
410
"__hash__" ,
390
411
"__eq__" ,
391
412
"to_json" ,
413
+ "cache_size" ,
392
414
]:
393
415
# all the methods defined in this class. Note `open` here, since
394
416
# it calls `_open`, but is actually in superclass
@@ -535,6 +557,7 @@ def commit_many(self, open_files):
535
557
os .remove (f .name )
536
558
except FileNotFoundError :
537
559
pass
560
+ self ._cache_size = None
538
561
539
562
def _make_local_details (self , path ):
540
563
hash = self ._mapper (path )
@@ -704,6 +727,7 @@ def _open(self, path, mode="rb", **kwargs):
704
727
kwargs ["mode" ] = mode
705
728
706
729
self ._mkcache ()
730
+ self ._cache_size = None
707
731
if self .compression :
708
732
with self .fs ._open (path , ** kwargs ) as f , open (fn , "wb" ) as f2 :
709
733
if isinstance (f , AbstractBufferedFile ):
0 commit comments