feat: implement hybrid cache store with singleflight

This commit is contained in:
qzl
2026-03-20 15:23:56 +08:00
parent 035ca46bd0
commit 632db2b68b
5 changed files with 135 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:social_app/core/cache/hybrid_cache_store.dart';
import 'package:social_app/core/cache/memory_cache_store.dart';
import 'package:social_app/core/cache/persistent_cache_store.dart';
void main() {
test('same key concurrent load should execute loader once', () async {
var calls = 0;
final store = HybridCacheStore(
memory: MemoryCacheStore(),
persistent: PersistentCacheStore(),
);
Future<String> loader() async {
calls += 1;
await Future<void>.delayed(const Duration(milliseconds: 20));
return 'ok';
}
await Future.wait([
store.getOrLoad<String>('k', loader: loader),
store.getOrLoad<String>('k', loader: loader),
]);
expect(calls, 1);
});
}