@@ -26,12 +26,14 @@ can be any Store implementation, providing flexibility in cache persistence:
26
26
27
27
``` python exec="true" session="experimental" source="above" result="ansi"
28
28
import zarr
29
- import zarr.storage
29
+ from zarr.storage import LocalStore
30
30
import numpy as np
31
+ from tempfile import mkdtemp
31
32
from zarr.experimental.cache_store import CacheStore
32
33
33
34
# Create a local store and a separate cache store
34
- source_store = zarr.storage.LocalStore(' test.zarr' )
35
+ local_store_path = mkdtemp(suffix = ' .zarr' )
36
+ source_store = LocalStore(local_store_path)
35
37
cache_store = zarr.storage.MemoryStore() # In-memory cache
36
38
cached_store = CacheStore(
37
39
store = source_store,
@@ -63,7 +65,7 @@ for _ in range(100):
63
65
elapsed_cache = time.time() - start
64
66
65
67
# Compare with direct store access (without cache)
66
- zarr_array_nocache = zarr.open(' test.zarr ' , mode = ' r' )
68
+ zarr_array_nocache = zarr.open(local_store_path , mode = ' r' )
67
69
start = time.time()
68
70
for _ in range (100 ):
69
71
_ = zarr_array_nocache[:]
@@ -75,45 +77,6 @@ speedup = elapsed_nocache / elapsed_cache
75
77
76
78
Cache effectiveness is particularly pronounced with repeated access to the same data chunks.
77
79
78
- ## Remote Store Caching
79
-
80
- The CacheStore is most beneficial when used with remote stores where network latency
81
- is a significant factor. You can use different store types for source and cache:
82
-
83
- ``` python
84
- # This example shows remote store setup but requires network access
85
- # from zarr.storage import FsspecStore, LocalStore
86
-
87
- # # Create a remote store (S3 example) - for demonstration only
88
- # remote_store = FsspecStore.from_url('s3://bucket/data.zarr', storage_options={'anon': True})
89
-
90
- # # Use a local store for persistent caching
91
- # local_cache_store = LocalStore('cache_data')
92
-
93
- # # Create cached store with persistent local cache
94
- # cached_store = CacheStore(
95
- # store=remote_store,
96
- # cache_store=local_cache_store,
97
- # max_size=512*1024*1024 # 512MB cache
98
- # )
99
-
100
- # # Open array through cached store
101
- # z = zarr.open(cached_store)
102
-
103
- # For demonstration, use local stores instead
104
- from zarr.storage import LocalStore
105
- local_source = LocalStore(' remote_data.zarr' )
106
- local_cache = LocalStore(' cache_data' )
107
- cached_store = CacheStore(
108
- store = local_source,
109
- cache_store = local_cache,
110
- max_size = 512 * 1024 * 1024 # 512MB cache
111
- )
112
- ```
113
-
114
- The first access to any chunk will be slow (network retrieval), but subsequent accesses
115
- to the same chunk will be served from the local cache, providing dramatic speedup.
116
- The cache persists between sessions when using a LocalStore for the cache backend.
117
80
118
81
## Cache Configuration
119
82
@@ -232,7 +195,10 @@ and use any store type for the cache backend:
232
195
``` python exec="true" session="experimental-memory-cache" source="above" result="ansi"
233
196
from zarr.storage import LocalStore, MemoryStore
234
197
from zarr.experimental.cache_store import CacheStore
235
- source_store = LocalStore(' data.zarr' )
198
+ from tempfile import mkdtemp
199
+
200
+ local_store_path = mkdtemp(suffix = ' .zarr' )
201
+ source_store = LocalStore(local_store_path)
236
202
cache_store = MemoryStore()
237
203
cached_store = CacheStore(
238
204
store = source_store,
@@ -241,40 +207,16 @@ cached_store = CacheStore(
241
207
)
242
208
```
243
209
244
- ### Remote Store with Local Cache
245
-
246
- ``` python exec="true" session="experimental-remote-cache" source="above" result="ansi"
247
- # Remote store example (commented out as it requires network access)
248
- # from zarr.storage import FsspecStore, LocalStore
249
- # remote_store = FsspecStore.from_url('s3://bucket/data.zarr', storage_options={'anon': True})
250
- # local_cache = LocalStore('local_cache')
251
- # cached_store = CacheStore(
252
- # store=remote_store,
253
- # cache_store=local_cache,
254
- # max_size=1024*1024*1024,
255
- # max_age_seconds=3600
256
- # )
257
-
258
- # Local store example for demonstration
259
- from zarr.storage import LocalStore
260
- from zarr.experimental.cache_store import CacheStore
261
- remote_like_store = LocalStore(' remote_like_data.zarr' )
262
- local_cache = LocalStore(' local_cache' )
263
- cached_store = CacheStore(
264
- store = remote_like_store,
265
- cache_store = local_cache,
266
- max_size = 1024 * 1024 * 1024 ,
267
- max_age_seconds = 3600
268
- )
269
- ```
270
-
271
210
### Memory Store with Persistent Cache
272
211
273
212
``` python exec="true" session="experimental-local-cache" source="above" result="ansi"
213
+ from tempfile import mkdtemp
274
214
from zarr.storage import MemoryStore, LocalStore
275
215
from zarr.experimental.cache_store import CacheStore
216
+
276
217
memory_store = MemoryStore()
277
- persistent_cache = LocalStore(' persistent_cache' )
218
+ local_store_path = mkdtemp(suffix = ' .zarr' )
219
+ persistent_cache = LocalStore(local_store_path)
278
220
cached_store = CacheStore(
279
221
store = memory_store,
280
222
cache_store = persistent_cache,
@@ -290,14 +232,16 @@ of source and cache stores for your specific use case.
290
232
Here's a complete example demonstrating cache effectiveness:
291
233
292
234
``` python exec="true" session="experimental-final" source="above" result="ansi"
235
+ import numpy as np
236
+ import time
237
+ from tempfile import mkdtemp
293
238
import zarr
294
239
import zarr.storage
295
- import time
296
- import numpy as np
297
240
from zarr.experimental.cache_store import CacheStore
298
241
299
242
# Create test data with dual-store cache
300
- source_store = zarr.storage.LocalStore(' benchmark.zarr' )
243
+ local_store_path = mkdtemp(suffix = ' .zarr' )
244
+ source_store = zarr.storage.LocalStore(local_store_path)
301
245
cache_store = zarr.storage.MemoryStore()
302
246
cached_store = CacheStore(
303
247
store = source_store,
0 commit comments