read method

Future<ApiCacheRead?> read(
  1. String originalKey, {
  2. bool allowStale = false,
})

Implementation

Future<ApiCacheRead?> read(
  String originalKey, {
  bool allowStale = false,
}) async {
  if (!config.enabled) return null;
  final key = _storageKey(originalKey);
  var entry = _memory[key];
  var source = ApiCacheSource.memory;
  if (entry == null) {
    entry = _CacheEntry.fromMap(_box.get(key));
    source = ApiCacheSource.hive;
    if (entry != null) _memory[key] = entry;
  }
  if (entry == null) return null;
  if (entry.sessionHash != _activeSessionHash) return null;
  if (entry.isStale && !allowStale) return null;
  entry.lastAccessedAtMs = DateTime.now().millisecondsSinceEpoch;
  if (source == ApiCacheSource.hive) {
    await _box.put(key, entry.toMap());
  }
  return ApiCacheRead(
    data: entry.data,
    source: source,
    isStale: entry.isStale,
    storedAt: DateTime.fromMillisecondsSinceEpoch(entry.storedAtMs),
    expiresAt: DateTime.fromMillisecondsSinceEpoch(entry.expiresAtMs),
  );
}