put<T> method

Future<void> put<T>(
  1. String key,
  2. T value, {
  3. int? seconds,
})

Stores a value in the cache.

key is the unique identifier for the cached item. value is the value to be stored. seconds is an optional parameter for setting an expiration time.

Implementation

Future<void> put<T>(String key, T value, {int? seconds}) async {
  final File cacheFile = File('${_cacheDirectory.path}/$key');
  final Map<String, dynamic> data = {'value': value};
  if (seconds != null) {
    data['expiration'] =
        DateTime.now().add(Duration(seconds: seconds)).toIso8601String();
  }
  await cacheFile.writeAsString(jsonEncode(data));
}