Skip to content

Commit eb8004f

Browse files
committed
WIP
1 parent 3d1b1b2 commit eb8004f

15 files changed

+137
-22
lines changed

lib/src/json_cache.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:collection';
2+
13
/// Represents cached data in json format.
24
///
35
///> Cache is a hardware or software component that stores data so that future
@@ -25,4 +27,9 @@ abstract class JsonCache {
2527
///
2628
/// Returns `true` if there is cached data at [key]; `false` otherwise.
2729
Future<bool> contains(String key);
30+
31+
/// Lists all keys.
32+
///
33+
/// Returns an **unmodifiable** list of cache keys without duplicates.
34+
Future<UnmodifiableListView<String>> keys();
2835
}

lib/src/json_cache_exception.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class JsonCacheException<T extends Object> implements Exception {
1111

1212
/// The original exception that indicated the failure of the caching
1313
/// operation.
14-
final Exception? exception;
14+
final Object? exception;
1515

1616
/// Returns [extra] along with the original exception message.
1717
@override

lib/src/json_cache_fake.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:collection';
2+
13
import 'package:json_cache/json_cache.dart';
24

35
/// In-memory cache without synchronization.
@@ -50,4 +52,9 @@ class JsonCacheFake implements JsonCache {
5052
Future<bool> contains(String key) async {
5153
return _memory.containsKey(key);
5254
}
55+
56+
@override
57+
Future<UnmodifiableListView<String>> keys() async {
58+
return UnmodifiableListView(_memory.keys);
59+
}
5360
}

lib/src/json_cache_flutter_secure_storage.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'dart:collection';
12
import 'dart:convert';
23

34
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
@@ -39,6 +40,11 @@ class JsonCacheFlutterSecureStorage implements JsonCache {
3940

4041
@override
4142
Future<bool> contains(String key) async {
42-
return _storage.containsKey(key: key);
43+
return await _storage.containsKey(key: key);
44+
}
45+
46+
@override
47+
Future<UnmodifiableListView<String>> keys() async {
48+
return UnmodifiableListView((await _storage.readAll()).keys);
4349
}
4450
}

lib/src/json_cache_hive.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'dart:collection';
12
import 'dart:convert';
23

34
import 'package:hive/hive.dart';
@@ -37,4 +38,9 @@ class JsonCacheHive implements JsonCache {
3738
Future<bool> contains(String key) async {
3839
return _box.containsKey(key);
3940
}
41+
42+
@override
43+
Future<UnmodifiableListView<String>> keys() async {
44+
return UnmodifiableListView(_box.keys.map((k) => k as String));
45+
}
4046
}

lib/src/json_cache_hollow.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:collection';
2+
13
import 'package:json_cache/json_cache.dart';
24

35
/// Hollow (empty) [JsonCache] — It is intended to serve as a placeholder for
@@ -38,4 +40,9 @@ class JsonCacheHollow implements JsonCache {
3840
/// Always returns `false`.
3941
@override
4042
Future<bool> contains(String key) async => false;
43+
44+
/// An empty list of keys.
45+
@override
46+
Future<UnmodifiableListView<String>> keys() async =>
47+
UnmodifiableListView(const []);
4148
}

lib/src/json_cache_local_storage.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'dart:collection';
12
import 'dart:convert';
23

34
import 'package:json_cache/json_cache.dart';
@@ -44,4 +45,15 @@ final class JsonCacheLocalStorage implements JsonCache {
4445

4546
@override
4647
Future<bool> contains(String key) async => _storage.getItem(key) != null;
48+
49+
@override
50+
Future<UnmodifiableListView<String>> keys() async {
51+
return UnmodifiableListView(
52+
List.generate(
53+
_storage.length,
54+
(int i) => _storage.key(i)!,
55+
growable: false,
56+
),
57+
);
58+
}
4759
}

lib/src/json_cache_mem.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:async';
2+
import 'dart:collection';
23

34
import 'package:json_cache/json_cache.dart';
45
import 'package:mutex/mutex.dart';
@@ -93,8 +94,6 @@ class JsonCacheMem implements JsonCache {
9394
static final _shrMutex = ReadWriteMutex();
9495

9596
/// Frees up storage space in both the level2 cache and in-memory cache.
96-
///
97-
/// Throws [JsonCacheException] to indicate operation failure.
9897
@override
9998
Future<void> clear() async {
10099
await _mutex.protectWrite(() async {
@@ -105,8 +104,6 @@ class JsonCacheMem implements JsonCache {
105104

106105
/// Updates the data located at [key] in both the _level 2_ cache and
107106
/// in-memory cache.
108-
///
109-
/// Throws [JsonCacheException] to indicate operation failure.
110107
@override
111108
Future<void> refresh(String key, Map<String, dynamic> data) async {
112109
// ATTENTION: It is safer to copy the content of [data] before calling an
@@ -160,4 +157,11 @@ class JsonCacheMem implements JsonCache {
160157
return foundInMemory ? foundInMemory : await _level2.contains(key);
161158
});
162159
}
160+
161+
@override
162+
Future<UnmodifiableListView<String>> keys() {
163+
return _mutex.protectRead(() async {
164+
return UnmodifiableListView(_memory.keys);
165+
});
166+
}
163167
}

lib/src/json_cache_safe_local_storage.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// ignore_for_file: avoid_dynamic_calls
22

3+
import 'dart:collection';
4+
35
import 'package:json_cache/json_cache.dart';
46
import 'package:safe_local_storage/safe_local_storage.dart';
57

@@ -43,6 +45,14 @@ class JsonCacheSafeLocalStorage implements JsonCache {
4345
return (await _cachedData)[key] as Map<String, dynamic>?;
4446
}
4547

48+
@override
49+
Future<UnmodifiableListView<String>> keys() async {
50+
final data = await _cachedData;
51+
return UnmodifiableListView(
52+
data.keys.map((k) => k as String),
53+
);
54+
}
55+
4656
/// Gets the cached data stored in the local storage file.
4757
Future<Map<dynamic, dynamic>> get _cachedData async {
4858
return await _localStorage.read() as Map<dynamic, dynamic>;

lib/src/json_cache_shared_preferences.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import 'dart:collection';
12
import 'dart:convert';
23

34
import 'package:json_cache/json_cache.dart';
@@ -47,4 +48,9 @@ class JsonCacheSharedPreferences implements JsonCache {
4748
Future<bool> contains(String key) async {
4849
return _sharedPreferences.containsKey(key);
4950
}
51+
52+
@override
53+
Future<UnmodifiableListView<String>> keys() async {
54+
return UnmodifiableListView(_sharedPreferences.getKeys());
55+
}
5056
}

0 commit comments

Comments
 (0)