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 - 空值安全: 是

public class TenantCache<K,V> extends Object
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:
  • Method Details

    • builder

      public static <K,V> TenantCache.Builder<K,V> builder(String name)
      Create builder for TenantCache 创建 TenantCache 构建器
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      name - cache name | 缓存名称
      Returns:
      builder | 构建器
    • get

      public V get(String tenantId, K key)
      Get value for tenant and key 获取租户和键对应的值
      Parameters:
      tenantId - tenant identifier | 租户标识
      key - the key | 键
      Returns:
      value or null | 值或 null
    • get

      public V get(String tenantId, K key, Function<? super K, ? extends V> loader)
      Get value with loader 使用加载器获取值
      Parameters:
      tenantId - tenant identifier | 租户标识
      key - the key | 键
      loader - loader function | 加载函数
      Returns:
      value | 值
    • put

      public void put(String tenantId, K key, V value)
      Put value for tenant 为租户放入值
      Parameters:
      tenantId - tenant identifier | 租户标识
      key - the key | 键
      value - the value | 值
    • put

      public void put(String tenantId, K key, V value, Duration ttl)
      Put value with custom TTL 使用自定义 TTL 放入值
      Parameters:
      tenantId - tenant identifier | 租户标识
      key - the key | 键
      value - the value | 值
      ttl - time to live | 存活时间
    • putAll

      public void putAll(String tenantId, Map<? extends K, ? extends V> map)
      Put all entries for tenant 为租户批量放入
      Parameters:
      tenantId - tenant identifier | 租户标识
      map - entries to put | 要放入的条目
    • invalidate

      public void invalidate(String tenantId, K key)
      Invalidate key for tenant 使租户的键失效
      Parameters:
      tenantId - tenant identifier | 租户标识
      key - the key | 键
    • invalidateAll

      public void invalidateAll(String tenantId)
      Invalidate all keys for tenant 使租户的所有键失效
      Parameters:
      tenantId - tenant identifier | 租户标识
    • invalidateAll

      public void invalidateAll()
      Invalidate all entries for all tenants 使所有租户的所有条目失效
    • containsKey

      public boolean containsKey(String tenantId, K key)
      Check if key exists for tenant 检查租户的键是否存在
      Parameters:
      tenantId - tenant identifier | 租户标识
      key - the key | 键
      Returns:
      true if exists | 如果存在返回 true
    • forTenant

      public Cache<K,V> forTenant(String tenantId)
      Get tenant-scoped cache view 获取租户作用域的缓存视图
      Parameters:
      tenantId - tenant identifier | 租户标识
      Returns:
      tenant-scoped cache | 租户作用域缓存
    • tenants

      public Set<String> tenants()
      Get all active tenant IDs 获取所有活跃租户 ID
      Returns:
      set of tenant IDs | 租户 ID 集合
    • tenantSize

      public long tenantSize(String tenantId)
      Get tenant cache size 获取租户缓存大小
      Parameters:
      tenantId - tenant identifier | 租户标识
      Returns:
      cache size | 缓存大小
    • tenantStats

      public CacheStats tenantStats(String tenantId)
      Get tenant statistics 获取租户统计信息
      Parameters:
      tenantId - tenant identifier | 租户标识
      Returns:
      cache stats | 缓存统计
    • aggregatedStats

      public CacheStats aggregatedStats()
      Get aggregated stats across all tenants 获取所有租户的聚合统计
      Returns:
      aggregated stats | 聚合统计
    • removeTenant

      public void removeTenant(String tenantId)
      Remove tenant and all its cached data 移除租户及其所有缓存数据
      Parameters:
      tenantId - tenant identifier | 租户标识
    • setTenantQuota

      public void setTenantQuota(String tenantId, long maxSize)
      Set tenant quota 设置租户配额
      Parameters:
      tenantId - tenant identifier | 租户标识
      maxSize - maximum size | 最大容量
    • setTenantQuota

      public void setTenantQuota(String tenantId, long maxSize, Duration ttl)
      Set tenant quota with TTL 设置带 TTL 的租户配额
      Parameters:
      tenantId - tenant identifier | 租户标识
      maxSize - maximum size | 最大容量
      ttl - default TTL | 默认 TTL
    • name

      public String name()
      Get cache name 获取缓存名称
      Returns:
      cache name | 缓存名称
    • totalSize

      public long totalSize()
      Get total size across all tenants 获取所有租户的总大小
      Returns:
      total size | 总大小
    • cleanUp

      public void cleanUp()
      Clean up all tenant caches 清理所有租户缓存