Skip to content

Commit 326f238

Browse files
authored
style: pub dev fixes
* refactor! rename JsonCacheMem.mem to JsonCacheMem.ext * refactor!: revert JsonCacheMem.mem constructor * style: pub dev fixes * style: run flutter format * refactor: more efficient refresh impl.
1 parent 2cef1dd commit 326f238

File tree

7 files changed

+84
-134
lines changed

7 files changed

+84
-134
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- several fixes and improvements to documentation.
13+
- run flutter format.
14+
1015
## [0.3.0] - 2021-08-25
1116

1217
### Added

README.md

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ It can also be thought of as a layer on top of Flutter's local storage packages
4444
like the [sharable_preferences](https://pub.dev/packages/shared_preferences) and
4545
[localstorage](https://pub.dev/packages/localstorage) packages.
4646

47-
The ultimate goal of this package is to unify Flutter's main local caching
48-
packages into an elegant caching API.
47+
Therefore, this package aims to unify most of Flutter's local caching packages
48+
into an elegant caching API.
4949

5050
**Why Json?**
5151

@@ -64,22 +64,22 @@ requirements.
6464
cached data. It is defined as:
6565

6666
```dart
67-
/// Represents data cached in json.
67+
/// Represents cached data in json format.
6868
abstract class JsonCache {
6969
/// Frees up storage space.
7070
Future<void> clear();
7171
72-
/// It either updates the data found at [key] with [value] or, if there is no
72+
/// Removes cached data located at [key].
73+
Future<void> remove(String key);
74+
75+
/// Retrieves cached data located at [key] or null if a cache miss occurs.
76+
Future<Map<String, dynamic>?> value(String key);
77+
78+
/// It either updates data located at [key] with [value] or, if there is no
7379
/// previous data at [key], creates a new cache row at [key] with [value].
7480
///
7581
/// **Note**: [value] must be json encodable.
7682
Future<void> refresh(String key, Map<String, dynamic> value);
77-
78-
/// Removes data from cache at [key].
79-
Future<void> remove(String key);
80-
81-
/// Retrieves the data value at [key] or null if a cache miss occurs.
82-
Future<Map<String, dynamic>?> value(String key);
8383
}
8484
```
8585

@@ -95,6 +95,15 @@ represents the name of a single data group. For example:
9595
Above, the _profile_ key is associated with the profile-related data group,
9696
while the _preferences_ key is associated with the preferences-related data.
9797

98+
A typical code for saving the previous _profile_ and _preferences_ data is:
99+
100+
```dart
101+
final JsonCache jsonCache = … retrieve one of the JsonCache implementations.
102+
103+
await jsonCache.refresh('profile', {'name': 'John Doe', 'email': 'johndoe@email.com', 'accountType': 'premium'});
104+
await jsonCache.refresh('preferences', {'theme': {'dark': true}, 'notifications':{'enabled': true}});
105+
```
106+
98107
## List of JsonCache Implementations
99108

100109
The library
@@ -121,9 +130,26 @@ object. For example:
121130

122131
```dart
123132
133+
/// Cache initialization
124134
final prefs = await SharedPreferences.getInstance();
125135
final JsonCacheMem jsonCache = JsonCacheMem(JsonCachePrefs(prefs));
126136
137+
/// Saving profile and preferences data.
138+
await jsonCache.refresh('profile', {'name': 'John Doe', 'email': 'johndoe@email.com', 'accountType': 'premium'});
139+
await jsonCache.refresh('preferences', {'theme': {'dark': true}, 'notifications':{'enabled': true}});
140+
141+
/// Retrieving preferences data.
142+
final Map<String, dynamic> preferences = await jsonCache.value('preferences');
143+
144+
/// Frees up cached data before the user leaves the application.
145+
Future<void> signout() async {
146+
await jsonCache.clear();
147+
}
148+
149+
/// Removes cached data related to a specific user.
150+
Future<void> signoutId(String userId) async {
151+
await jsonCache.remove(userId);
152+
}
127153
```
128154

129155
In addition, `JsonCacheMem` has the `JsonCacheMem.init` constructor whose

lib/src/json_cache.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// Represents data cached in json.
1+
/// Represents cached data in json format.
22
///
33
///> Cache is a hardware or software component that stores data so that future
44
///> requests for that data can be served faster; the data stored in a cache
@@ -9,15 +9,15 @@ abstract class JsonCache {
99
/// Frees up storage space.
1010
Future<void> clear();
1111

12+
/// Removes cached data located at [key].
13+
Future<void> remove(String key);
14+
15+
/// Retrieves cached data located at [key] or null if a cache miss occurs.
16+
Future<Map<String, dynamic>?> value(String key);
17+
1218
/// It either updates the data found at [key] with [value] or, if there is no
13-
/// previous data at [key], creates a new cache row at [key] with [value].
19+
/// previous data at [key], creates a new cache line at [key] with [value].
1420
///
1521
/// **Note**: [value] must be json encodable.
1622
Future<void> refresh(String key, Map<String, dynamic> value);
17-
18-
/// Removes data from cache at [key].
19-
Future<void> remove(String key);
20-
21-
/// Retrieves the data value at [key] or null if a cache miss occurs.
22-
Future<Map<String, dynamic>?> value(String key);
2323
}

lib/src/json_cache_mem.dart

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,16 @@ class JsonCacheMem implements JsonCache {
2828
/// prototyping or mocking phase. However, its unlikely to be the right
2929
/// behavior in production code.
3030
JsonCacheMem([JsonCache? level2])
31-
<<<<<<< HEAD
32-
: this.ext(_shrMem, level2: level2, mutex: _shrMutex);
33-
=======
3431
: this.mem(_shrMem, level2: level2, mutex: _shrMutex);
35-
>>>>>>> 32
3632

37-
/// Cache with initial data.
38-
///
39-
/// Besides copying data from [init] to its internal shared memory, it
40-
/// encapsulates a [level2] cache that is supposed to persist data to the
41-
/// user's device's local storage area.
33+
/// Initializes both the level1 (internal memory) and level2 (user's device's
34+
/// local storage area) caches with data.
4235
///
4336
/// It also provides a type of transaction guarantee whereby, if an error
44-
/// occurs while copying [init] to the cache, it tries to revert the cached
45-
/// data to its previous state before rethrowing the exception. Finally, after
46-
/// reverting the cached data, it invokes [onInitError].
37+
/// occurs while copying [init] to cache, it will try to revert the cached
38+
/// data to its previous state before rethrowing the exception that signaled
39+
/// the error. Finally, after reverting the cached data, it invokes
40+
/// [onInitError].
4741
JsonCacheMem.init(
4842
Map<String, Map<String, dynamic>?> init, {
4943
JsonCache? level2,
@@ -80,11 +74,7 @@ class JsonCacheMem implements JsonCache {
8074
/// Cache with an external memory and an optional custom mutex.
8175
///
8276
/// **Note**: the memory [mem] will **not** be copied deeply.
83-
<<<<<<< HEAD
84-
JsonCacheMem.ext(
85-
=======
8677
JsonCacheMem.mem(
87-
>>>>>>> 32
8878
Map<String, Map<String, dynamic>?> mem, {
8979
JsonCache? level2,
9080
ReadWriteMutex? mutex,
@@ -147,7 +137,7 @@ class JsonCacheMem implements JsonCache {
147137

148138
/// Retrieves the value at [key] or null if there is no data.
149139
@override
150-
Future<Map<String, dynamic>?> value(String key) async {
140+
Future<Map<String, dynamic>?> value(String key) {
151141
return _mutex.protectRead(() async {
152142
final cachedL1 = _memory[key];
153143
if (cachedL1 != null) return Map<String, dynamic>.of(cachedL1);
@@ -157,7 +147,6 @@ class JsonCacheMem implements JsonCache {
157147
_memory[key] = cachedL2;
158148
return Map<String, dynamic>.of(cachedL2);
159149
}
160-
161150
return null;
162151
});
163152
}

pubspec.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ packages:
77
name: async
88
url: "https://pub.dartlang.org"
99
source: hosted
10-
version: "2.6.1"
10+
version: "2.8.1"
1111
boolean_selector:
1212
dependency: transitive
1313
description:
@@ -28,7 +28,7 @@ packages:
2828
name: charcode
2929
url: "https://pub.dartlang.org"
3030
source: hosted
31-
version: "1.2.0"
31+
version: "1.3.1"
3232
clock:
3333
dependency: transitive
3434
description:
@@ -120,7 +120,7 @@ packages:
120120
name: meta
121121
url: "https://pub.dartlang.org"
122122
source: hosted
123-
version: "1.3.0"
123+
version: "1.7.0"
124124
mutex:
125125
dependency: "direct main"
126126
description:
@@ -279,7 +279,7 @@ packages:
279279
name: test_api
280280
url: "https://pub.dartlang.org"
281281
source: hosted
282-
version: "0.3.0"
282+
version: "0.4.2"
283283
typed_data:
284284
dependency: transitive
285285
description:

test/json_cache_fake_test.dart

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,13 @@ void main() {
3838
});
3939
test('mem ctor', () async {
4040
final copy = Map<String, Map<String, dynamic>?>.of(data);
41-
<<<<<<< HEAD
42-
final JsonCacheFake memMemCache = JsonCacheFake.mem(copy);
43-
await memMemCache.clear();
44-
45-
final cachedProf = await memMemCache.value(profKey);
46-
expect(cachedProf, isNull);
47-
48-
final cachedPref = await memMemCache.value(prefKey);
49-
=======
5041
final JsonCacheFake memCache = JsonCacheFake.mem(copy);
5142
await memCache.clear();
5243

5344
final cachedProf = await memCache.value(profKey);
5445
expect(cachedProf, isNull);
5546

5647
final cachedPref = await memCache.value(prefKey);
57-
>>>>>>> 32
5848
expect(cachedPref, isNull);
5949

6050
expect(copy.isEmpty, true);

0 commit comments

Comments
 (0)