Skip to content

Commit 8b2eda2

Browse files
author
Kinpert
committed
refactor: JsonCacheSafeLocalStorage functionalities
1 parent a91a59d commit 8b2eda2

File tree

3 files changed

+72
-27
lines changed

3 files changed

+72
-27
lines changed

lib/json_cache.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export 'src/json_cache_hive.dart';
1010
export 'src/json_cache_hollow.dart';
1111
export 'src/json_cache_local_storage.dart';
1212
export 'src/json_cache_mem.dart';
13+
export 'src/json_cache_safe_local_storage.dart';
1314
export 'src/json_cache_shared_preferences.dart';
1415
export 'src/json_cache_try.dart';
1516
export 'src/json_cache_wrap.dart';

lib/src/json_cache_safe_local_storage.dart

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,32 @@ class JsonCacheSafeLocalStorage implements JsonCache {
1919

2020
@override
2121
Future<bool> contains(String key) async {
22-
final item = await _localStorage.read();
23-
return item[key] != null;
22+
return (await _cachedData).containsKey(key);
2423
}
2524

2625
@override
2726
Future<void> refresh(String key, Map<String, dynamic> value) async {
28-
final data = {key: value};
29-
await _localStorage.write(data);
27+
final dataCopied = Map<dynamic, dynamic>.of(await _cachedData);
28+
dataCopied[key] = value;
29+
await _localStorage.write(dataCopied);
3030
}
3131

3232
@override
3333
Future<void> remove(String key) async {
34-
final data = await _localStorage.read() as Map<String, dynamic>;
35-
data.removeWhere((keyMap, valueMap) => keyMap == key);
36-
await _localStorage.write(data);
34+
final data = await _cachedData;
35+
if (data.containsKey(key)) {
36+
data.remove(key);
37+
await _localStorage.write(data);
38+
}
3739
}
3840

3941
@override
4042
Future<Map<String, dynamic>?> value(String key) async {
41-
final data = await _localStorage.read();
42-
return data[key] == null ? null : data[key] as Map<String, dynamic>;
43+
return (await _cachedData)[key] as Map<String, dynamic>?;
44+
}
45+
46+
/// Gets the cached data stored in the local storage file.
47+
Future<Map<dynamic, dynamic>> get _cachedData async {
48+
return await _localStorage.read() as Map<dynamic, dynamic>;
4349
}
4450
}

test/json_cache_safe_local_storage_test.dart

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,53 +40,91 @@ void main() {
4040
'JsonCacheSafeLocalStorage',
4141
() {
4242
test(
43-
'refresh, value, clear, refresh',
43+
'"refresh", "value", "remove" with one piece of data',
4444
() async {
45+
// Insert a single piece of data into the cache
4546
await jsonCacheSafeLocalStorage.refresh(profKey, profData);
4647

47-
final data = await jsonCacheSafeLocalStorage.value(profKey);
48-
expect(data, profData);
49-
await jsonCacheSafeLocalStorage.clear();
48+
// Check if the data has been inserted into the cache.
49+
final profileRetrieved =
50+
await jsonCacheSafeLocalStorage.value(profKey);
51+
expect(profileRetrieved, equals(profData));
52+
53+
// Remove the single piece of data stored.
54+
await jsonCacheSafeLocalStorage.remove(profKey);
5055

51-
final cleanCache = await jsonCacheSafeLocalStorage.value(profKey);
52-
expect(cleanCache, isNull);
56+
// No data should remain in the cache.
57+
final cachedValue = await jsonCacheSafeLocalStorage.value(profKey);
58+
expect(cachedValue, isNull);
5359
},
5460
);
5561

5662
test(
57-
'contains',
63+
'"refresh", "value", "remove" with multiple data',
5864
() async {
59-
// Adding content on cache. Each 'refresh' method overrides the last one.
65+
// Insert multiple data into the cache
6066
await jsonCacheSafeLocalStorage.refresh(profKey, profData);
6167
await jsonCacheSafeLocalStorage.refresh(prefKey, prefData);
6268

63-
// Returning true if the last content exists on cache based on 'prefKey'.
64-
expect(await jsonCacheSafeLocalStorage.contains(prefKey), true);
65-
expect(await jsonCacheSafeLocalStorage.contains(profKey), false);
69+
// Check if multiple data has been inserted into the cache.
70+
final profileRetrieved =
71+
await jsonCacheSafeLocalStorage.value(profKey);
72+
expect(profileRetrieved, equals(profData));
73+
74+
final preferencesRetrived =
75+
await jsonCacheSafeLocalStorage.value(prefKey);
76+
expect(preferencesRetrived, prefData);
6677

78+
// Remove data from the cache.
79+
await jsonCacheSafeLocalStorage.remove(profKey);
80+
await jsonCacheSafeLocalStorage.remove(prefKey);
81+
82+
final removedProfValue =
83+
await jsonCacheSafeLocalStorage.value(profKey);
84+
expect(removedProfValue, isNull);
85+
final removedPrefValue =
86+
await jsonCacheSafeLocalStorage.value(prefKey);
87+
expect(removedPrefValue, isNull);
88+
},
89+
);
90+
91+
test(
92+
'contains',
93+
() async {
94+
// Insert a single piece of data into the cache.
6795
await jsonCacheSafeLocalStorage.refresh(profKey, profData);
68-
// Returning true if the last content exists on cache based on 'profKey'.
96+
97+
// Check
6998
expect(await jsonCacheSafeLocalStorage.contains(profKey), true);
7099
expect(await jsonCacheSafeLocalStorage.contains(prefKey), false);
71100

72101
// Test for keys that doesn't exist
73102
expect(await jsonCacheSafeLocalStorage.contains('generickey'), false);
74103
expect(await jsonCacheSafeLocalStorage.contains('PROFKEY'), false);
75104
expect(await jsonCacheSafeLocalStorage.contains('PREFKEY'), false);
105+
106+
// Insert a new piece of data into the cache to test more than one key stored.
107+
await jsonCacheSafeLocalStorage.refresh(prefKey, prefData);
108+
109+
// Check for multiple data.
110+
expect(await jsonCacheSafeLocalStorage.contains(profKey), true);
111+
expect(await jsonCacheSafeLocalStorage.contains(prefKey), true);
76112
},
77113
);
78114

79115
test(
80-
'remove',
116+
'clear',
81117
() async {
118+
// Insert multiple data into the cache.
82119
await jsonCacheSafeLocalStorage.refresh(profKey, profData);
120+
await jsonCacheSafeLocalStorage.refresh(prefKey, prefData);
83121

84-
final recoveredData = await jsonCacheSafeLocalStorage.value(profKey);
85-
expect(recoveredData, profData);
122+
// Clear it.
123+
await jsonCacheSafeLocalStorage.clear();
86124

87-
await jsonCacheSafeLocalStorage.remove(profKey);
88-
final cleanCache = await jsonCacheSafeLocalStorage.value(profKey);
89-
expect(cleanCache, isNull);
125+
// No data should remain in the cache.
126+
final cachedValue = await jsonCacheSafeLocalStorage.value(profKey);
127+
expect(cachedValue, isNull);
90128
},
91129
);
92130
},

0 commit comments

Comments
 (0)