refactor: wire unified cache and invalidator in di

This commit is contained in:
qzl
2026-03-20 15:25:33 +08:00
parent 632db2b68b
commit 1cea877bf1
3 changed files with 55 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
import 'dart:async';
import 'hybrid_cache_store.dart';
class CacheInvalidator {
final HybridCacheStore? _store;
final Set<String> _invalidated = <String>{};
CacheInvalidator({HybridCacheStore? store}) : _store = store;
void invalidate(String key) {
_invalidated.add(key);
final removeFuture = _store?.remove(key);
if (removeFuture != null) {
unawaited(removeFuture);
}
}
void invalidateCalendarDay(DateTime date) {
final month = '${date.year}-${date.month.toString().padLeft(2, '0')}';
final day = '$month-${date.day.toString().padLeft(2, '0')}';
invalidate('calendar:day:$day');
invalidate('calendar:month:$month');
}
bool wasInvalidated(String key) => _invalidated.contains(key);
}
+17
View File
@@ -2,6 +2,10 @@ import 'package:dio/dio.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:get_it/get_it.dart'; import 'package:get_it/get_it.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import '../cache/cache_invalidator.dart';
import '../cache/hybrid_cache_store.dart';
import '../cache/memory_cache_store.dart';
import '../cache/persistent_cache_store.dart';
import '../api/api_client.dart'; import '../api/api_client.dart';
import '../api/i_api_client.dart'; import '../api/i_api_client.dart';
import '../storage/token_storage.dart'; import '../storage/token_storage.dart';
@@ -56,6 +60,19 @@ Future<void> configureDependencies() async {
final sharedPreferences = await SharedPreferences.getInstance(); final sharedPreferences = await SharedPreferences.getInstance();
sl.registerSingleton<SharedPreferences>(sharedPreferences); sl.registerSingleton<SharedPreferences>(sharedPreferences);
final memoryCacheStore = MemoryCacheStore();
final persistentCacheStore = PersistentCacheStore();
final hybridCacheStore = HybridCacheStore(
memory: memoryCacheStore,
persistent: persistentCacheStore,
);
sl.registerSingleton<MemoryCacheStore>(memoryCacheStore);
sl.registerSingleton<PersistentCacheStore>(persistentCacheStore);
sl.registerSingleton<HybridCacheStore>(hybridCacheStore);
sl.registerSingleton<CacheInvalidator>(
CacheInvalidator(store: hybridCacheStore),
);
final usersApi = UsersApi(apiClient); final usersApi = UsersApi(apiClient);
sl.registerSingleton<UsersApi>(usersApi); sl.registerSingleton<UsersApi>(usersApi);
+11
View File
@@ -0,0 +1,11 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:social_app/core/cache/cache_invalidator.dart';
void main() {
test('invalidate calendar day should also invalidate month key', () {
final inv = CacheInvalidator();
inv.invalidateCalendarDay(DateTime(2026, 3, 20));
expect(inv.wasInvalidated('calendar:day:2026-03-20'), true);
expect(inv.wasInvalidated('calendar:month:2026-03'), true);
});
}