Spring RedisTemplate 扩展支持 FastJson:GenericFastJson2JsonRedisSerializer


声明:本文转载自https://my.oschina.net/jsonavaj/blog/1595017,转载目的在于传递更多信息,仅供学习交流之用。如有侵权行为,请联系我,我会及时删除。

类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>

 

本文发表于2017年12月26日 14:32
(c)注:本文转载自https://my.oschina.net/jsonavaj/blog/1595017,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除.

阅读 3965 讨论 0 喜欢 0

抢先体验

扫码体验
趣味小程序
文字表情生成器

闪念胶囊

你要过得好哇,这样我才能恨你啊,你要是过得不好,我都不知道该恨你还是拥抱你啊。

直抵黄龙府,与诸君痛饮尔。

那时陪伴我的人啊,你们如今在何方。

不出意外的话,我们再也不会见了,祝你前程似锦。

这世界真好,吃野东西也要留出这条命来看看

快捷链接
网站地图
提交友链
Copyright © 2016 - 2021 Cion.
All Rights Reserved.
京ICP备2021004668号-1