Files
social-app/apps/lib/core/cache/cache_policy.dart
T

50 lines
1.2 KiB
Dart
Raw Normal View History

2026-03-20 15:21:52 +08:00
class CacheDecision {
final bool canUseCached;
final bool shouldRefreshInBackground;
final bool mustBlockForNetwork;
const CacheDecision({
required this.canUseCached,
required this.shouldRefreshInBackground,
required this.mustBlockForNetwork,
});
}
class CachePolicy {
final Duration softTtl;
final Duration hardTtl;
final Duration minRefreshInterval;
const CachePolicy({
required this.softTtl,
required this.hardTtl,
this.minRefreshInterval = Duration.zero,
});
CacheDecision evaluate({required DateTime now, required DateTime fetchedAt}) {
final age = now.difference(fetchedAt);
if (age >= hardTtl) {
return const CacheDecision(
canUseCached: false,
shouldRefreshInBackground: false,
mustBlockForNetwork: true,
);
}
if (age >= softTtl) {
final shouldRefresh = age >= minRefreshInterval;
return CacheDecision(
canUseCached: true,
shouldRefreshInBackground: shouldRefresh,
mustBlockForNetwork: false,
);
}
return const CacheDecision(
canUseCached: true,
shouldRefreshInBackground: false,
mustBlockForNetwork: false,
);
}
}