Java redis 序列化和反序列化例子
时间: 2023-11-11 16:01:35 浏览: 54
很高兴帮助你解答问题。以下是Java redis序列化和反序列化的示例代码:
Redis序列化对象:
```
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.util.Assert;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class RedisObjectSerializer implements RedisSerializer<Object> {
private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
public RedisObjectSerializer() {
super();
}
public byte[] serialize(Object obj) throws SerializationException {
if (obj == null) {
return EMPTY_BYTE_ARRAY;
}
try {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(128);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteStream);
objectOutputStream.writeObject(obj);
objectOutputStream.flush();
return byteStream.toByteArray();
} catch (Throwable ex) {
throw new SerializationException("Failed to serialize", ex);
}
}
public Object deserialize(byte[] bytes) throws SerializationException {
if (isEmpty(bytes)) {
return null;
}
try {
ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(bytes));
return objectInputStream.readObject();
} catch (Throwable ex) {
throw new SerializationException("Failed to deserialize", ex);
}
}
private boolean isEmpty(byte[] data) {
return (data == null || data.length == 0);
}
}
```
Redis反序列化对象:
```
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.util.Assert;
import java.nio.charset.Charset;
public class RedisStringSerializer implements RedisSerializer<String> {
private final Charset charset;
public RedisStringSerializer() {
this(Charset.forName("UTF8"));
}
public RedisStringSerializer(Charset charset) {
Assert.notNull(charset, "Charset must not be null!");
this.charset = charset;
}
public String deserialize(byte[] bytes) {
return (bytes == null ? null : new String(bytes, charset));
}
public byte[] serialize(String string) throws SerializationException {
return (string == null ? null : string.getBytes(charset));
}
}
```
请注意,这里使用了Spring Data Redis库,并实现了RedisSerializer接口。您可以根据需要使用其他Redis客户端库和其他序列化器。希望这可以帮助到您!
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)