Class TenantCache<K,V>
java.lang.Object
cloud.opencode.base.cache.TenantCache<K,V>
- Type Parameters:
K- the type of keys | 键类型V- the type of values | 值类型Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Null-safe: Yes - 空值安全: 是
Tenant Cache - Multi-tenant cache with isolation and per-tenant limits
租户缓存 - 带隔离和租户限制的多租户缓存
Provides tenant-isolated caching with per-tenant size limits, TTL defaults, and statistics. Ideal for SaaS applications requiring cache isolation.
提供租户隔离的缓存,支持每租户容量限制、TTL 默认值和统计信息。 适用于需要缓存隔离的 SaaS 应用。
Features | 主要功能:
- Tenant isolation - 租户隔离
- Per-tenant size limits - 每租户容量限制
- Per-tenant TTL defaults - 每租户 TTL 默认值
- Per-tenant statistics - 每租户统计信息
- Tenant quota enforcement - 租户配额执行
- Cross-tenant operations - 跨租户操作
Usage Examples | 使用示例:
// Create tenant cache - 创建租户缓存
TenantCache<String, User> cache = TenantCache.builder("users")
.defaultMaxSize(1000)
.defaultTtl(Duration.ofMinutes(30))
.tenantQuota("premium", 10000)
.tenantQuota("free", 100)
.build();
// Operations with tenant context - 带租户上下文的操作
cache.put("tenant-1", "user:1", user1);
User user = cache.get("tenant-1", "user:1");
// Using tenant-scoped view - 使用租户作用域视图
Cache<String, User> tenantView = cache.forTenant("tenant-1");
tenantView.put("user:2", user2); // Automatically scoped to tenant-1
// Get per-tenant stats - 获取每租户统计
CacheStats stats = cache.tenantStats("tenant-1");
- Since:
- JDK 25, opencode-base-cache V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for TenantCache TenantCache 构建器static classException thrown when tenant quota is exceeded 租户配额超限时抛出的异常 -
Method Summary
Modifier and TypeMethodDescriptionGet aggregated stats across all tenants 获取所有租户的聚合统计static <K,V> TenantCache.Builder <K, V> Create builder for TenantCache 创建 TenantCache 构建器voidcleanUp()Clean up all tenant caches 清理所有租户缓存booleancontainsKey(String tenantId, K key) Check if key exists for tenant 检查租户的键是否存在Get tenant-scoped cache view 获取租户作用域的缓存视图Get value for tenant and key 获取租户和键对应的值Get value with loader 使用加载器获取值voidinvalidate(String tenantId, K key) Invalidate key for tenant 使租户的键失效voidInvalidate all entries for all tenants 使所有租户的所有条目失效voidinvalidateAll(String tenantId) Invalidate all keys for tenant 使租户的所有键失效name()Get cache name 获取缓存名称voidPut value for tenant 为租户放入值voidPut value with custom TTL 使用自定义 TTL 放入值voidPut all entries for tenant 为租户批量放入voidremoveTenant(String tenantId) Remove tenant and all its cached data 移除租户及其所有缓存数据voidsetTenantQuota(String tenantId, long maxSize) Set tenant quota 设置租户配额voidsetTenantQuota(String tenantId, long maxSize, Duration ttl) Set tenant quota with TTL 设置带 TTL 的租户配额tenants()Get all active tenant IDs 获取所有活跃租户 IDlongtenantSize(String tenantId) Get tenant cache size 获取租户缓存大小tenantStats(String tenantId) Get tenant statistics 获取租户统计信息longGet total size across all tenants 获取所有租户的总大小
-
Method Details
-
builder
Create builder for TenantCache 创建 TenantCache 构建器- Type Parameters:
K- key type | 键类型V- value type | 值类型- Parameters:
name- cache name | 缓存名称- Returns:
- builder | 构建器
-
get
-
get
-
put
-
put
-
putAll
-
invalidate
-
invalidateAll
Invalidate all keys for tenant 使租户的所有键失效- Parameters:
tenantId- tenant identifier | 租户标识
-
invalidateAll
public void invalidateAll()Invalidate all entries for all tenants 使所有租户的所有条目失效 -
containsKey
-
forTenant
-
tenants
-
tenantSize
Get tenant cache size 获取租户缓存大小- Parameters:
tenantId- tenant identifier | 租户标识- Returns:
- cache size | 缓存大小
-
tenantStats
Get tenant statistics 获取租户统计信息- Parameters:
tenantId- tenant identifier | 租户标识- Returns:
- cache stats | 缓存统计
-
aggregatedStats
Get aggregated stats across all tenants 获取所有租户的聚合统计- Returns:
- aggregated stats | 聚合统计
-
removeTenant
Remove tenant and all its cached data 移除租户及其所有缓存数据- Parameters:
tenantId- tenant identifier | 租户标识
-
setTenantQuota
Set tenant quota 设置租户配额- Parameters:
tenantId- tenant identifier | 租户标识maxSize- maximum size | 最大容量
-
setTenantQuota
-
name
-
totalSize
public long totalSize()Get total size across all tenants 获取所有租户的总大小- Returns:
- total size | 总大小
-
cleanUp
public void cleanUp()Clean up all tenant caches 清理所有租户缓存
-