refactor: merge profile cache into unified cache repository
This commit is contained in:
@@ -1,49 +1,30 @@
|
||||
import 'dart:async';
|
||||
|
||||
import '../../../users/data/models/user_response.dart';
|
||||
import 'user_profile_cache_repository.dart';
|
||||
|
||||
class SettingsUserCache {
|
||||
final UserProfileCacheRepository _repository;
|
||||
|
||||
SettingsUserCache(this._repository);
|
||||
|
||||
UserResponse? _cachedUser;
|
||||
Future<UserResponse>? _inflight;
|
||||
int _generation = 0;
|
||||
|
||||
UserResponse? get cachedUser => _cachedUser;
|
||||
|
||||
Future<UserResponse> getOrLoad(Future<UserResponse> Function() loader) {
|
||||
final cached = _cachedUser;
|
||||
if (cached != null) {
|
||||
return Future<UserResponse>.value(cached);
|
||||
}
|
||||
|
||||
final inflight = _inflight;
|
||||
if (inflight != null) {
|
||||
return inflight;
|
||||
}
|
||||
|
||||
final generation = _generation;
|
||||
late final Future<UserResponse> request;
|
||||
request = loader()
|
||||
.then((user) {
|
||||
if (generation == _generation) {
|
||||
_cachedUser = user;
|
||||
}
|
||||
return user;
|
||||
})
|
||||
.whenComplete(() {
|
||||
if (identical(_inflight, request)) {
|
||||
_inflight = null;
|
||||
}
|
||||
});
|
||||
|
||||
_inflight = request;
|
||||
return request;
|
||||
Future<UserResponse> getProfile({bool forceRefresh = false}) async {
|
||||
final user = await _repository.getProfile(forceRefresh: forceRefresh);
|
||||
_cachedUser = user;
|
||||
return user;
|
||||
}
|
||||
|
||||
void set(UserResponse user) {
|
||||
_cachedUser = user;
|
||||
unawaited(_repository.setCached(user));
|
||||
}
|
||||
|
||||
void invalidate() {
|
||||
_generation += 1;
|
||||
_cachedUser = null;
|
||||
_inflight = null;
|
||||
unawaited(_repository.invalidate());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
import 'dart:async';
|
||||
|
||||
import '../../../../core/cache/cache_entry.dart';
|
||||
import '../../../../core/cache/cache_policy.dart';
|
||||
import '../../../../core/cache/hybrid_cache_store.dart';
|
||||
import '../../../users/data/models/user_response.dart';
|
||||
|
||||
class UserProfileCacheRepository {
|
||||
static const String cacheKey = 'settings:user_profile';
|
||||
|
||||
final HybridCacheStore store;
|
||||
final CachePolicy policy;
|
||||
final DateTime Function() now;
|
||||
final Future<UserResponse> Function() remoteLoader;
|
||||
|
||||
Future<void>? _refreshInFlight;
|
||||
|
||||
UserProfileCacheRepository({
|
||||
required this.store,
|
||||
required this.remoteLoader,
|
||||
CachePolicy? policy,
|
||||
DateTime Function()? now,
|
||||
}) : policy =
|
||||
policy ??
|
||||
const CachePolicy(
|
||||
softTtl: Duration(minutes: 2),
|
||||
hardTtl: Duration(minutes: 30),
|
||||
minRefreshInterval: Duration(minutes: 1),
|
||||
),
|
||||
now = now ?? DateTime.now;
|
||||
|
||||
Future<UserResponse> getProfile({bool forceRefresh = false}) async {
|
||||
if (forceRefresh) {
|
||||
return _refreshAndRead();
|
||||
}
|
||||
|
||||
final cached = await store.read<CacheEntry<UserResponse>>(cacheKey);
|
||||
if (cached == null) {
|
||||
return _refreshAndRead();
|
||||
}
|
||||
|
||||
final decision = policy.evaluate(now: now(), fetchedAt: cached.fetchedAt);
|
||||
if (decision.shouldRefreshInBackground) {
|
||||
_refreshInBackground();
|
||||
}
|
||||
if (decision.mustBlockForNetwork || !decision.canUseCached) {
|
||||
return _refreshAndRead();
|
||||
}
|
||||
return cached.value;
|
||||
}
|
||||
|
||||
Future<void> setCached(UserResponse user) {
|
||||
return store.write<CacheEntry<UserResponse>>(
|
||||
cacheKey,
|
||||
CacheEntry<UserResponse>(value: user, fetchedAt: now()),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> invalidate() => store.remove(cacheKey);
|
||||
|
||||
void _refreshInBackground() {
|
||||
final running = _refreshInFlight;
|
||||
if (running != null) {
|
||||
return;
|
||||
}
|
||||
final task = _refreshAndWrite().whenComplete(() {
|
||||
_refreshInFlight = null;
|
||||
});
|
||||
_refreshInFlight = task;
|
||||
unawaited(task);
|
||||
}
|
||||
|
||||
Future<UserResponse> _refreshAndRead() async {
|
||||
await _refreshAndWrite();
|
||||
final cached = await store.read<CacheEntry<UserResponse>>(cacheKey);
|
||||
return cached!.value;
|
||||
}
|
||||
|
||||
Future<void> _refreshAndWrite() async {
|
||||
final remote = await remoteLoader();
|
||||
await setCached(remote);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user