@@ -14,6 +14,8 @@ class CachingFileSystem(FS):
14
14
def __init__ (self , * args , ** kwargs ):
15
15
if cache_storage == "TMP" :
16
16
storage = tempfile .mkdtemp ()
17
+ else :
18
+ storage = cache_storage
17
19
os .makedirs (storage , exist_ok = True )
18
20
self .storage = storage
19
21
self .load_cache ()
@@ -24,11 +26,18 @@ def load_cache(self):
24
26
if os .path .exists (fn ):
25
27
with open (fn ) as f :
26
28
self .cache = ujson .load (f )
29
+ for c in self .cache .values ():
30
+ if isinstance (c ['blocks' ], list ):
31
+ c ['blocks' ] = set (c ['blocks' ])
27
32
else :
28
33
self .cache = {}
29
34
30
35
def save_cache (self ):
31
36
fn = os .path .join (self .storage , 'cache.json' )
37
+ cache = {k : v .copy () for k , v in self .cache .items ()}
38
+ for c in cache .values ():
39
+ if isinstance (c ['blocks' ], set ):
40
+ c ['blocks' ] = list (c ['blocks' ])
32
41
with open (fn , 'w' ) as f :
33
42
ujson .dump (self .cache , f )
34
43
@@ -40,7 +49,7 @@ def _open(self, path, mode='rb', **kwargs):
40
49
hash , blocks = detail ['fn' ], detail ['blocks' ]
41
50
fn = os .path .join (self .storage , hash )
42
51
if blocks is True :
43
- return open (fn )
52
+ return open (fn , 'rb' )
44
53
else :
45
54
blocks = set (blocks )
46
55
else :
@@ -56,8 +65,10 @@ def _open(self, path, mode='rb', **kwargs):
56
65
return f
57
66
58
67
def close_and_update (self , f , close ):
59
- if len (self .cache [f .path ]['blocks' ]) * f .blocksize >= f .size :
60
- self .cache [f .path ]['blocks' ] = True
68
+ c = self .cache [f .path ]
69
+ if (c ['blocks' ] is not True
70
+ and len (['blocks' ]) * f .blocksize >= f .size ):
71
+ c ['blocks' ] = True
61
72
self .save_cache ()
62
73
close ()
63
74
0 commit comments