From 8786de14c7c2e5a86621913f0501e421e06699bb Mon Sep 17 00:00:00 2001 From: Renan araujo Date: Mon, 15 Jul 2024 18:20:19 +0100 Subject: [PATCH 1/4] readme --- README.md | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 592b3a8..3bcdb24 100644 --- a/README.md +++ b/README.md @@ -9,25 +9,22 @@ It is useful to cache values that: - Can be changed given known and unknown conditions; - Should not be computed on every access (like a getter); +A `cached_value` is better used over imperative APIs, such as Flutter's render objects. See [Motivation](#motivation) for more. + ## Installation Add to pubspec.yaml: ```yaml - dependencies: - cached_value: +dart pub add cached_value ``` -Find the most recent version on [pub](https://pub.dev/packages/cached_value). - ## Usage -A cache can be created from a simple manually controlled cache and composed with automatic -functionalities. Such as dependencies and time to live. +A cache can start as a simple manually controlled cache and then be enhanced with automatic functionalities such as dependencies and time-to-live (TTL). +### 1.Creating a simple cached that is invaldiated manually -### Creating a cache: -A simple cache is only invalidated manually. ```dart int factorial(int n) { @@ -77,7 +74,8 @@ Example: int originalValue = 1; final fancyFactorialCache = CachedValue( () => factorial(originalValue), - ).withDependency(() => originalValue) // Add dependency + ) + .withDependency(() => originalValue) // Add dependency .withTimeToLive(lifetime: Duration(seconds: 4)); // Add TTL ``` From d4ea15a6d8aac7b8d667bd0c2004edff9424596f Mon Sep 17 00:00:00 2001 From: Renan araujo Date: Mon, 15 Jul 2024 18:22:04 +0100 Subject: [PATCH 2/4] chore: v1.0.0 --- CHANGELOG.md | 7 +++++++ pubspec.yaml | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a2d90d..5067b79 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## 1.0.0 + +* Update dependencies, lints and tests +* Fix usage with sync generators +* Update README + + ## 0.2.0 * Add declarative API (#2) along with TTL. diff --git a/pubspec.yaml b/pubspec.yaml index 597fedf..b2bf2c4 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: cached_value description: A simple way to cache values that result from rather expensive operations. -version: 0.2.0 +version: 1.0.0 repository: https://github.com/bluefireteam/cached_value issue_tracker: https://github.com/bluefireteam/cached_value/issues homepage: https://github.com/bluefireteam/cached_value From 8517d45c777bdedf985bcd92bfa8029d29c53627 Mon Sep 17 00:00:00 2001 From: Renan araujo Date: Mon, 15 Jul 2024 18:26:15 +0100 Subject: [PATCH 3/4] readme --- README.md | 31 +++++-------------------------- 1 file changed, 5 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 3bcdb24..900ef84 100644 --- a/README.md +++ b/README.md @@ -59,29 +59,7 @@ the `refresh` method: print(factorialCache.value); // 12 ``` -## Composing a cache - -A cache can be composed with more resources via a declarative API. By doing that, it is possible to -add TTL and dependency without diverging from the original behavior of a cache. - -Example: -```dart - int factorial(int n) { - if (n < 0) throw ('Negative numbers are not allowed.'); - return n <= 1 ? 1 : n * factorial(n - 1); - } - - int originalValue = 1; - final fancyFactorialCache = CachedValue( - () => factorial(originalValue), - ) - .withDependency(() => originalValue) // Add dependency - .withTimeToLive(lifetime: Duration(seconds: 4)); // Add TTL -``` - -You can even create your behavior yourself by extending `SingleChildCachedValue`. - -### Adding dependency +### 2. Adding dependencies A dependent cache is marked as invalid if its dependency value has changed. @@ -110,13 +88,14 @@ A dependent cache is marked as invalid if its dependency value has changed. The dependency callback is called on every value access. So it is recommended to keep it as declarative as possible. ```dart -// Avoid this: final someCache = CachedValue( // ... -).withDependency(() => someExpensiveOperation(originalValue)); + ).withDependency( + () => someExpensiveOperation(originalValue), // ❌ Avoid this: + ); ``` -### Adding time to live +### 3. Adding time to live (TTL) A cache can be automatically marked as invalid some time after a refresh. From b29c327c6193781a28900a3574b5db99e985b43f Mon Sep 17 00:00:00 2001 From: Renan araujo Date: Mon, 15 Jul 2024 18:30:35 +0100 Subject: [PATCH 4/4] example --- README.md | 2 ++ example/example.dart | 26 +++++++++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 900ef84..e0c4df3 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,8 @@ final someCache = CachedValue( ); ``` + + ### 3. Adding time to live (TTL) A cache can be automatically marked as invalid some time after a refresh. diff --git a/example/example.dart b/example/example.dart index 0273ed1..4070f32 100644 --- a/example/example.dart +++ b/example/example.dart @@ -9,6 +9,7 @@ int factorial(int n) { void main() { withDependency(); + withMultipleDependencies(); withTimeToLive(); } @@ -24,13 +25,36 @@ void withDependency() { print(factorialCache.value); // 1 - print(factorialCache.value); // 1 - not recomputes + print(factorialCache.value); // 1 - cached originalValue = 6; print(factorialCache.value); // 720 } +void withMultipleDependencies() { + print('with multiple dependencies'); + + var originalValue1 = 1; + var originalValue2 = 2; + final factorialCache = CachedValue( + () => factorial(originalValue1) + factorial(originalValue2), + ).withDependency( + () sync* { + yield originalValue1; + yield originalValue2; + }, + ); + + print(factorialCache.value); // 3 + + print(factorialCache.value); // 3 - cached + + originalValue2 = 6; + + print(factorialCache.value); // 721 +} + Future withTimeToLive() async { print('with TTL:');