11# json_cache
22
3- <center >
4- <img width =" 406 " hight =" 192 " alt =" json cache logo " src =" https://user-images.githubusercontent.com/24878574/119276278-56ef4a80-bbf0-11eb-8701-53a94f24f75b.png " align =" middle " >
5- </center >
3+ <img width =" 406 " hight =" 192 " alt =" json cache logo " src =" https://user-images.githubusercontent.com/24878574/119276278-56ef4a80-bbf0-11eb-8701-53a94f24f75b.png " align =" middle " >
64
75[ ![ EO principles respected
86here] ( https://www.elegantobjects.org/badge.svg )] ( https://www.elegantobjects.org )
@@ -28,6 +26,15 @@ Rultor.com](https://www.rultor.com/b/dartoos-dev/json_cache)](https://www.rultor
2826
2927## Overview
3028
29+
30+ > Cache is a hardware or software component that stores data so that future
31+ > requests for that data can be served faster; the data stored in a cache might
32+ > be the result of an earlier computation or a copy of data stored elsewhere.
33+ >
34+ > — [ Cache_ (computing) (2021, August 22). In Wikipedia, The Free Encyclopedia.
35+ > Retrieved 09:55, August 22,
36+ > 2021] ( https://en.wikipedia.org/wiki/Cache_(computing) )
37+
3138** Json Cache** is an object-oriented package to cache user data locally in json.
3239It can also be thought of as a layer on top of Flutter's local storage packages
3340like [ sharable_preferences] ( https://pub.dev/packages/shared_preferences ) and
@@ -57,16 +64,17 @@ abstract class JsonCache {
5764 /// Frees up storage space.
5865 Future<void> clear();
5966
60- /// Updates data at [key] or creates a new cache line at [key] if there is no
61- /// previous data there.
62- Future<void> refresh(String key, Map<String, dynamic> data);
67+ /// It either updates the data found at [key] with [value] or, if there is no
68+ /// previous data at [key], creates a new cache row at [key] with [value].
69+ ///
70+ /// **Note**: [value] must be json encodable.
71+ Future<void> refresh(String key, Map<String, dynamic> value);
6372
64- /// Removes data from cache at [key] and returns it or returns null if there
65- /// is no data at [key].
66- Future<Map<String, dynamic>?> erase(String key);
73+ /// Removes data from cache at [key].
74+ Future<void> remove(String key);
6775
68- /// Retrieves either the data at [key] or null if a cache miss occurs.
69- Future<Map<String, dynamic>?> recover (String key);
76+ /// Retrieves the data value at [key] or null if a cache miss occurs.
77+ Future<Map<String, dynamic>?> value (String key);
7078}
7179```
7280
@@ -82,7 +90,24 @@ represents the name of a single data group. For example:
8290Above, the _ profile_ key is associated with the profile-related data group,
8391while the _ preferences_ key is associated with the preferences-related data.
8492
85- <!-- @todo #10 Some implementation is needed to add more examples -->
93+ ### JsonCacheMem
94+
95+ It is a thread-safe, in-memory implementation of the ` JsonCache ` interface.
96+ Moreover, it encapsulates a secondary cache or "slower level2 cache". Typically,
97+ the secondary cache instance is responsible for the local cache; that is, it is
98+ the cache instance that persists data on the user's device.
99+
100+ #### JsonCacheMem Usage
101+
102+ Due to the fact that ` JsonCacheMem ` is a decorator, you should always pass
103+ another ` JsonCache ` instance when you instantiates it. For example:
104+
105+ ``` dart
106+ …
107+ final prefs = await SharedPreferences.getInstance();
108+ final JsonCacheMem jsonCache = JsonCacheMem(JsonCachePrefs(prefs));
109+ …
110+ ```
86111
87112## Demo application
88113
0 commit comments