Cache¶
The ilostat package caches downloaded data and metadata locally to avoid repeatedly downloading the same resources. The cache persists across Python sessions and is available to both the Bulk and SDMX interfaces.
Default cache location¶
No cache configuration is required. When cache_dir is not specified, the package uses the operating system's standard user cache location.
import ilostat
df = ilostat.get_df("UNE_TUNE_SEX_AGE_NB_A")
Inspect the cache location:
print(ilostat.get_cache_dir())
Custom cache directory¶
Use cache_dir to specify an alternative location.
=== "Windows"
```python
from pathlib import Path
cache = Path("C:/Temp/ilostat_cache")
```
=== "Cross-platform"
```python
from pathlib import Path
cache = Path.home() / "ilostat_cache"
```
Use the selected directory when retrieving resources:
=== "Bulk data"
```python
df = ilostat.get_df(
"UNE_TUNE_SEX_AGE_NB_A",
cache_dir=cache,
)
```
=== "SDMX structure"
```python
structure = ilostat.get_sdmx(
"UNE_TUNE_SEX_AGE_NB_A",
resource="structure",
cache_dir=cache,
)
```
Inspect the custom location:
print(ilostat.get_cache_dir(cache))
Local storage
Prefer a writable local directory, particularly when working with a project stored on a shared network drive. This can reduce filesystem overhead and avoid network-share permission issues.
Cache version management¶
Dataset-specific cached resources are versioned using the ILOSTAT
last.update timestamp.
The cache lifecycle is:
- Determine the resource version using the relevant metadata.
- Reuse the current version if it is already cached.
- Otherwise, download the required version.
- Remove obsolete cached versions of the same resource when a newer version is downloaded.
Cached resources are not automatically deleted when a Python session ends.
Refresh resources¶
Use refresh=True to refresh the relevant metadata before checking cached dataset resources.
=== "Default cache"
```python
df = ilostat.get_df(
"UNE_TUNE_SEX_AGE_NB_A",
refresh=True,
)
```
=== "Custom cache"
```python
df = ilostat.get_df(
"UNE_TUNE_SEX_AGE_NB_A",
cache_dir=cache,
refresh=True,
)
```
Refresh versus deletion
Refreshing metadata is not the same as deleting cached files.
A current cached dataset version can still be reused after the metadata check.
Use clean_cache() when you need to remove cached resources.
Dataflow update information¶
The SDMX dataflow catalogue exposes the ILOSTAT LAST_UPDATE value.
dataflows = ilostat.get_sdmx(resource="dataflow")
print(dataflows[["DATAFLOW_ID", "DATAFLOW_NAME", "LAST_UPDATE"]].head())
| Column | Description |
|---|---|
DATAFLOW_ID |
Dataflow identifier |
DATAFLOW_NAME |
Dataflow name |
LAST_UPDATE |
ILOSTAT update information |
This information can be used to identify datasets that have changed since a previous retrieval.
See SDMX dataflows for more information.
Clear the cache¶
Use clean_cache() to remove cached files and empty subdirectories while preserving the cache root directory.
=== "Default cache"
```python
ilostat.clean_cache()
```
=== "Custom cache"
```python
ilostat.clean_cache(cache_dir=cache)
```
Cache cleaning
Check the cache location before cleaning it. Use a dedicated cache directory rather than a directory containing unrelated files. Subsequent requests may need to download resources again.
Recommended usage¶
| Situation | Recommendation |
|---|---|
| Routine analysis | Use the default cache |
| Project on a shared drive | Prefer a writable local cache |
| Check for updated datasets | Use refresh=True |
| Inspect dataset update information | Retrieve the SDMX dataflow catalogue |
| Reclaim cache storage | Use clean_cache() |