@@ -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.
7980abstract 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.
119120await jsonCache.refresh('profile', {'name': 'John Doe', 'email': 'johndoe@email.com', 'accountType': 'premium'});
120121await 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
0 commit comments