Skip to content

Commit 3cb0028

Browse files
authored
feat: JsonCacheCrossLocalStorage class
* feat: JsonCacheCrossLocalStorage class * doc(README): remove unnecessary comma
1 parent 326f238 commit 3cb0028

File tree

5 files changed

+126
-8
lines changed

5 files changed

+126
-8
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+
### Added
11+
12+
- JsonCacheCrossLocalStorage: implementation on top of the cross_local_storage
13+
package — [32](https://github.com/dartoos-dev/json_cache/issues/32).
14+
1015
### Changed
1116

1217
- several fixes and improvements to documentation.

README.md

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Rultor.com](https://www.rultor.com/b/dartoos-dev/json_cache)](https://www.rultor
2525
- [JsonCacheMem — Thread-safe In-memory cache](#jsoncachemem)
2626
- [JsonCachePrefs — SharedPreferences](#jsoncacheprefs)
2727
- [JsonCacheLocalStorage — LocalStorage](#jsoncachelocalstorage)
28+
- [JsonCacheCrossLocalStorage — CrossLocalStorage](#jsoncachecrosslocalstorage)
2829
- [Demo application](#demo-application)
2930
- [References](#references)
3031

@@ -39,13 +40,13 @@ Rultor.com](https://www.rultor.com/b/dartoos-dev/json_cache)](https://www.rultor
3940
> Retrieved 09:55, August 22,
4041
> 2021](https://en.wikipedia.org/wiki/Cache_(computing))
4142
42-
**JsonCache** is an object-oriented package to cache user data locally in json.
43-
It can also be thought of as a layer on top of Flutter's local storage packages
44-
like the [sharable_preferences](https://pub.dev/packages/shared_preferences) and
45-
[localstorage](https://pub.dev/packages/localstorage) packages.
46-
47-
Therefore, this package aims to unify most of Flutter's local caching packages
48-
into an elegant caching API.
43+
**JsonCache** is an object-oriented package for the local caching of user data
44+
in json. It can also be thought of as a layer on top of Flutter's local storage
45+
packages like the
46+
[sharable_preferences](https://pub.dev/packages/shared_preferences) and
47+
[localstorage](https://pub.dev/packages/localstorage) packages. Thus, this
48+
package aims to unify most of Flutter's local caching packages into a stable and
49+
elegant caching API.
4950

5051
**Why Json?**
5152

@@ -181,7 +182,7 @@ is an implementation on top of the
181182

182183
[JsonCacheLocalStorage](https://pub.dev/documentation/json_cache/latest/json_cache/JsonCacheLocalStorage-class.html)
183184
is an implementation on top of the
184-
[localstorage](https://pub.dev/packages/localstorage)
185+
[localstorage](https://pub.dev/packages/localstorage) package.
185186

186187
```dart
187188
@@ -190,6 +191,19 @@ is an implementation on top of the
190191
191192
```
192193

194+
### JsonCacheCrossLocalStorage
195+
196+
[JsonCacheLocalCrossStorage](https://pub.dev/documentation/json_cache/latest/json_cache/JsonCacheCrossLocalStorage-class.html)
197+
is an implementation on top of the
198+
[cross_local_storage](https://pub.dev/packages/cross_local_storage) package.
199+
200+
```dart
201+
202+
final LocalStorageInterface prefs = await LocalStorage.getInstance();
203+
final JsonCache jsonCache = JsonCacheMem(JsonCacheCrossLocalStorage(prefs));
204+
205+
```
206+
193207
## Demo application
194208

195209
The demo application provides a fully working example, focused on demonstrating

lib/json_cache.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
library json_cache;
33

44
export 'src/json_cache.dart';
5+
export 'src/json_cache_cross_local_storage.dart';
56
export 'src/json_cache_fake.dart';
67
export 'src/json_cache_hollow.dart';
78
export 'src/json_cache_local_storage.dart';
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import 'dart:convert';
2+
3+
import 'package:cross_local_storage/cross_local_storage.dart';
4+
5+
import 'json_cache.dart';
6+
7+
/// [JsonCache] on top of the Cross Local Storage package.
8+
///
9+
/// See:
10+
/// - [cross local storage](https://pub.dev/packages/cross_local_storage)
11+
class JsonCacheCrossLocalStorage implements JsonCache {
12+
/// Ctor.
13+
JsonCacheCrossLocalStorage(this._prefs);
14+
15+
final LocalStorageInterface _prefs;
16+
17+
/// Clears user preferences.
18+
@override
19+
Future<void> clear() async {
20+
await _prefs.clear();
21+
}
22+
23+
/// Removes an entry located at [key] from persistence storage
24+
@override
25+
Future<void> remove(String key) async {
26+
await _prefs.remove(key);
27+
}
28+
29+
/// Writes [value] to persistence storage.
30+
///
31+
/// **Note**: [value] must be json encodable — `json.encode()` is called under
32+
/// the hood.
33+
@override
34+
Future<void> refresh(String key, Map<String, dynamic> value) async {
35+
await _prefs.setString(key, json.encode(value));
36+
}
37+
38+
/// The value at [key] from the preferences file; it returns null if a cache
39+
/// miss occurs.
40+
@override
41+
Future<Map<String, dynamic>?> value(String key) async {
42+
final strJson = _prefs.getString(key);
43+
return strJson == null
44+
? null
45+
: json.decode(strJson) as Map<String, dynamic>;
46+
}
47+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import 'package:cross_local_storage/cross_local_storage.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
import 'package:json_cache/json_cache.dart';
4+
5+
void main() {
6+
group('JsonCacheCrossLocalStorage', () {
7+
test('clear, recover and refresh', () async {
8+
final prefs = await LocalStorage.getInstance();
9+
const profKey = 'profile';
10+
const profData = <String, Object>{'id': 1, 'name': 'John Due'};
11+
final JsonCacheCrossLocalStorage crossCache =
12+
JsonCacheCrossLocalStorage(prefs);
13+
await crossCache.refresh(profKey, profData);
14+
var prof = await crossCache.value(profKey);
15+
expect(prof, profData);
16+
await crossCache.clear();
17+
prof = await crossCache.value(profKey);
18+
expect(prof, isNull);
19+
await prefs.clear();
20+
});
21+
22+
test('remove', () async {
23+
final prefs = await LocalStorage.getInstance();
24+
const profKey = 'profile';
25+
const prefKey = 'preferences';
26+
final profData = <String, Object>{'id': 1, 'name': 'John Due'};
27+
final prefData = <String, Object>{
28+
'theme': 'dark',
29+
'notifications': {'enabled': true}
30+
};
31+
final JsonCacheCrossLocalStorage crossCache =
32+
JsonCacheCrossLocalStorage(prefs);
33+
await crossCache.refresh(profKey, profData);
34+
await crossCache.refresh(prefKey, prefData);
35+
36+
var prof = await crossCache.value(profKey);
37+
expect(prof, profData);
38+
39+
await crossCache.remove(profKey);
40+
prof = await crossCache.value(profKey);
41+
expect(prof, isNull);
42+
43+
var pref = await crossCache.value(prefKey);
44+
expect(pref, prefData);
45+
await crossCache.remove(prefKey);
46+
pref = await crossCache.value(prefKey);
47+
expect(pref, isNull);
48+
await prefs.clear();
49+
});
50+
});
51+
}

0 commit comments

Comments
 (0)