json_cache
json cache is an object-oriented package to serve as a layer on top of local storage packages – packages that persist data locally on the user’s device -, unifying them as an elegant cache api.
in addition, this package gives the programmer great flexibility; it provides a set of classes that can be selected and combined in various ways to address specific caching requirements.
why json?
- because most of the local storage packages available for flutter applications
use json as the data format. - there is an one-to-one relationship between the dart’s built-in type
map<string, dynamic>
and json, which makes json encoding/decoding a
trivial task.
getting started
jsoncache
– the core interface of this package – represents the concept of
cached data. it is defined as:
/// represents a cached json data.
abstract class jsoncache {
/// frees up cache storage space.
future<void> clear();
/// refreshes some cached data by its associated key.
future<void> refresh(string key, map<string, dynamic> data);
/// erases [key] and returns its associated data.
future<map<string, dynamic>?> erase(string key);
/// recovers some cached data; null if a cache miss occurs - no [key] found.
future<map<string, dynamic>?> recover(string key);
}
it’s reasonable to consider each cache entry (pair of key/data) as a group of
related data. thus, it is expected to cache user data in groups, in which a key
represents the name of a single group of data. example:
'profile': {'name': 'john doe', 'email': '[email protected]', 'accounttype': 'premium'};
'preferences': {'theme': {'dark': true}, 'notifications':{'enabled': true}}
above the ‘profile’ key is associated with the group of profile related data;
‘preferences’, with preferences data.
Comments are closed.