Skip to content

Cache downloads in a persistent, release-versioned directory - #62

Open
rafapereirabr wants to merge 2 commits into
mainfrom
cache
Open

rafapereirabr wants to merge 2 commits into
mainfrom
cache

Conversation

@rafapereirabr

Copy link
Copy Markdown
Contributor

Summary

aopdata used to cache downloads in tempdir(), so every R session downloaded the same files again. Files now go to a persistent cache that lives across sessions, versioned by data release.

  • Where: tools::R_user_dir("aopdata", "cache") by default, in a folder named after the data release (aopdata_data_release_v1.0.0). A new data release never reads files cached from an old one.
  • Old releases are deleted once per session; options(aopdata.keep_old_cache = TRUE) opts out.
  • New exports:
    • set_aopdata_cache_dir(path): creates an aopdata folder inside path and saves the setting across sessions; path = NULL resets to the default.
    • get_aopdata_cache_dir()
    • list_aopdata_cache()
    • delete_aopdata_cache()
  • Session-only cache: options(aopdata.cache_dir = <path>), e.g. tempdir().
  • Offline: a copy of metadata.csv is kept in the cache and used for the rest of the session when it can't be downloaded, so cached data can be read offline. The message has class aopdata_cache.
  • One release constant: the data release is a single constant, data_release in R/aopdata.R. It names the cache folder and builds both GitHub mirror URLs.

Things a reviewer should know

  • delete_aopdata_cache() never deletes user files. It removes aopdata's release folders, then the cache folder only if nothing else is left in it. Review found that a pre-existing user folder or file named aopdata inside the chosen path would otherwise be wiped.
  • Unversioned Ipea URLs. The Ipea URLs are not versioned, so the cache is keyed only on data_release. Any change to the hosted files requires bumping data_release and the GitHub release tag. This rule is added to .claude/rules/r-package-conventions.md.
  • Concurrent sessions: each process writes its own .part file. If another session saved the same file first (on Windows the rename fails while that file is open), the file counts as downloaded.
  • No reader changes: the read_*() signatures are unchanged and no cache argument was added.
  • Examples: the examples that run on CRAN (get_, list_) only read. The set_ and delete_ examples are guarded by interactive() and leave the user's settings as they were.
  • Tests: setup-cache.R and teardown-cache.R redirect tools::R_user_dir() to tempdir() for the whole suite, so tests never touch the real cache or config. devtools::check() builds vignettes with NOT_CRAN set, so those do write to the developer's real cache. That's expected.
  • Version: not bumped. NEWS has an aopdata (development version) heading.

Checks

  • devtools::test(): 258 passed, 0 failed (including the network tests)
  • R CMD check --as-cran: 0 errors, 0 warnings, 0 notes
  • Plan review: 3 adversarial rounds (minimalist, skeptic, CRAN) reached consensus
  • Code review:
    • Correctness lens: two MAJOR findings, fixed in rounds 1 and 2; dry in round 3
    • Package lens (r-package-reviewer): one MAJOR finding fixed in round 1; dry in round 2
  • Offline behaviour: mocked-offline tests cover the metadata fallback (one aopdata_cache message, 0 failures), an uncreatable cache dir, and the rename race
  • NEWS.md: updated; both READMEs have a new "Cache" section

🤖 Generated with Claude Code

rafapereirabr and others added 2 commits November 21, 2026 00:40
Files were cached in tempdir() and downloaded again in every R session.
They now go to tools::R_user_dir("aopdata", "cache") (or an 'aopdata'
folder inside a directory set with set_aopdata_cache_dir()), in a folder
named after the data release, so a new release never reads old files.
Old releases are deleted once per session (opt-out:
options(aopdata.keep_old_cache = TRUE)).

- New exports: set_aopdata_cache_dir(), get_aopdata_cache_dir(),
  list_aopdata_cache(), delete_aopdata_cache(); session-only override
  options(aopdata.cache_dir).
- A copy of metadata.csv is kept in the cache and used when it can't be
  downloaded, so cached data can be read offline (message class
  aopdata_cache).
- delete_aopdata_cache() removes only aopdata's release folders, and the
  cache folder only if it is then empty, so user files are never deleted.
- Per-process .part files; a file saved first by another session counts
  as downloaded.
- Tests redirect R_user_dir() to tempdir() via setup/teardown files.

Gates: devtools::test (cache|utils) 147 pass; R CMD check --as-cran
0 errors, 0 warnings, 0 notes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Describe the release-versioned cache under tools::R_user_dir(), the
data_release constant, test isolation via setup-cache.R, and the
maintainer rule that any change to hosted files needs a new data_release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant