Commit a9b2085d by liqiuwei

完善redis

parent 46aecfb6
package com.secoo.mall.redis.template; package com.secoo.mall.redis.helper;
import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import com.alibaba.fastjson.support.spring.GenericFastJsonRedisSerializer;
import com.secoo.mall.common.util.colletion.CollectionUtil; import com.secoo.mall.common.util.colletion.CollectionUtil;
import com.secoo.mall.common.util.string.StringUtil; import com.secoo.mall.common.util.string.StringUtil;
import lombok.Data;
import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisStringCommands.SetOption; import org.springframework.data.redis.connection.RedisStringCommands.SetOption;
...@@ -9,24 +12,38 @@ import org.springframework.data.redis.connection.jedis.JedisClusterConnection; ...@@ -9,24 +12,38 @@ import org.springframework.data.redis.connection.jedis.JedisClusterConnection;
import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.types.Expiration; import org.springframework.data.redis.core.types.Expiration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer; import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class MatrixRedisTemplate { /**
* 说明:GenericJackson2JsonRedisSerializer性能没有FastJsonRedisSerializer和Jackson2JsonRedisSerializer性能要好,Jackson2JsonRedisSerializer存在反序列失败的问题.
* GenericJackson2JsonRedisSerializer会将基本类型Type保存到redis,增加redis的使用内存。如果存储的对象的属性缺少了,将反序列化失败
* 推荐使用GenericFastJsonRedisSerializer
*/
@Data
public class RedisHelper {
private final String MUTEX_KEY = "mutex";
private final static RedisSerializer DEFAULT_STRING_SERIALIZER = new StringRedisSerializer();
private final static RedisSerializer DEFAULT_JSON_SERIALIZER = new GenericFastJsonRedisSerializer();
private RedisSerializer keySerializer = DEFAULT_STRING_SERIALIZER;
private RedisSerializer hashKeySerializer = DEFAULT_STRING_SERIALIZER;
private RedisSerializer valueSerializer = DEFAULT_JSON_SERIALIZER;
private RedisSerializer hashValueSerializer = DEFAULT_JSON_SERIALIZER;
private RedisTemplate<String, ?> redisTemplate; private RedisTemplate<String, ?> redisTemplate;
public RedisTemplate<String, ?> getRedisTemplate() {
return redisTemplate;
}
public void setRedisTemplate(RedisTemplate<String, ?> redisTemplate) { public RedisHelper(RedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate; this.redisTemplate = redisTemplate;
} }
private final String MUTEX_KEY="mutex";
/** /**
* 设置失效时间 * 设置失效时间
...@@ -38,6 +55,7 @@ public class MatrixRedisTemplate { ...@@ -38,6 +55,7 @@ public class MatrixRedisTemplate {
redisTemplate.execute(new RedisCallback<Object>() { redisTemplate.execute(new RedisCallback<Object>() {
@Override @Override
public Object doInRedis(RedisConnection connection) throws DataAccessException { public Object doInRedis(RedisConnection connection) throws DataAccessException {
connection.expire(getKeySerializer().serialize(k), seconds); connection.expire(getKeySerializer().serialize(k), seconds);
return null; return null;
} }
...@@ -50,8 +68,8 @@ public class MatrixRedisTemplate { ...@@ -50,8 +68,8 @@ public class MatrixRedisTemplate {
* @author zhangxiaosong * @author zhangxiaosong
*/ */
public <K, V> void set(K k, V v) { public <K, V> void set(K k, V v) {
final RedisSerializer keySeri = redisTemplate.getKeySerializer(); final RedisSerializer keySeri = getKeySerializer();
final RedisSerializer valueSeri = redisTemplate.getValueSerializer(); final RedisSerializer valueSeri = getValueSerializer();
redisTemplate.execute(new RedisCallback<Object>() { redisTemplate.execute(new RedisCallback<Object>() {
@Override @Override
public Object doInRedis(RedisConnection connection) throws DataAccessException { public Object doInRedis(RedisConnection connection) throws DataAccessException {
...@@ -67,8 +85,8 @@ public class MatrixRedisTemplate { ...@@ -67,8 +85,8 @@ public class MatrixRedisTemplate {
* @author zhangxiaosong * @author zhangxiaosong
*/ */
public <K, V> Boolean setnx(K k, V v) { public <K, V> Boolean setnx(K k, V v) {
final RedisSerializer keySeri = redisTemplate.getKeySerializer(); final RedisSerializer keySeri = getKeySerializer();
final RedisSerializer valueSeri = redisTemplate.getValueSerializer(); final RedisSerializer valueSeri = getValueSerializer();
return redisTemplate.execute(new RedisCallback<Boolean>() { return redisTemplate.execute(new RedisCallback<Boolean>() {
@Override @Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException { public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
...@@ -81,7 +99,7 @@ public class MatrixRedisTemplate { ...@@ -81,7 +99,7 @@ public class MatrixRedisTemplate {
* 判定key是否存在 * 判定key是否存在
*/ */
public <K> Boolean exists(K k) { public <K> Boolean exists(K k) {
final RedisSerializer keySeri = redisTemplate.getKeySerializer(); final RedisSerializer keySeri = getKeySerializer();
return redisTemplate.execute(new RedisCallback<Boolean>() { return redisTemplate.execute(new RedisCallback<Boolean>() {
@Override @Override
public Boolean doInRedis(RedisConnection connection) throws DataAccessException { public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
...@@ -97,8 +115,8 @@ public class MatrixRedisTemplate { ...@@ -97,8 +115,8 @@ public class MatrixRedisTemplate {
* @author zhangxiaosong * @author zhangxiaosong
*/ */
public <K, V> void set(K k, V v, long seconds) { public <K, V> void set(K k, V v, long seconds) {
final RedisSerializer keySeri = redisTemplate.getKeySerializer(); final RedisSerializer keySeri = getKeySerializer();
final RedisSerializer valueSeri = redisTemplate.getValueSerializer(); final RedisSerializer valueSeri = getValueSerializer();
redisTemplate.execute(new RedisCallback<Object>() { redisTemplate.execute(new RedisCallback<Object>() {
@Override @Override
public Object doInRedis(RedisConnection connection) throws DataAccessException { public Object doInRedis(RedisConnection connection) throws DataAccessException {
...@@ -124,7 +142,7 @@ public class MatrixRedisTemplate { ...@@ -124,7 +142,7 @@ public class MatrixRedisTemplate {
* @param timeUnit 时间单位 * @param timeUnit 时间单位
*/ */
public <K> Long ttl(K k, TimeUnit timeUnit) { public <K> Long ttl(K k, TimeUnit timeUnit) {
final RedisSerializer keySeri = redisTemplate.getKeySerializer(); final RedisSerializer keySeri = getKeySerializer();
return redisTemplate.execute(new RedisCallback<Long>() { return redisTemplate.execute(new RedisCallback<Long>() {
@Override @Override
public Long doInRedis(RedisConnection connection) throws DataAccessException { public Long doInRedis(RedisConnection connection) throws DataAccessException {
...@@ -139,7 +157,7 @@ public class MatrixRedisTemplate { ...@@ -139,7 +157,7 @@ public class MatrixRedisTemplate {
* @param k key * @param k key
*/ */
public <K> Long ttl(K k) { public <K> Long ttl(K k) {
final RedisSerializer keySeri = redisTemplate.getKeySerializer(); final RedisSerializer keySeri = getKeySerializer();
return redisTemplate.execute(new RedisCallback<Long>() { return redisTemplate.execute(new RedisCallback<Long>() {
@Override @Override
public Long doInRedis(RedisConnection connection) throws DataAccessException { public Long doInRedis(RedisConnection connection) throws DataAccessException {
...@@ -155,7 +173,7 @@ public class MatrixRedisTemplate { ...@@ -155,7 +173,7 @@ public class MatrixRedisTemplate {
* @param step 每次增加多少 * @param step 每次增加多少
*/ */
public <K> Long incr(K k, long step) { public <K> Long incr(K k, long step) {
final RedisSerializer keySeri = redisTemplate.getKeySerializer(); final RedisSerializer keySeri = getKeySerializer();
return redisTemplate.execute(new RedisCallback<Long>() { return redisTemplate.execute(new RedisCallback<Long>() {
@Override @Override
public Long doInRedis(RedisConnection connection) throws DataAccessException { public Long doInRedis(RedisConnection connection) throws DataAccessException {
...@@ -175,8 +193,8 @@ public class MatrixRedisTemplate { ...@@ -175,8 +193,8 @@ public class MatrixRedisTemplate {
return; return;
} }
final RedisSerializer keySeri = redisTemplate.getKeySerializer(); final RedisSerializer keySeri = getKeySerializer();
final RedisSerializer valueSeri = redisTemplate.getValueSerializer(); final RedisSerializer valueSeri = getValueSerializer();
redisTemplate.execute(new RedisCallback<Object>() { redisTemplate.execute(new RedisCallback<Object>() {
@Override @Override
public Object doInRedis(RedisConnection connection) throws DataAccessException { public Object doInRedis(RedisConnection connection) throws DataAccessException {
...@@ -185,11 +203,9 @@ public class MatrixRedisTemplate { ...@@ -185,11 +203,9 @@ public class MatrixRedisTemplate {
K k = entry.getKey(); K k = entry.getKey();
V v = entry.getValue(); V v = entry.getValue();
if (seconds == null) { if (seconds == null) {
connection connection.set(keySeri.serialize(k), valueSeri.serialize(v));
.set(keySeri.serialize(k), valueSeri.serialize(v));
} else { } else {
connection connection.set(keySeri.serialize(k), valueSeri.serialize(v),
.set(keySeri.serialize(k), valueSeri.serialize(v),
Expiration.seconds(seconds), Expiration.seconds(seconds),
SetOption.UPSERT); SetOption.UPSERT);
} }
...@@ -203,7 +219,7 @@ public class MatrixRedisTemplate { ...@@ -203,7 +219,7 @@ public class MatrixRedisTemplate {
* 删除指定的可以 * 删除指定的可以
*/ */
public <K> void del(K k) { public <K> void del(K k) {
final RedisSerializer keySeri = redisTemplate.getKeySerializer(); final RedisSerializer keySeri = getKeySerializer();
redisTemplate.execute(new RedisCallback<Object>() { redisTemplate.execute(new RedisCallback<Object>() {
@Override @Override
public Object doInRedis(RedisConnection connection) throws DataAccessException { public Object doInRedis(RedisConnection connection) throws DataAccessException {
...@@ -284,7 +300,7 @@ public class MatrixRedisTemplate { ...@@ -284,7 +300,7 @@ public class MatrixRedisTemplate {
/** /**
* key value get方法 * key value get方法
*/ */
public <K, V> V get(K k, Class<V> v) { public <K, V> V get(K k,Class<V> v) {
if (k == null) { if (k == null) {
return null; return null;
} }
...@@ -401,9 +417,9 @@ public class MatrixRedisTemplate { ...@@ -401,9 +417,9 @@ public class MatrixRedisTemplate {
* @author zhangxiaosong * @author zhangxiaosong
*/ */
public <K, F, V> void hSet(K k, F f, V v) { public <K, F, V> void hSet(K k, F f, V v) {
final RedisSerializer keySeri = redisTemplate.getKeySerializer(); final RedisSerializer keySeri = getKeySerializer();
final RedisSerializer hashKeySeri = redisTemplate.getHashKeySerializer(); final RedisSerializer hashKeySeri = getHashKeySerializer();
final RedisSerializer hashValueSeri = redisTemplate.getHashValueSerializer(); final RedisSerializer hashValueSeri = getHashValueSerializer();
redisTemplate.execute(new RedisCallback<Object>() { redisTemplate.execute(new RedisCallback<Object>() {
@Override @Override
public Object doInRedis(RedisConnection connection) throws DataAccessException { public Object doInRedis(RedisConnection connection) throws DataAccessException {
...@@ -422,9 +438,9 @@ public class MatrixRedisTemplate { ...@@ -422,9 +438,9 @@ public class MatrixRedisTemplate {
* @param v 哈希表值 * @param v 哈希表值
*/ */
public <K, F, V> void hSetNX(K k, F f, V v) { public <K, F, V> void hSetNX(K k, F f, V v) {
final RedisSerializer keySeri = redisTemplate.getKeySerializer(); final RedisSerializer keySeri = getKeySerializer();
final RedisSerializer hashKeySeri = redisTemplate.getHashKeySerializer(); final RedisSerializer hashKeySeri = getHashKeySerializer();
final RedisSerializer hashValueSeri = redisTemplate.getHashValueSerializer(); final RedisSerializer hashValueSeri = getHashValueSerializer();
redisTemplate.execute(new RedisCallback<Object>() { redisTemplate.execute(new RedisCallback<Object>() {
@Override @Override
public Object doInRedis(RedisConnection connection) throws DataAccessException { public Object doInRedis(RedisConnection connection) throws DataAccessException {
...@@ -443,9 +459,9 @@ public class MatrixRedisTemplate { ...@@ -443,9 +459,9 @@ public class MatrixRedisTemplate {
* @param increment 增量 * @param increment 增量
*/ */
public <K, F> Long hIncrBy(K k, F f, Long increment) { public <K, F> Long hIncrBy(K k, F f, Long increment) {
final RedisSerializer keySeri = redisTemplate.getKeySerializer(); final RedisSerializer keySeri = getKeySerializer();
final RedisSerializer hashKeySeri = redisTemplate.getHashKeySerializer(); final RedisSerializer hashKeySeri = getHashKeySerializer();
final RedisSerializer hashValueSeri = redisTemplate.getHashValueSerializer(); final RedisSerializer hashValueSeri = getHashValueSerializer();
return redisTemplate.execute(new RedisCallback<Long>() { return redisTemplate.execute(new RedisCallback<Long>() {
@Override @Override
...@@ -466,9 +482,9 @@ public class MatrixRedisTemplate { ...@@ -466,9 +482,9 @@ public class MatrixRedisTemplate {
if (map == null || map.size() == 0) { if (map == null || map.size() == 0) {
return; return;
} }
final RedisSerializer keySeri = redisTemplate.getKeySerializer(); final RedisSerializer keySeri = getKeySerializer();
final RedisSerializer hashKeySeri = redisTemplate.getHashKeySerializer(); final RedisSerializer hashKeySeri = getHashKeySerializer();
final RedisSerializer hashValueSeri = redisTemplate.getHashValueSerializer(); final RedisSerializer hashValueSeri = getHashValueSerializer();
Map<byte[], byte[]> byteMap = new HashMap<>(); Map<byte[], byte[]> byteMap = new HashMap<>();
for (Map.Entry<F, V> ety : map.entrySet()) { for (Map.Entry<F, V> ety : map.entrySet()) {
byteMap.put(hashKeySeri.serialize(ety.getKey()), byteMap.put(hashKeySeri.serialize(ety.getKey()),
...@@ -554,22 +570,4 @@ public class MatrixRedisTemplate { ...@@ -554,22 +570,4 @@ public class MatrixRedisTemplate {
} }
private RedisSerializer getKeySerializer() {
return redisTemplate.getKeySerializer();
}
private RedisSerializer getValueSerializer() {
return redisTemplate.getValueSerializer();
}
private RedisSerializer getHashKeySerializer() {
return redisTemplate.getHashKeySerializer();
}
private RedisSerializer getHashValueSerializer() {
return redisTemplate.getHashValueSerializer();
}
} }
...@@ -15,6 +15,10 @@ ...@@ -15,6 +15,10 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId> <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.secoo.mall</groupId>
<artifactId>matrix-datahelper-redis-core</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>
\ No newline at end of file
package com.secoo.mall.mybatis.spring.boot.autoconfigure;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
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.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* Created by QIANG
*
* @author QIANG
*/
@Configuration
public class MatrixeRedisAutoConfiguration {
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisSerializer stringSerializer = new StringRedisSerializer();
RedisTemplate<String, String> redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(stringSerializer);
redisTemplate.setValueSerializer(stringSerializer);
redisTemplate.setHashKeySerializer(stringSerializer);
redisTemplate.setHashValueSerializer(stringSerializer);
return redisTemplate;
}
}
package com.secoo.mall.redis.spring.boot.autoconfigure;
import com.secoo.mall.redis.helper.RedisHelper;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
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.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import java.net.UnknownHostException;
/**
* Created by QIANG
*
* @author QIANG
*/
@Configuration
@ConditionalOnClass({RedisOperations.class})
@AutoConfigureBefore({RedisAutoConfiguration.class})
public class MatrixeRedisAutoConfiguration {
@Bean
@ConditionalOnMissingBean(name = {"redisTemplate"})
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) throws UnknownHostException {
RedisTemplate<Object, Object> template = new RedisTemplate();
template.setConnectionFactory(redisConnectionFactory);
return template;
}
@Bean
@ConditionalOnMissingBean(name = "redisHelper")
public RedisHelper redisHelper(@Qualifier("redisTemplate") RedisTemplate redisTemplate) {
return new RedisHelper(redisTemplate);
}
}
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.secoo.mall.redis.spring.boot.autoconfigure.MatrixeRedisAutoConfiguration
\ No newline at end of file
...@@ -172,11 +172,16 @@ ...@@ -172,11 +172,16 @@
<version>1.3.2.RELEASE</version> <version>1.3.2.RELEASE</version>
</dependency> </dependency>
<!--redis--> <!--redis-->
<!-- <dependency> <dependency>
<groupId>com.secoo.mall</groupId>
<artifactId>matrix-datahelper-redis-core</artifactId>
<version>1.3.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.secoo.mall</groupId> <groupId>com.secoo.mall</groupId>
<artifactId>matrix-datahelper-redis-starter</artifactId> <artifactId>matrix-datahelper-redis-starter</artifactId>
<version>1.3.2.RELEASE</version> <version>1.3.2.RELEASE</version>
</dependency>--> </dependency>
<!--普通jar--> <!--普通jar-->
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment