feat: 添加缓存作用域支持多用户数据隔离

This commit is contained in:
zl-q
2026-03-30 09:06:24 +08:00
parent 4285b4ec80
commit 41f35d6e22
4 changed files with 72 additions and 14 deletions
+28
View File
@@ -0,0 +1,28 @@
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() {
final raw = _provider?.call()?.trim();
if (raw == null || raw.isEmpty) {
return 'anonymous';
}
return raw;
}
static String scopedKey(String key, {String? scopeToken}) {
final scope = scopeToken ?? token();
return 'cache:$scope:$key';
}
}