add method

Future<CachedData> add(
  1. String key,
  2. dynamic value, {
  3. Map<String, dynamic> queryParams = const <String, dynamic>{},
})

Add data to cache.

The data should be provided as key, value pair where key is the unique identifier and value is the data to cache. You can also use queryParams to specify the query parameters you used to fetch the data.

Implementation

Future<CachedData> add(
  String key,
  dynamic value, {
  Map<String, dynamic> queryParams = const <String, dynamic>{},
}) async {
  final params = QueryParams(queryParams);
  final dateTime = await _getCurrDateTime();
  final dbData = DatabaseData.fromUserValue(
    value: value,
    key: key,
    queryParams: params,
    dateTime: dateTime,
  );
  final result = CachedData(
    value: value,
    location: CacheLoc.server,
    updatedAt: dateTime,
    lastUsedAt: dateTime,
    useCount: 0,
  );

  await dataStore.upsert(dbData);
  if (config.useMemCache) {
    _memCache.add(
      key,
      params.asStr,
      result.copyWith(location: CacheLoc.memory),
    );
  }

  return result;
}