文章目录 Redis使用FastJson序列化 Redis配置类 Redis工具类
Redis使用FastJson序列化
< ! -- fastjson依赖-- > < dependency> < groupId> com. alibaba< / groupId> < artifactId> fastjson< / artifactId> < version> 1.2 .33 < / version> < / dependency>
import com. alibaba. fastjson. JSON;
import com. alibaba. fastjson. serializer. SerializerFeature ;
import com. fasterxml. jackson. databind. JavaType ;
import com. fasterxml. jackson. databind. ObjectMapper ;
import com. fasterxml. jackson. databind. type. TypeFactory ;
import org. springframework. data. redis. serializer. RedisSerializer ;
import org. springframework. data. redis. serializer. SerializationException ;
import com. alibaba. fastjson. parser. ParserConfig ;
import org. springframework. util. Assert ;
import java. nio. charset. Charset ;
public class FastJsonRedisSerializer < T > implements RedisSerializer < T >
{ public static final Charset DEFAULT_CHARSET = Charset . forName ( "UTF-8" ) ; private Class < T > clazz; static { ParserConfig . getGlobalInstance ( ) . setAutoTypeSupport ( true ) ; } public FastJsonRedisSerializer ( Class < T > clazz) { super ( ) ; this . clazz = clazz; } @Override public byte [ ] serialize ( T t) throws SerializationException { if ( t == null ) { return new byte [ 0 ] ; } return JSON. toJSONString ( t, 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) ; return JSON. parseObject ( str, clazz) ; } protected JavaType getJavaType ( Class < ? > clazz) { return TypeFactory . defaultInstance ( ) . constructType ( clazz) ; }
}
Redis配置类
import org. springframework. context. annotation. Bean ;
import org. springframework. context. annotation. Configuration ;
import org. springframework. data. redis. connection. RedisConnectionFactory ;
import org. springframework. data. redis. core. RedisTemplate ;
import org. springframework. data. redis. serializer. StringRedisSerializer ; @Configuration
public class RedisConfig { @Bean @SuppressWarnings ( value = { "unchecked" , "rawtypes" } ) public RedisTemplate < Object , Object > redisTemplate ( RedisConnectionFactory connectionFactory) { RedisTemplate < Object , Object > template = new RedisTemplate < > ( ) ; template. setConnectionFactory ( connectionFactory) ; FastJsonRedisSerializer serializer = new FastJsonRedisSerializer ( Object . class ) ; template. setKeySerializer ( new StringRedisSerializer ( ) ) ; template. setValueSerializer ( serializer) ; template. setHashKeySerializer ( new StringRedisSerializer ( ) ) ; template. setHashValueSerializer ( serializer) ; template. afterPropertiesSet ( ) ; return template; }
}
Redis工具类
import java. util. * ;
import java. util. concurrent. TimeUnit ; @SuppressWarnings ( value = { "unchecked" , "rawtypes" } )
@Component
public class RedisCache
{ @Autowired public RedisTemplate redisTemplate; public < T > void setCacheObject ( final String key, final T value) { redisTemplate. opsForValue ( ) . set ( key, value) ; } public < T > void setCacheObject ( final String key, final T value, final Integer timeout, final TimeUnit timeUnit) { redisTemplate. opsForValue ( ) . set ( key, value, timeout, timeUnit) ; } public boolean expire ( final String key, final long timeout) { return expire ( key, timeout, TimeUnit . SECONDS) ; } public boolean expire ( final String key, final long timeout, final TimeUnit unit) { return redisTemplate. expire ( key, timeout, unit) ; } public < T > T getCacheObject ( final String key) { ValueOperations < String , T > operation = redisTemplate. opsForValue ( ) ; return operation. get ( key) ; } public boolean deleteObject ( final String key) { return redisTemplate. delete ( key) ; } public long deleteObject ( final Collection collection) { return redisTemplate. delete ( collection) ; } public < T > long setCacheList ( final String key, final List < T > dataList) { Long count = redisTemplate. opsForList ( ) . rightPushAll ( key, dataList) ; return count == null ? 0 : count; } public < T > List < T > getCacheList ( final String key) { return redisTemplate. opsForList ( ) . range ( key, 0 , - 1 ) ; } public < T > BoundSetOperations < String , T > setCacheSet ( final String key, final Set < T > dataSet) { BoundSetOperations < String , T > setOperation = redisTemplate. boundSetOps ( key) ; Iterator < T > it = dataSet. iterator ( ) ; while ( it. hasNext ( ) ) { setOperation. add ( it. next ( ) ) ; } return setOperation; } public < T > Set < T > getCacheSet ( final String key) { return redisTemplate. opsForSet ( ) . members ( key) ; } public < T > void setCacheMap ( final String key, final Map < String , T > dataMap) { if ( dataMap != null ) { redisTemplate. opsForHash ( ) . putAll ( key, dataMap) ; } } public < T > Map < String , T > getCacheMap ( final String key) { return redisTemplate. opsForHash ( ) . entries ( key) ; } public < T > void setCacheMapValue ( final String key, final String hKey, final T value) { redisTemplate. opsForHash ( ) . put ( key, hKey, value) ; } public < T > T getCacheMapValue ( final String key, final String hKey) { HashOperations < String , String , T > opsForHash = redisTemplate. opsForHash ( ) ; return opsForHash. get ( key, hKey) ; } public void delCacheMapValue ( final String key, final String hkey) { HashOperations hashOperations = redisTemplate. opsForHash ( ) ; hashOperations. delete ( key, hkey) ; } public < T > List < T > getMultiCacheMapValue ( final String key, final Collection < Object > hKeys) { return redisTemplate. opsForHash ( ) . multiGet ( key, hKeys) ; } public Collection < String > keys ( final String pattern) { return redisTemplate. keys ( pattern) ; }
}