Interface ValueWeigher<V>

Type Parameters:
V - the type of values | 值类型
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface ValueWeigher<V>
Value Weigher - Calculate weight of cache entries for memory-based eviction 值权重计算器 - 计算缓存条目权重用于基于内存的淘汰

Used to calculate the weight of cache values for memory-aware caching. When cache reaches maximum weight, entries are evicted based on their weight.

用于计算缓存值的权重以实现内存感知的缓存。当缓存达到最大权重时,根据权重淘汰条目。

Features | 主要功能:

  • Custom weight calculation - 自定义权重计算
  • Memory-based eviction support - 基于内存的淘汰支持
  • Built-in weighers for common types - 常见类型的内置权重计算器

Usage Examples | 使用示例:

// Fixed weight per entry - 每个条目固定权重
ValueWeigher<String> fixed = ValueWeigher.fixed(1);

// String length as weight - 字符串长度作为权重
ValueWeigher<String> stringWeigher = ValueWeigher.stringLength();

// Collection size as weight - 集合大小作为权重
ValueWeigher<List<User>> listWeigher = ValueWeigher.collectionSize();

// Custom weigher - 自定义权重计算器
ValueWeigher<User> userWeigher = user -> user.getDataSize();

// Use in cache config - 在缓存配置中使用
Cache<String, User> cache = OpenCache.builder()
    .maximumWeight(1_000_000)  // 1MB
    .weigher(user -> user.getSerializedSize())
    .build();

Security | 安全性:

  • Thread-safe: Depends on implementation - 线程安全: 取决于实现
  • Weight must be non-negative - 权重必须非负
Since:
JDK 25, opencode-base-cache V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static interface 
    Entry weigher interface for key-value pairs 键值对的条目权重计算器接口
  • Method Summary

    Modifier and Type
    Method
    Description
    static <V> ValueWeigher<V>
    Create a weigher using array length 创建使用数组长度的权重计算器
    default ValueWeigher<V>
    bounded(long minWeight, long maxWeight)
    Create a weigher bounded between min and max 创建在最小和最大之间的权重计算器
    static ValueWeigher<byte[]>
    Create a weigher using byte array length 创建使用字节数组长度的权重计算器
    static <V extends Collection<?>>
    ValueWeigher<V>
    Create a weigher using collection size 创建使用集合大小的权重计算器
    static <K,V> ValueWeigher.EntryWeigher<K,V>
    combined(ValueWeigher<K> keyWeigher, ValueWeigher<V> valueWeigher)
    Create a weigher that considers both key and value 创建同时考虑键和值的权重计算器
    static <V> ValueWeigher<V>
    Create a weigher that estimates memory size for common types 创建估算常见类型内存大小的权重计算器
    static <V> ValueWeigher<V>
    fixed(long weight)
    Create a weigher with fixed weight per entry 创建每个条目固定权重的权重计算器
    static <V extends Map<?,?>>
    ValueWeigher<V>
    Create a weigher using map size 创建使用 Map 大小的权重计算器
    Create a weigher using string length 创建使用字符串长度的权重计算器
    default ValueWeigher<V>
    times(long multiplier)
    Create a weigher that applies a multiplier 创建应用乘数的权重计算器
    long
    weigh(V value)
    Calculate the weight of a value 计算值的权重
    default ValueWeigher<V>
    withMaximum(long maxWeight)
    Create a weigher with maximum weight 创建带最大权重的权重计算器
    default ValueWeigher<V>
    withMinimum(long minWeight)
    Create a weigher with minimum weight 创建带最小权重的权重计算器
  • Method Details

    • weigh

      long weigh(V value)
      Calculate the weight of a value 计算值的权重
      Parameters:
      value - the value to weigh | 要计算权重的值
      Returns:
      the weight (must be non-negative) | 权重(必须非负)
    • fixed

      static <V> ValueWeigher<V> fixed(long weight)
      Create a weigher with fixed weight per entry 创建每个条目固定权重的权重计算器
      Type Parameters:
      V - the value type | 值类型
      Parameters:
      weight - the fixed weight | 固定权重
      Returns:
      the weigher | 权重计算器
    • stringLength

      static ValueWeigher<String> stringLength()
      Create a weigher using string length 创建使用字符串长度的权重计算器
      Returns:
      the weigher | 权重计算器
    • byteArrayLength

      static ValueWeigher<byte[]> byteArrayLength()
      Create a weigher using byte array length 创建使用字节数组长度的权重计算器
      Returns:
      the weigher | 权重计算器
    • collectionSize

      static <V extends Collection<?>> ValueWeigher<V> collectionSize()
      Create a weigher using collection size 创建使用集合大小的权重计算器
      Type Parameters:
      V - the collection type | 集合类型
      Returns:
      the weigher | 权重计算器
    • mapSize

      static <V extends Map<?,?>> ValueWeigher<V> mapSize()
      Create a weigher using map size 创建使用 Map 大小的权重计算器
      Type Parameters:
      V - the map type | Map 类型
      Returns:
      the weigher | 权重计算器
    • arrayLength

      static <V> ValueWeigher<V> arrayLength()
      Create a weigher using array length 创建使用数组长度的权重计算器
      Type Parameters:
      V - the array type | 数组类型
      Returns:
      the weigher | 权重计算器
    • estimatedMemory

      static <V> ValueWeigher<V> estimatedMemory()
      Create a weigher that estimates memory size for common types 创建估算常见类型内存大小的权重计算器

      Estimates memory usage based on type:

      • String: length * 2 + 40 (approx char array + header)
      • byte[]: length + 16 (array + header)
      • Collection: size * 8 + 40 (references + header)
      • Map: size * 16 + 40 (key + value refs + header)
      • Other: 40 (minimum object overhead)
      Type Parameters:
      V - the value type | 值类型
      Returns:
      the weigher | 权重计算器
    • times

      default ValueWeigher<V> times(long multiplier)
      Create a weigher that applies a multiplier 创建应用乘数的权重计算器
      Parameters:
      multiplier - the multiplier | 乘数
      Returns:
      the scaled weigher | 缩放后的权重计算器
    • withMinimum

      default ValueWeigher<V> withMinimum(long minWeight)
      Create a weigher with minimum weight 创建带最小权重的权重计算器
      Parameters:
      minWeight - the minimum weight | 最小权重
      Returns:
      the bounded weigher | 带边界的权重计算器
    • withMaximum

      default ValueWeigher<V> withMaximum(long maxWeight)
      Create a weigher with maximum weight 创建带最大权重的权重计算器
      Parameters:
      maxWeight - the maximum weight | 最大权重
      Returns:
      the bounded weigher | 带边界的权重计算器
    • bounded

      default ValueWeigher<V> bounded(long minWeight, long maxWeight)
      Create a weigher bounded between min and max 创建在最小和最大之间的权重计算器
      Parameters:
      minWeight - the minimum weight | 最小权重
      maxWeight - the maximum weight | 最大权重
      Returns:
      the bounded weigher | 带边界的权重计算器
    • combined

      static <K,V> ValueWeigher.EntryWeigher<K,V> combined(ValueWeigher<K> keyWeigher, ValueWeigher<V> valueWeigher)
      Create a weigher that considers both key and value 创建同时考虑键和值的权重计算器
      Type Parameters:
      K - the key type | 键类型
      V - the value type | 值类型
      Parameters:
      keyWeigher - the key weigher | 键权重计算器
      valueWeigher - the value weigher | 值权重计算器
      Returns:
      the combined weigher | 组合的权重计算器