setCache method

Future<void> setCache({
  1. required String key,
  2. required dynamic value,
  3. Duration? ttl,
})

Persist value under key.

  • Non-strings are jsonEncoded when possible; otherwise value.toString() is used.
  • ttl: if null, the entry never auto-expires; if provided, the entry is considered fresh until (now - savedAt) > ttl.

Implementation

Future<void> setCache({
  required String key,
  required dynamic value,
  Duration? ttl,
}) async {
  final String storedValue = value is String ? value : _safeJsonEncode(value);

  final jsonString = jsonEncode({
    'value': storedValue,
    'savedAt': DateTime.now().millisecondsSinceEpoch,
    'ttl': ttl?.inMilliseconds,
  });
  await _localStorageManager.setString(key: key, value: jsonString);
}