类GenericFastJson2JsonRedisSerializer
package com.utils.redis; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference; import com.alibaba.fastjson.serializer.SerializerFeature; import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.SerializationException; import java.nio.charset.Charset; /* * GenericFastJson2JsonRedisSerializer */ public class GenericFastJson2JsonRedisSerializer<T> implements RedisSerializer<T> { public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); private FastJsonWraper<T> wraper; public GenericFastJson2JsonRedisSerializer() { super(); this.wraper=new FastJsonWraper<>(); } @Override public byte[] serialize(T t) throws SerializationException { if (t == null) { return new byte[0]; } wraper.setValue(t); return JSON.toJSONString(wraper, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET); } @Override public T deserialize(byte[] bytes) throws SerializationException { if (bytes == null || bytes.length <= 0) { return null; } String str = new String(bytes, DEFAULT_CHARSET); FastJsonWraper wraper=JSON.parseObject(str,FastJsonWraper.class); return (T)wraper.getValue(); } }
类FastJsonWraper
package com.utils.redis; /** * Created by jsonzou on 2017/12/26. */ public class FastJsonWraper<T> { private T value; public FastJsonWraper() { } public FastJsonWraper(T value) { this.value = value; } public T getValue() { return value; } public void setValue(T value) { this.value = value; } }
spring-redis 配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <description>Spring-redis</description> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="50" /> <!--最大连接数--> <property name="maxIdle" value="10" /> <!--最大空闲数--> <property name="maxWaitMillis" value="3000" /> <!--最大等待时间ms--> <property name="testOnBorrow" value="true" /> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="127.0.0.1" /> <property name="port" value="9500"/> <property name="password" value="redis"/> <property name="poolConfig" ref="jedisPoolConfig" /> <property name="usePool" value="true"/> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory" /> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="valueSerializer"> <bean class="com.utils.redis.GenericFastJson2JsonRedisSerializer" /> </property> <property name="hashKeySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/> </property> <property name="hashValueSerializer"> <bean class="com.utils.redis.GenericFastJson2JsonRedisSerializer"/> </property> </bean> </beans>