Skip to content

Commit 03f5d76

Browse files
committed
feat: implementation of the 'contains' method
Closes #92
1 parent 6ec937d commit 03f5d76

23 files changed

+309
-21
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+
- implementation of the 'contains' method —
13+
[92](https://github.com/dartoos-dev/json_cache/issues/92).
14+
1015
## [1.2.6] - 2022-08-01
1116

1217
### Changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ abstract class JsonCache {
9191
///
9292
/// **Note**: [value] must be json encodable.
9393
Future<void> refresh(String key, Map<String, dynamic> value);
94+
95+
/// Checks whether there is cached data at [key].
96+
///
97+
/// Returns `true` if there is cached data at [key]; `false` otherwise.
98+
Future<bool> contains(String key);
9499
}
95100
```
96101

lib/json_cache.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// Collection of json cache decorators.
1+
/// A collection of [JsonCache] decorators.
22
library json_cache;
33

44
export 'src/json_cache.dart';

lib/src/json_cache.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@ abstract class JsonCache {
2020
///
2121
/// **Note**: [value] must be json encodable.
2222
Future<void> refresh(String key, Map<String, dynamic> value);
23+
24+
/// Checks whether there is cached data at [key].
25+
///
26+
/// Returns `true` if there is cached data at [key]; `false` otherwise.
27+
Future<bool> contains(String key);
2328
}

lib/src/json_cache_cross_local_storage.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,10 @@ class JsonCacheCrossLocalStorage implements JsonCache {
4343
? null
4444
: json.decode(strJson) as Map<String, dynamic>;
4545
}
46+
47+
/// Checks whether there is cached data at [key].
48+
@override
49+
Future<bool> contains(String key) async {
50+
return _prefs.containsKey(key);
51+
}
4652
}

lib/src/json_cache_enc_prefs.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,9 @@ class JsonCacheEncPrefs implements JsonCache {
4040
}
4141
return null;
4242
}
43+
44+
@override
45+
Future<bool> contains(String key) async {
46+
return (await _encPrefs.getInstance()).containsKey(key);
47+
}
4348
}

lib/src/json_cache_fake.dart

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,31 @@ class JsonCacheFake implements JsonCache {
2323

2424
/// Clears its internal in-memory storage.
2525
@override
26-
Future<void> clear() async => _memory.clear();
26+
Future<void> clear() async {
27+
_memory.clear();
28+
}
2729

2830
/// Updates data located at [key].
2931
@override
30-
Future<void> refresh(String key, Map<String, dynamic> data) async =>
31-
_memory[key] = Map<String, dynamic>.of(data);
32+
Future<void> refresh(String key, Map<String, dynamic> data) async {
33+
_memory[key] = Map<String, dynamic>.of(data);
34+
}
3235

3336
/// Removes data located at [key].
3437
@override
35-
Future<void> remove(String key) async => _memory.remove(key);
38+
Future<void> remove(String key) async {
39+
_memory.remove(key);
40+
}
3641

3742
/// Retrieves a copy of the data at [key] or `null` if there is no data.
3843
@override
3944
Future<Map<String, dynamic>?> value(String key) async {
4045
final cached = _memory[key];
4146
return cached == null ? null : Map<String, dynamic>.of(cached);
4247
}
48+
49+
@override
50+
Future<bool> contains(String key) async {
51+
return _memory.containsKey(key);
52+
}
4353
}

lib/src/json_cache_hive.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ class JsonCacheHive implements JsonCache {
1010
/// Sets the Hive [Box] instance.
1111
const JsonCacheHive(this._box);
1212

13-
// final Box<String> _box;
1413
final Box<String> _box;
1514

1615
@override
@@ -33,4 +32,9 @@ class JsonCacheHive implements JsonCache {
3332
final data = _box.get(key);
3433
return data == null ? null : json.decode(data) as Map<String, dynamic>;
3534
}
35+
36+
@override
37+
Future<bool> contains(String key) async {
38+
return _box.containsKey(key);
39+
}
3640
}

lib/src/json_cache_hollow.dart

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

34-
/// Always returns null.
34+
/// Always returns `null`.
3535
@override
3636
Future<Map<String, dynamic>?> value(String key) async => null;
37+
38+
/// Always returns `false`.
39+
@override
40+
Future<bool> contains(String key) async => false;
3741
}

lib/src/json_cache_local_storage.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,11 @@ class JsonCacheLocalStorage implements JsonCache {
3535
await _getReady;
3636
return await _storage.getItem(key) as Map<String, dynamic>?;
3737
}
38+
39+
@override
40+
Future<bool> contains(String key) async {
41+
await _getReady;
42+
final Object? item = _storage.getItem(key);
43+
return item != null;
44+
}
3845
}

0 commit comments

Comments
 (0)