Skip to content

feat: add CacheStrategy system for fine-grained cache control#10

Merged
EliaTolin merged 1 commit intomainfrom
feature/cache-strategy
Jan 7, 2026
Merged

feat: add CacheStrategy system for fine-grained cache control#10
EliaTolin merged 1 commit intomainfrom
feature/cache-strategy

Conversation

@EliaTolin
Copy link
Copy Markdown
Owner

Summary

This PR implements the CacheStrategy system as requested in #6, giving developers fine-grained control over how data is retrieved from cache vs remote source.

Changes

  • Added CacheStrategy enum with two strategies:
    • cacheFirst (default): Uses cache if available and valid, otherwise fetches from network
    • networkFirst: Always tries network first, falls back to cache (even expired) on failure
  • Added strategy parameter to call() method
  • Implemented fallback mechanism for networkFirst that uses expired cache data when network fails
  • Added comprehensive test coverage (8 new tests)
  • Updated README with usage examples, API reference, and FAQ

Usage Example

// Network-first for fresh data with offline fallback
final news = await RemoteCaching.instance.call<News>(
  'latest_news',
  strategy: CacheStrategy.networkFirst,
  remote: () async => await newsService.getLatest(),
  fromJson: (json) => News.fromJson(json as Map<String, dynamic>),
);

Backward Compatibility

  • Default strategy remains cacheFirst, preserving existing behavior
  • No breaking changes

Test plan

  • All existing tests pass
  • New tests for cacheFirst strategy behavior
  • New tests for networkFirst strategy behavior
  • Tests for network failure fallback to cache
  • Tests for expired cache fallback
  • Flutter analyze passes with no issues

Closes #6

🤖 Generated with Claude Code

Introduces a CacheStrategy enum with two strategies:
- cacheFirst (default): Uses cache if available, otherwise fetches from network
- networkFirst: Always tries network first, falls back to cache on failure

Key changes:
- Added CacheStrategy enum with documentation
- Added strategy parameter to call() method
- Implemented networkFirst with expired cache fallback
- Added comprehensive tests for both strategies
- Updated README with usage examples and API docs

Closes #6

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@EliaTolin EliaTolin merged commit 5bd88bf into main Jan 7, 2026
3 checks passed
@codecov-commenter
Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 83.33333% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.80%. Comparing base (2da88ea) to head (48c5b1d).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
lib/src/remote_caching_impl.dart 83.33% 4 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #10      +/-   ##
==========================================
- Coverage   90.09%   88.80%   -1.30%     
==========================================
  Files           3        3              
  Lines         101      125      +24     
==========================================
+ Hits           91      111      +20     
- Misses         10       14       +4     

☔ View full report in Codecov by Sentry.
📢 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.

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.

[FEATURE] Introduce a CacheStrategy system

2 participants