putAll method

  1. @override
Future<void> putAll(
  1. Map<String, CacheItem> entries
)
override

Stores multiple values in the cache with the given keys.

Each value is wrapped in a CacheItem object, which allows for optional expiry.

Throws a CacheException if there is an error storing the data.

Implementation

@override
Future<void> putAll(Map<String, CacheItem<dynamic>> entries) async {
  final p = await prefs;
  final Map<String, String> stringEntries = {};

  for (final entry in entries.entries) {
    final key = _getKey(entry.key);
    final item = entry.value;
    final Map<String, dynamic> jsonMap = {
      'value': item.value,
      'expiry': item.expiry?.millisecondsSinceEpoch,
    };

    String jsonString = jsonEncode(jsonMap);
    if (enableEncryption) {
      jsonString = _encrypt(jsonString);
    }

    stringEntries[key] = jsonString;
  }

  await p.setString(_getKey('batch_temp'), 'temp');
  for (final entry in stringEntries.entries) {
    await p.setString(entry.key, entry.value);
  }
  await p.remove(_getKey('batch_temp'));
}