2026-03-30 09:06:24 +08:00
|
|
|
typedef CacheScopeProvider = String? Function();
|
|
|
|
|
|
|
|
|
|
class CacheScope {
|
|
|
|
|
CacheScope._();
|
|
|
|
|
|
|
|
|
|
static CacheScopeProvider? _provider;
|
|
|
|
|
|
|
|
|
|
static void configureProvider(CacheScopeProvider provider) {
|
|
|
|
|
_provider = provider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void resetProvider() {
|
|
|
|
|
_provider = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static String token() {
|
2026-03-31 18:26:36 +08:00
|
|
|
final value = maybeToken();
|
|
|
|
|
if (value == null) {
|
|
|
|
|
throw StateError('CacheScope not configured - user must be logged in');
|
|
|
|
|
}
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static String? maybeToken() {
|
2026-03-30 09:06:24 +08:00
|
|
|
final raw = _provider?.call()?.trim();
|
|
|
|
|
if (raw == null || raw.isEmpty) {
|
2026-03-31 18:26:36 +08:00
|
|
|
return null;
|
2026-03-30 09:06:24 +08:00
|
|
|
}
|
|
|
|
|
return raw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static String scopedKey(String key, {String? scopeToken}) {
|
|
|
|
|
final scope = scopeToken ?? token();
|
|
|
|
|
return 'cache:$scope:$key';
|
|
|
|
|
}
|
|
|
|
|
}
|