Skip to content

Commit 97bff0e

Browse files
authored
feat: JsonCacheHive class (#65)
* feat: JsonCacheHive class Closes #64 * style: fix linter complain
1 parent 79c3604 commit 97bff0e

File tree

8 files changed

+340
-8
lines changed

8 files changed

+340
-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+
- JsonCacheHive: an implementation on top of the Hive package —
13+
[64](https://github.com/dartoos-dev/json_cache/issues/64).
14+
1015
## [1.1.2] - 2022-01-28
1116

1217
### Fixed

README.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Rultor.com](https://www.rultor.com/b/dartoos-dev/json_cache)](https://www.rultor
2929
- [JsonCacheEncPrefs — EncryptedSharedPreferences](#jsoncacheencprefs)
3030
- [JsonCacheSecStorage — FlutterSecureStorage](#jsoncachesecstorage)
3131
- [JsonCacheLocalStorage — LocalStorage](#jsoncachelocalstorage)
32+
- [JsonCacheHive — Hive](#jsoncachehive)
3233
- [JsonCacheCrossLocalStorage — CrossLocalStorage](#jsoncachecrosslocalstorage)
3334
- [Demo application](#demo-application)
3435
- [Contribute](#contribute)
@@ -79,7 +80,7 @@ abstract class JsonCache {
7980
Future<Map<String, dynamic>?> value(String key);
8081
8182
/// It either updates data located at [key] with [value] or, if there is no
82-
/// previous data at [key], creates a new cache row at [key] with [value].
83+
/// data at [key], creates a new cache row at [key] with [value].
8384
///
8485
/// **Note**: [value] must be json encodable.
8586
Future<void> refresh(String key, Map<String, dynamic> value);
@@ -127,7 +128,7 @@ the cache instance that persists data on the user's device.
127128

128129
#### Typical Usage
129130

130-
Due to the fact that `JsonCacheMem` is a decorator, you should always pass
131+
Due to the fact that `JsonCacheMem` is a decorator, you should normally pass
131132
another `JsonCache` instance to it whenever you instantiate a `JsonCacheMem`
132133
object. For example:
133134

@@ -231,6 +232,20 @@ is an implementation on top of the
231232
232233
```
233234

235+
### JsonCacheHive
236+
237+
[JsonCacheHive](https://pub.dev/documentation/json_cache/latest/json_cache/JsonCacheHive.html)
238+
is an implementation on top of the [hive](https://pub.dev/packages/hive)
239+
package.
240+
241+
```dart
242+
243+
await Hive.initFlutter(); // mandatory initialization.
244+
final box = await Hive.openBox<String>('appBox'); // it must be a Box<String>.
245+
final JsonCache hiveCache = JsonCacheMem(JsonCacheHive(box));
246+
247+
```
248+
234249
### JsonCacheCrossLocalStorage
235250

236251
[JsonCacheLocalCrossStorage](https://pub.dev/documentation/json_cache/latest/json_cache/JsonCacheCrossLocalStorage-class.html)
@@ -270,7 +285,7 @@ Contributors are welcome!
270285
branch and make a Pull Request.
271286
3. After review and acceptance, the PR is merged and closed.
272287

273-
Make sure the commands below **passes** before making a Pull Request.
288+
Make sure the command below **passes** before making a Pull Request.
274289

275290
```shell
276291
flutter analyze && flutter test

lib/json_cache.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export 'src/json_cache.dart';
55
export 'src/json_cache_cross_local_storage.dart';
66
export 'src/json_cache_enc_prefs.dart';
77
export 'src/json_cache_fake.dart';
8+
export 'src/json_cache_hive.dart';
89
export 'src/json_cache_hollow.dart';
910
export 'src/json_cache_local_storage.dart';
1011
export 'src/json_cache_mem.dart';

lib/src/json_cache_hive.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import 'dart:convert';
2+
3+
import 'package:hive/hive.dart';
4+
import 'package:json_cache/json_cache.dart';
5+
6+
/// Implementation on top of the Hive package.
7+
///
8+
/// See: [local storage](https://pub.dev/packages/hive)
9+
class JsonCacheHive implements JsonCache {
10+
/// Sets the Hive [Box] instance.
11+
const JsonCacheHive(this._box);
12+
13+
// final Box<String> _box;
14+
final Box<String> _box;
15+
16+
@override
17+
Future<void> clear() async {
18+
await _box.clear();
19+
}
20+
21+
@override
22+
Future<void> refresh(String key, Map<String, dynamic> value) async {
23+
await _box.put(key, json.encode(value));
24+
}
25+
26+
@override
27+
Future<void> remove(String key) async {
28+
await _box.delete(key);
29+
}
30+
31+
@override
32+
Future<Map<String, dynamic>?> value(String key) async {
33+
final data = _box.get(key);
34+
return data == null ? null : json.decode(data) as Map<String, dynamic>;
35+
}
36+
}

lib/src/json_cache_hollow.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ class JsonCacheHollow implements JsonCache {
3131
@override
3232
Future<void> remove(String key) async {}
3333

34-
/// Does nothing.
34+
/// Always returns null.
3535
@override
36-
Future<Map<String, dynamic>?> value(String key) async {}
36+
Future<Map<String, dynamic>?> value(String key) async {
37+
return null;
38+
}
3739
}

0 commit comments

Comments
 (0)