cache manager配置

This commit is contained in:
GYJ
2025-02-10 14:24:21 +08:00
parent f7a5b7c9f7
commit babfa6a8ca
4 changed files with 87 additions and 2 deletions

View File

@@ -0,0 +1,38 @@
package com.czg.config;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import java.nio.charset.StandardCharsets;
/**
* @author GYJoker
*/
public class FastJson2RedisSerializer<T> implements RedisSerializer<T> {
private final Class<T> clazz;
public FastJson2RedisSerializer(Class<T> clazz) {
this.clazz = clazz;
}
@Override
public byte[] serialize(T t) throws SerializationException {
if (t == null) {
return new byte[0];
}
return JSON.toJSONString(t, JSONWriter.Feature.WriteClassName).getBytes(StandardCharsets.UTF_8);
}
@Override
public T deserialize(byte[] bytes) throws SerializationException {
if (bytes == null || bytes.length == 0) {
return null;
}
String str = new String(bytes, StandardCharsets.UTF_8);
return JSON.parseObject(str, clazz, JSONReader.Feature.SupportAutoType);
}
}