hydrate method
Restores entries from the PersistentStore into the in-memory store.
Call once at startup (e.g. before runApp) to make cached data available
immediately. Expired records are skipped and pruned from disk. Records
without a matching registered codec are left untouched on disk and not
loaded. Returns the number of entries restored.
No-op (returns 0) when no PersistentStore is configured.
New in v1.2.0.
Implementation
Future<int> hydrate() async {
final store = _persistentStore;
if (store == null) return 0;
final records = await store.loadAll();
final now = _now();
var restored = 0;
for (final record in records) {
final codec = _codecFor(record.key);
if (codec == null) continue;
final createdAt = DateTime.fromMillisecondsSinceEpoch(record.createdAtMs);
final ttl =
record.ttlMs == null ? null : Duration(milliseconds: record.ttlMs!);
final entry = CacheEntry<dynamic>(
value: codec.decode(record.value),
createdAt: createdAt,
ttl: ttl,
);
if (entry.isExpiredAt(now)) {
_track(store.remove(record.key));
continue;
}
_store.write(record.key, entry);
final scope = _scopeFromName(record.scope);
if (scope != CacheScope.global) {
_scopes[record.key] = _ScopeMeta(scope, record.userId);
}
restored++;
}
return restored;
}