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 TypeMethodDescriptionstatic <V> CacheSerializer<V> compressed(CacheSerializer<V> delegate) Create a compressed serializer wrapper 创建压缩序列化器包装器deserialize(byte[] bytes) Deserialize byte array to value 将字节数组反序列化为值default longestimateSize(V value) Estimate serialized size 估算序列化后的大小static CacheSerializer<byte[]> identity()Create identity serializer for byte arrays 创建字节数组透传序列化器static <V> CacheSerializer<V> jdk()Create JDK serializer using Java serialization 创建使用 Java 序列化的 JDK 序列化器static <V> CacheSerializer<V> Create JSON serializer using reflection (auto-detect Jackson or Gson) 使用反射创建 JSON 序列化器(自动检测 Jackson 或 Gson)static <V> CacheSerializer<V> Create Kryo serializer using reflection 使用反射创建 Kryo 序列化器byte[]Serialize value to byte array 将值序列化为字节数组static CacheSerializer<String> string()Create string serializer 创建字符串序列化器
-
Method Details
-
serialize
Serialize value to byte array 将值序列化为字节数组- Parameters:
value- the value | 值- Returns:
- serialized bytes | 序列化后的字节
-
deserialize
Deserialize byte array to value 将字节数组反序列化为值- Parameters:
bytes- the bytes | 字节数组- Returns:
- deserialized value | 反序列化后的值
-
estimateSize
Estimate serialized size 估算序列化后的大小- Parameters:
value- the value | 值- Returns:
- estimated size in bytes | 估算的字节大小
-
jdk
Create JDK serializer using Java serialization 创建使用 Java 序列化的 JDK 序列化器- Type Parameters:
V- value type | 值类型- Returns:
- JDK serializer | JDK 序列化器
-
string
Create string serializer 创建字符串序列化器- Returns:
- string serializer | 字符串序列化器
-
identity
Create identity serializer for byte arrays 创建字节数组透传序列化器- Returns:
- identity serializer | 透传序列化器
-
json
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
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
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
-