---
sidebar:
  label: roboto.storage.cache
  order: 1
title: roboto.storage.cache
---
## Module Contents

### COLUMN_COUNT_LOCAL_CACHE_THRESHOLD

```python
roboto.storage.cache.COLUMN_COUNT_LOCAL_CACHE_THRESHOLD = 10
```

`from roboto.storage.cache import COLUMN_COUNT_LOCAL_CACHE_THRESHOLD`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/cache.py#L22-L22)

Number of columns at or above which a `CachePolicy.ADAPTIVE` read downloads a file.

Reading over HTTP fetches only the requested columns, but requests each column's bytes separately, so it slows as the number of columns grows. Below 10 columns, reading over HTTP is faster; from 10 on, downloading the whole file once is.

### CachePolicy

```python
class roboto.storage.cache.CachePolicy
```

`from roboto.storage import CachePolicy`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/cache.py#L30-L47)

Bases: `str`, `enum.Enum`

Cache policy of a topic data read: whether each data file is downloaded to the local cache before it is read.

Applies to Parquet data files. MCAP data files are always read over HTTP, whatever the policy.

**Attributes**

- **CachePolicy.ADAPTIVE** = `'adaptive'`: A copy already in the local cache is read from there; otherwise the column count picks download or HTTP.

  The file is downloaded when the read needs at least 10 columns (`COLUMN_COUNT_LOCAL_CACHE_THRESHOLD`), and read over HTTP when it needs fewer.

- **CachePolicy.ALWAYS** = `'always'`: Each file is downloaded to the local cache before it is read, unless a copy is already there.

- **CachePolicy.NEVER** = `'never'`: Every file is read over HTTP; the local cache is neither read nor written.

### FetchMode

```python
class roboto.storage.cache.FetchMode(*args, **kwds)
```

`from roboto.storage.cache import FetchMode`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/cache.py#L50-L64)

Bases: `enum.Enum`

Source a remote data file is opened from, as `choose_fetch_mode` chooses it.

[`get_data()`](/docs/reference/python-sdk/roboto/domain/topics/topic#roboto.domain.topics.topic.Topic.get_data) and [`open_parquet_file()`](/docs/reference/python-sdk/roboto/formats/parquet/fetch#roboto.formats.parquet.fetch.open_parquet_file) open Parquet files through it, so most code never uses it.

**Attributes**

- **FetchMode.CACHED**: Copy already in the local cache.
- **FetchMode.DOWNLOAD**: Copy downloaded to the local cache for this read.
- **FetchMode.STREAM**: Remote file, read over HTTP without writing to disk.

### cached_file_is_current()

```python
def roboto.storage.cache.cached_file_is_current(
    path: pathlib.Path,
    expected_size: Optional[int],
) -> bool
```

`from roboto.storage.cache import cached_file_is_current`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/cache.py#L96-L115)

Check whether the file at a local cache path can be read instead of downloaded again.

[`open_parquet_file()`](/docs/reference/python-sdk/roboto/formats/parquet/fetch#roboto.formats.parquet.fetch.open_parquet_file) checks a cached copy through it before reading it, so most code never calls it. The file is current when it exists and its size equals `expected_size`, or when `expected_size` is `None`. A file of a different size no longer matches the remote file, so it is downloaded again.

**Parameters**

- **path** (`pathlib.Path`): Local cache path to check.
- **expected_size** (`Optional[int]`): Current size of the remote file, in bytes. `None` accepts a cached file of any size.

**Returns**

- `bool`: `True` when the cached file can be read; `False` when it is missing or its size differs.

### choose_fetch_mode()

```python
def roboto.storage.cache.choose_fetch_mode(
    policy: CachePolicy,
    already_cached: bool,
    estimated_column_count: int,
) -> FetchMode
```

`from roboto.storage.cache import choose_fetch_mode`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/cache.py#L67-L93)

Choose the source to open a remote data file from, under a cache policy.

[`get_data()`](/docs/reference/python-sdk/roboto/domain/topics/topic#roboto.domain.topics.topic.Topic.get_data) and [`open_parquet_file()`](/docs/reference/python-sdk/roboto/formats/parquet/fetch#roboto.formats.parquet.fetch.open_parquet_file) choose how to open each Parquet file through it, so most code never calls it. A copy already in the local cache is used under every policy except `CachePolicy.NEVER`, however many columns the read needs, because opening it makes no request.

**Parameters**

- **policy** (`CachePolicy`): Cache policy of the read.
- **already_cached** (`bool`): Whether a usable copy of the file is already in the local cache.
- **estimated_column_count** (`int`): Number of columns the read is expected to need. Under `ADAPTIVE`, compared with `COLUMN_COUNT_LOCAL_CACHE_THRESHOLD`.

**Returns**

- `FetchMode`

### download_to_cache()

```python
def roboto.storage.cache.download_to_cache(
    url_provider: Callable[[], str],
    outfile: pathlib.Path,
    expected_size: Optional[int] = None,
) -> None
```

`from roboto.storage.cache import download_to_cache`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/cache.py#L150-L207)

Download a remote file to a local cache path, so that no reader ever finds a partial file there.

[`get_data()`](/docs/reference/python-sdk/roboto/domain/topics/topic#roboto.domain.topics.topic.Topic.get_data) and [`open_parquet_file()`](/docs/reference/python-sdk/roboto/formats/parquet/fetch#roboto.formats.parquet.fetch.open_parquet_file) download files to the local cache through it, so most code never calls it. It works in these steps:

1. Wait for this process's lock on `outfile`, so threads that download the same file download it once.
2. Return if a current copy is already at `outfile` (see `cached_file_is_current`); another thread may have downloaded it during the wait.
3. Create `outfile`'s directory if needed, and download to a uniquely named `.part` file beside it.
4. When `expected_size` is given, compare the `.part` file's size with it. `urllib.request.urlretrieve` does not detect a truncated body when the response has no `Content-Length`, so this check keeps a truncated download out of the cache.
5. Rename the `.part` file to `outfile` with `os.replace`, which replaces it in one step on POSIX and Windows. When two processes download the same file, the later rename replaces one complete file with another.

If the download, the size check or the rename raises, including on `KeyboardInterrupt`, the `.part` file is deleted, `outfile` is left as it was, and the exception propagates.

**Parameters**

- **url_provider** (`Callable[[], str]`): Function that returns a signed URL for the file. Called only when the file is downloaded, so no URL is signed for a file already in the cache.
- **outfile** (`pathlib.Path`): Local cache path to download the file to.
- **expected_size** (`Optional[int]`): Size of the remote file in bytes, as Roboto reports it. `None` skips the size check and accepts any copy already at `outfile`.

**Raises**

- `ValueError`: `expected_size` is given and the downloaded file has a different size.
- `urllib.error.URLError`: The download request fails.

**Returns**

- `None`

### get_download_lock()

```python
def roboto.storage.cache.get_download_lock(key: str) -> threading.Lock
```

`from roboto.storage.cache import get_download_lock`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/cache.py#L133-L147)

Return this process's download lock for a cache path, creating it on first use.

`download_to_cache` takes its lock through it, so most code never calls it. Threads that download the same file share the lock, so they wait for one another.

**Parameters**

- **key** (`str`): Cache path of the file, as a string.

**Returns**

- `threading.Lock`

### logger

```python
roboto.storage.cache.logger
```

`from roboto.storage.cache import logger`

[Source](https://github.com/roboto-ai/roboto-python-sdk/blob/main/src/roboto/storage/cache.py#L20-L20)
