Skip to content

Commit

Permalink
chore: v1.0.0 (#4)
Browse files Browse the repository at this point in the history
[chore:
v1.0.0](d4ea15a)
  • Loading branch information
renancaraujo authored Jul 15, 2024
1 parent c773bdb commit c325e03
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 35 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
45 changes: 12 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: <most recent version>
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) {
Expand Down Expand Up @@ -62,28 +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.

Expand Down Expand Up @@ -112,13 +88,16 @@ 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.

Expand Down
26 changes: 25 additions & 1 deletion example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ int factorial(int n) {

void main() {
withDependency();
withMultipleDependencies();
withTimeToLive();
}

Expand All @@ -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<void> withTimeToLive() async {
print('with TTL:');

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit c325e03

Please sign in to comment.