Skip to content

Commit 923cc53

Browse files
committed
apply changes based on AI code review, and move tests into tests/test_experimental
1 parent 3d21514 commit 923cc53

File tree

3 files changed

+294
-211
lines changed

3 files changed

+294
-211
lines changed

docs/user-guide/experimental.md

Lines changed: 18 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ can be any Store implementation, providing flexibility in cache persistence:
2626

2727
```python exec="true" session="experimental" source="above" result="ansi"
2828
import zarr
29-
import zarr.storage
29+
from zarr.storage import LocalStore
3030
import numpy as np
31+
from tempfile import mkdtemp
3132
from zarr.experimental.cache_store import CacheStore
3233

3334
# 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)
3537
cache_store = zarr.storage.MemoryStore() # In-memory cache
3638
cached_store = CacheStore(
3739
store=source_store,
@@ -63,7 +65,7 @@ for _ in range(100):
6365
elapsed_cache = time.time() - start
6466

6567
# 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')
6769
start = time.time()
6870
for _ in range(100):
6971
_ = zarr_array_nocache[:]
@@ -75,45 +77,6 @@ speedup = elapsed_nocache / elapsed_cache
7577

7678
Cache effectiveness is particularly pronounced with repeated access to the same data chunks.
7779

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.
11780

11881
## Cache Configuration
11982

@@ -232,7 +195,10 @@ and use any store type for the cache backend:
232195
```python exec="true" session="experimental-memory-cache" source="above" result="ansi"
233196
from zarr.storage import LocalStore, MemoryStore
234197
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)
236202
cache_store = MemoryStore()
237203
cached_store = CacheStore(
238204
store=source_store,
@@ -241,40 +207,16 @@ cached_store = CacheStore(
241207
)
242208
```
243209

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-
271210
### Memory Store with Persistent Cache
272211

273212
```python exec="true" session="experimental-local-cache" source="above" result="ansi"
213+
from tempfile import mkdtemp
274214
from zarr.storage import MemoryStore, LocalStore
275215
from zarr.experimental.cache_store import CacheStore
216+
276217
memory_store = MemoryStore()
277-
persistent_cache = LocalStore('persistent_cache')
218+
local_store_path = mkdtemp(suffix='.zarr')
219+
persistent_cache = LocalStore(local_store_path)
278220
cached_store = CacheStore(
279221
store=memory_store,
280222
cache_store=persistent_cache,
@@ -290,14 +232,16 @@ of source and cache stores for your specific use case.
290232
Here's a complete example demonstrating cache effectiveness:
291233

292234
```python exec="true" session="experimental-final" source="above" result="ansi"
235+
import numpy as np
236+
import time
237+
from tempfile import mkdtemp
293238
import zarr
294239
import zarr.storage
295-
import time
296-
import numpy as np
297240
from zarr.experimental.cache_store import CacheStore
298241

299242
# 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)
301245
cache_store = zarr.storage.MemoryStore()
302246
cached_store = CacheStore(
303247
store=source_store,

0 commit comments

Comments
 (0)