Skip to content

Commit fd83ac2

Browse files
authored
doc: documentation improvements
* chore: fix minor typos * doc(README): tips for storing simple values
1 parent 543dbdd commit fd83ac2

File tree

3 files changed

+44
-25
lines changed

3 files changed

+44
-25
lines changed

README.md

+40-21
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ Rultor.com](https://www.rultor.com/b/dartoos-dev/json_cache)](https://www.rultor
2323

2424
- [Overview](#overview)
2525
- [Getting Started](#getting-started)
26+
- [Storing Simple Values](#storing-simple-values)
2627
- [Suggested Dependency Relationship](#suggested-dependency-relationship)
2728
- [Implementations](#implementations)
2829
- [JsonCacheMem — Thread-safe In-memory cache](#jsoncachemem)
2930
- [JsonCachePrefs — SharedPreferences](#jsoncacheprefs)
3031
- [JsonCacheEncPrefs — EncryptedSharedPreferences](#jsoncacheencprefs)
31-
- [JsonCacheSecStorage — FlutterSecureStorage](#jsoncachesecstorage)
3232
- [JsonCacheLocalStorage — LocalStorage](#jsoncachelocalstorage)
33+
- [JsonCacheSecStorage — FlutterSecureStorage](#jsoncachesecstorage)
3334
- [JsonCacheCrossLocalStorage — CrossLocalStorage](#jsoncachecrosslocalstorage)
3435
- [JsonCacheHive — Hive](#jsoncachehive)
3536
- [Unit Test Tips](#unit-test-tips)
@@ -77,13 +78,13 @@ It is defined as:
7778
```dart
7879
/// Represents cached data in json format.
7980
abstract class JsonCache {
80-
/// Frees up storage space — deletes all keys with associated values.
81+
/// Frees up storage space — deletes all keys and values.
8182
Future<void> clear();
8283
8384
/// Removes cached data located at [key].
8485
Future<void> remove(String key);
8586
86-
/// Retrieves cached data located at [key] or null if a cache miss occurs.
87+
/// Retrieves cached data located at [key] or `null` if a cache miss occurs.
8788
Future<Map<String, dynamic>?> value(String key);
8889
8990
/// It either updates data located at [key] with [value] or, if there is no
@@ -92,7 +93,7 @@ abstract class JsonCache {
9293
/// **Note**: [value] must be json encodable.
9394
Future<void> refresh(String key, Map<String, dynamic> value);
9495
95-
/// Checks whether there is cached data at [key].
96+
/// Checks for cached data located at [key].
9697
///
9798
/// Returns `true` if there is cached data at [key]; `false` otherwise.
9899
Future<bool> contains(String key);
@@ -119,6 +120,25 @@ final JsonCache jsonCache = … retrieve one of the JsonCache implementations.
119120
await jsonCache.refresh('profile', {'name': 'John Doe', 'email': '[email protected]', 'accountType': 'premium'});
120121
await jsonCache.refresh('preferences', {'theme': {'dark': true}, 'notifications':{'enabled': true}});
121122
```
123+
### Storing Simple Values
124+
125+
In order to store a simple value such as a `string`, `int`, `double`, etc,
126+
define it as a **map key** whose associated value is a boolean placeholder value
127+
set to `true`. For example:
128+
129+
```dart
130+
/// Storing a simple text information.
131+
jsonCache.refresh('info', {'an important information': true});
132+
133+
// later on…
134+
135+
// This variable is a Map containing a single key.
136+
final cachedInfo = await memCache.value('info');
137+
// The key itself is the content of the stored information.
138+
final info = cachedInfo?.keys.first;
139+
print(info); // 'an important information'
140+
141+
```
122142

123143
### Suggested Dependency Relationship
124144

@@ -243,6 +263,19 @@ package.
243263
final JsonCache jsonCache = JsonCacheMem(JsonCacheEncPrefs(encPrefs));
244264
245265
```
266+
### JsonCacheLocalStorage
267+
268+
[JsonCacheLocalStorage](https://pub.dev/documentation/json_cache/latest/json_cache/JsonCacheLocalStorage-class.html)
269+
is an implementation on top of the
270+
[localstorage](https://pub.dev/packages/localstorage) package.
271+
272+
```dart
273+
274+
final LocalStorage storage = LocalStorage('my_data');
275+
final JsonCache jsonCache = JsonCacheMem(JsonCacheLocalStorage(storage));
276+
277+
```
278+
246279

247280
### JsonCacheSecStorage
248281

@@ -256,26 +289,12 @@ is an implementation on top of the
256289
final JsonCache jsonCache = JsonCacheSecStorage(secStorage);
257290
// In order to write a string value, define it as a map key whose associated
258291
// value is a boolean placeholder value set to 'true'.
259-
final Map<String, dynamic> info = {'an secret info': true};
260-
jsonCache.refresh('secret', info);
292+
jsonCache.refresh('secret', {'a secret info': true});
261293
262294
// later on…
263295
264-
final mappedInfo = (await jsonCache.value('secret'))!;
265-
final originalInfo = mappedInfo.keys.first; // 'an secret info'
266-
```
267-
268-
### JsonCacheLocalStorage
269-
270-
[JsonCacheLocalStorage](https://pub.dev/documentation/json_cache/latest/json_cache/JsonCacheLocalStorage-class.html)
271-
is an implementation on top of the
272-
[localstorage](https://pub.dev/packages/localstorage) package.
273-
274-
```dart
275-
276-
final LocalStorage storage = LocalStorage('my_data');
277-
final JsonCache jsonCache = JsonCacheMem(JsonCacheLocalStorage(storage));
278-
296+
final cachedInfo = await jsonCache.value('secret');
297+
final info = cachedInfo?.keys.first; // 'a secret info'
279298
```
280299

281300
### JsonCacheCrossLocalStorage

analysis_options.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ analyzer:
66
implicit-casts: false
77
# A value of false ensures that the type inference engine never chooses the
88
# dynamic type when it can’t determine a static type.
9-
implicit-dynamic: false
9+
# implicit-dynamic: false
1010
linter:
1111
rules:
1212
# Make constructors the first thing in every class

lib/src/json_cache.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
///> elsewhere.
77
///> — [cache Wikipedia](https://en.wikipedia.org/wiki/Cache_(computing))
88
abstract class JsonCache {
9-
/// Frees up storage space — deletes all keys with associated values.
9+
/// Frees up storage space — deletes all keys and values.
1010
Future<void> clear();
1111

1212
/// Removes cached data located at [key].
1313
Future<void> remove(String key);
1414

15-
/// Retrieves cached data located at [key] or null if a cache miss occurs.
15+
/// Retrieves cached data located at [key] or `null` if a cache miss occurs.
1616
Future<Map<String, dynamic>?> value(String key);
1717

1818
/// It either updates the data found at [key] with [value] or, if there is no
@@ -21,7 +21,7 @@ abstract class JsonCache {
2121
/// **Note**: [value] must be json encodable.
2222
Future<void> refresh(String key, Map<String, dynamic> value);
2323

24-
/// Checks whether there is cached data at [key].
24+
/// Checks for cached data located at [key].
2525
///
2626
/// Returns `true` if there is cached data at [key]; `false` otherwise.
2727
Future<bool> contains(String key);

0 commit comments

Comments
 (0)