Interface CacheSerializer<V>

Type Parameters:
V - the type of values | 值类型

public interface CacheSerializer<V>
Cache Serializer SPI - Cache value serialization interface 缓存序列化器 SPI - 缓存值序列化接口

Provides interface for serializing and deserializing cache values.

提供缓存值序列化和反序列化的接口。

Features | 主要功能:

  • Serialize to bytes - 序列化为字节
  • Deserialize from bytes - 从字节反序列化
  • Size estimation - 大小估算

Usage Examples | 使用示例:

CacheSerializer<User> serializer = CacheSerializer.jdk();
byte[] bytes = serializer.serialize(user);
User user = serializer.deserialize(bytes);

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: No (throws on null) - 空值安全: 否(null 时抛异常)

Performance | 性能特性:

  • Time complexity: O(n) for serialize/deserialize where n is the data size - 时间复杂度: serialize/deserialize 为 O(n),n为数据大小
  • Space complexity: O(n) for the serialized byte array - 空间复杂度: O(n),存储序列化字节数组
Since:
JDK 25, opencode-base-cache V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static <V> CacheSerializer<V>
    Create a compressed serializer wrapper 创建压缩序列化器包装器
    deserialize(byte[] bytes)
    Deserialize byte array to value 将字节数组反序列化为值
    default long
    estimateSize(V value)
    Estimate serialized size 估算序列化后的大小
    static CacheSerializer<byte[]>
    Create identity serializer for byte arrays 创建字节数组透传序列化器
    static <V> CacheSerializer<V>
    jdk()
    Create JDK serializer using Java serialization 创建使用 Java 序列化的 JDK 序列化器
    static <V> CacheSerializer<V>
    json(Class<V> type)
    Create JSON serializer using reflection (auto-detect Jackson or Gson) 使用反射创建 JSON 序列化器(自动检测 Jackson 或 Gson)
    static <V> CacheSerializer<V>
    kryo(Class<V> type)
    Create Kryo serializer using reflection 使用反射创建 Kryo 序列化器
    byte[]
    serialize(V value)
    Serialize value to byte array 将值序列化为字节数组
    Create string serializer 创建字符串序列化器
  • Method Details

    • serialize

      byte[] serialize(V value)
      Serialize value to byte array 将值序列化为字节数组
      Parameters:
      value - the value | 值
      Returns:
      serialized bytes | 序列化后的字节
    • deserialize

      V deserialize(byte[] bytes)
      Deserialize byte array to value 将字节数组反序列化为值
      Parameters:
      bytes - the bytes | 字节数组
      Returns:
      deserialized value | 反序列化后的值
    • estimateSize

      default long estimateSize(V value)
      Estimate serialized size 估算序列化后的大小
      Parameters:
      value - the value | 值
      Returns:
      estimated size in bytes | 估算的字节大小
    • jdk

      static <V> CacheSerializer<V> jdk()
      Create JDK serializer using Java serialization 创建使用 Java 序列化的 JDK 序列化器
      Type Parameters:
      V - value type | 值类型
      Returns:
      JDK serializer | JDK 序列化器
    • string

      static CacheSerializer<String> string()
      Create string serializer 创建字符串序列化器
      Returns:
      string serializer | 字符串序列化器
    • identity

      static CacheSerializer<byte[]> identity()
      Create identity serializer for byte arrays 创建字节数组透传序列化器
      Returns:
      identity serializer | 透传序列化器
    • json

      static <V> CacheSerializer<V> json(Class<V> type)
      Create JSON serializer using reflection (auto-detect Jackson or Gson) 使用反射创建 JSON 序列化器(自动检测 Jackson 或 Gson)

      Tries to detect and use Jackson first, then Gson. Falls back to basic toString/fromString if neither is available.

      优先尝试检测使用 Jackson,然后是 Gson。如果都不可用则回退到基本的 toString/fromString。

      Type Parameters:
      V - value type | 值类型
      Parameters:
      type - the class type for deserialization | 反序列化的类类型
      Returns:
      JSON serializer | JSON 序列化器
      Since:
      V1.9.0
    • kryo

      static <V> CacheSerializer<V> kryo(Class<V> type)
      Create Kryo serializer using reflection 使用反射创建 Kryo 序列化器

      Kryo provides high-performance serialization. Falls back to JDK serialization if Kryo is not available.

      Kryo 提供高性能序列化。如果 Kryo 不可用则回退到 JDK 序列化。

      Type Parameters:
      V - value type | 值类型
      Parameters:
      type - the class type | 类类型
      Returns:
      Kryo serializer or JDK fallback | Kryo 序列化器或 JDK 回退
      Since:
      V1.9.0
    • compressed

      static <V> CacheSerializer<V> compressed(CacheSerializer<V> delegate)
      Create a compressed serializer wrapper 创建压缩序列化器包装器

      Wraps another serializer and applies GZIP compression.

      包装另一个序列化器并应用 GZIP 压缩。

      Type Parameters:
      V - value type | 值类型
      Parameters:
      delegate - the underlying serializer | 底层序列化器
      Returns:
      compressed serializer | 压缩序列化器
      Since:
      V1.9.0