refactor: wire unified cache and invalidator in di
This commit is contained in:
+27
@@ -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);
|
||||
}
|
||||
@@ -2,6 +2,10 @@ import 'package:dio/dio.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:get_it/get_it.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/i_api_client.dart';
|
||||
import '../storage/token_storage.dart';
|
||||
@@ -56,6 +60,19 @@ Future<void> configureDependencies() async {
|
||||
final sharedPreferences = await SharedPreferences.getInstance();
|
||||
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);
|
||||
sl.registerSingleton<UsersApi>(usersApi);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user