Skip to content

feat: reuse the downloaded feed per instance for 60 seconds, add refresh() - #53

Merged
Baspa merged 15 commits into
mainfrom
feat/memoize-feed
Nov 13, 2026
Merged

Baspa merged 15 commits into
mainfrom
feat/memoize-feed

Conversation

@Baspa

@Baspa Baspa commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner

What

Every forecast(), actualForecastForStation(), stations(), nearestStation(), sunrise() and sunset() call used to download and decode the whole JSON feed again (~35 KB).

  • The decoded feed is reused by the same instance for 60 seconds. That's long enough to collapse the calls in one request/command, and short enough that a long-running process (queue worker, daemon) still sees fresh data; Buienradar updates the measurements every 10 minutes.
  • refresh() forces a new download ($buienradar->refresh()->forecast()).
  • Optional second constructor argument cacheSeconds (default 60, 0 turns caching off). Non-breaking: existing new Buienradar() / new Buienradar($client) calls behave the same apart from the reuse.
  • A failed download is not cached, so the next call retries.
  • rainForecast() is not cached (different feed, per coordinate).
  • README: "Caching" section.
  • Review follow-ups: expiry uses the monotonic hrtime() clock (a wall-clock jump can't stretch or cut the window), a negative cacheSeconds throws InvalidArgumentException, and the README states that failed downloads and rainForecast() aren't cached and that refresh() affects the next download (call forecast() again to reload get()/report()).

Usage

$buienradar = new Buienradar(cacheSeconds: 300);

$buienradar->forecast()->report();     // downloads the feed
$buienradar->forecast()->shortTerm();  // reuses it
$buienradar->refresh()->forecast();    // downloads again

Tests

53 tests / 102 assertions pass, PHPStan level 8 clean. New tests/FeedCacheTest.php: several different calls on one instance make one request and return the fixture data; the cache expires after cacheSeconds; refresh() makes a second; cacheSeconds: 0 disables reuse; a failed download is retried on the next call; a negative cache time is rejected. Live check: forecast(), stations(), sunrise() and a station lookup on one instance take 0.9 s in total (one download).

Closes #42

@Baspa Baspa self-assigned this Nov 13, 2026
@codecov-commenter

codecov-commenter commented Nov 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.72%. Comparing base (79bf4ca) to head (2c67645).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main      #53      +/-   ##
============================================
+ Coverage     84.74%   87.72%   +2.98%     
- Complexity       50       68      +18     
============================================
  Files             4        4              
  Lines           177      220      +43     
============================================
+ Hits            150      193      +43     
  Misses           27       27              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Baspa
Baspa merged commit c35cd75 into main Nov 13, 2026
22 checks passed
@Baspa
Baspa deleted the feat/memoize-feed branch November 13, 2026 12:05
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.

Every call re-downloads the whole feed

2 participants