Files
SpotiFLAC-Mobile/lib/services/explore_cache_store.dart
zarzet d125c50fac perf(storage): move preference blobs to atomic files
Keep Explore payloads and timestamps, autoplay learning, and release bodies with their validators in application-support snapshots. Serialize migration, replacement, metadata updates and clear across store instances. Flush and rename before removing legacy keys, retain data on failure, and use a tombstone to prevent clear from reviving old preferences.

Preserve full payload content and unrelated settings. Explore reads are asynchronous and report actual file length; delayed release revalidation updates current metadata without overwriting a newer body.

The 261281-byte host preference fixture drops to 21 bytes after migration. Sixty subsequent blob saves make zero preference writes, with only three initial removals. Migration, corruption, failure/retry, ordering and all three consumer paths pass scoped host tests.
2026-11-01 20:30:50 +07:00

46 lines
1.2 KiB
Dart

import 'package:shared_preferences/shared_preferences.dart';
import 'package:mornye/services/preference_blob_store.dart';
class ExploreCacheSnapshot {
const ExploreCacheSnapshot({
required this.payload,
required this.timestampMs,
required this.sizeBytes,
});
final String? payload;
final int? timestampMs;
final int sizeBytes;
}
class ExploreCacheStore {
const ExploreCacheStore(this._prefs);
static const _cacheKey = 'explore_home_feed_cache';
static const _cacheTsKey = 'explore_home_feed_ts';
static const _blobs = PreferenceBlobStore('explore_feed', {
_cacheKey: String,
_cacheTsKey: int,
});
final SharedPreferences _prefs;
Future<ExploreCacheSnapshot> read() async {
final snapshot = await _blobs.read(_prefs);
return ExploreCacheSnapshot(
payload: snapshot.values[_cacheKey] as String?,
timestampMs: snapshot.values[_cacheTsKey] as int?,
sizeBytes: snapshot.sizeBytes,
);
}
Future<void> write(String payload) async {
await _blobs.write(_prefs, {
_cacheKey: payload,
_cacheTsKey: DateTime.now().millisecondsSinceEpoch,
});
}
Future<void> clear() => _blobs.clear(_prefs);
}