@@ -23,13 +23,14 @@ Rultor.com](https://www.rultor.com/b/dartoos-dev/json_cache)](https://www.rultor
23
23
24
24
- [ Overview] ( #overview )
25
25
- [ Getting Started] ( #getting-started )
26
+ - [ Storing Simple Values] ( #storing-simple-values )
26
27
- [ Suggested Dependency Relationship] ( #suggested-dependency-relationship )
27
28
- [ Implementations] ( #implementations )
28
29
- [ JsonCacheMem — Thread-safe In-memory cache] ( #jsoncachemem )
29
30
- [ JsonCachePrefs — SharedPreferences] ( #jsoncacheprefs )
30
31
- [ JsonCacheEncPrefs — EncryptedSharedPreferences] ( #jsoncacheencprefs )
31
- - [ JsonCacheSecStorage — FlutterSecureStorage] ( #jsoncachesecstorage )
32
32
- [ JsonCacheLocalStorage — LocalStorage] ( #jsoncachelocalstorage )
33
+ - [ JsonCacheSecStorage — FlutterSecureStorage] ( #jsoncachesecstorage )
33
34
- [ JsonCacheCrossLocalStorage — CrossLocalStorage] ( #jsoncachecrosslocalstorage )
34
35
- [ JsonCacheHive — Hive] ( #jsoncachehive )
35
36
- [ Unit Test Tips] ( #unit-test-tips )
@@ -77,13 +78,13 @@ It is defined as:
77
78
``` dart
78
79
/// Represents cached data in json format.
79
80
abstract class JsonCache {
80
- /// Frees up storage space — deletes all keys with associated values.
81
+ /// Frees up storage space — deletes all keys and values.
81
82
Future<void> clear();
82
83
83
84
/// Removes cached data located at [key].
84
85
Future<void> remove(String key);
85
86
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.
87
88
Future<Map<String, dynamic>?> value(String key);
88
89
89
90
/// It either updates data located at [key] with [value] or, if there is no
@@ -92,7 +93,7 @@ abstract class JsonCache {
92
93
/// **Note**: [value] must be json encodable.
93
94
Future<void> refresh(String key, Map<String, dynamic> value);
94
95
95
- /// Checks whether there is cached data at [key].
96
+ /// Checks for cached data located at [key].
96
97
///
97
98
/// Returns `true` if there is cached data at [key]; `false` otherwise.
98
99
Future<bool> contains(String key);
@@ -119,6 +120,25 @@ final JsonCache jsonCache = … retrieve one of the JsonCache implementations.
119
120
await jsonCache.refresh('profile', {'name': 'John Doe', 'email': '[email protected] ', 'accountType': 'premium'});
120
121
await jsonCache.refresh('preferences', {'theme': {'dark': true}, 'notifications':{'enabled': true}});
121
122
```
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
+ ```
122
142
123
143
### Suggested Dependency Relationship
124
144
@@ -243,6 +263,19 @@ package.
243
263
final JsonCache jsonCache = JsonCacheMem(JsonCacheEncPrefs(encPrefs));
244
264
…
245
265
```
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
+
246
279
247
280
### JsonCacheSecStorage
248
281
@@ -256,26 +289,12 @@ is an implementation on top of the
256
289
final JsonCache jsonCache = JsonCacheSecStorage(secStorage);
257
290
// In order to write a string value, define it as a map key whose associated
258
291
// 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});
261
293
262
294
// later on…
263
295
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'
279
298
```
280
299
281
300
### JsonCacheCrossLocalStorage
0 commit comments