yangyoupeng

redis序列化修改默认key:string,value:genericJson

......@@ -61,8 +61,18 @@
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.1</version>
</dependency>
</dependencies>
<build>
......
......@@ -7,9 +7,14 @@ import org.springframework.cache.Cache;
import org.springframework.data.redis.cache.RedisCache;
import org.springframework.data.redis.cache.RedisCacheElement;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import com.zhaoonline.redis.serialize.SafeEncodeSerializer;
import com.zhaoonline.redis.template.RedisTemplateFactory;
import redis.clients.util.SafeEncoder;
/**
* class name:CacheOnRedis <BR>
* class description: 基于redis的cache <BR>
......@@ -33,13 +38,36 @@ public class CacheOnRedis {
* @param prefix <BR>
*/
public CacheOnRedis(RedisTemplateFactory factory,String cacheName,byte[] prefix){
// redisTemplate = factory.createTemplate();
// cache= new RedisCache(cacheName, prefix, redisTemplate, TimeUnit.MINUTES.toSeconds(10));
this(factory,cacheName,prefix,new SafeEncodeSerializer(),new GenericJackson2JsonRedisSerializer());
}
public CacheOnRedis(RedisTemplateFactory factory,String cacheName,byte[] prefix,RedisSerializer keySerializer,RedisSerializer valueSerializer){
redisTemplate = factory.createTemplate();
redisTemplate.setKeySerializer(keySerializer);
if(valueSerializer !=null){
redisTemplate.setValueSerializer(valueSerializer);
}
cache= new RedisCache(cacheName, prefix, redisTemplate, TimeUnit.MINUTES.toSeconds(10));
}
public CacheOnRedis(RedisTemplateFactory factory,String cacheName,byte[] prefix,long exipreTime){
// redisTemplate = factory.createTemplate();
// cache= new RedisCache(cacheName, prefix, redisTemplate,exipreTime);
this(factory,cacheName,prefix,exipreTime,new SafeEncodeSerializer(),null);
}
public CacheOnRedis(RedisTemplateFactory factory,String cacheName,byte[] prefix,long exipreTime,RedisSerializer keySerializer,RedisSerializer valueSerializer){
redisTemplate = factory.createTemplate();
redisTemplate.setKeySerializer(keySerializer);
if(valueSerializer !=null){
redisTemplate.setValueSerializer(valueSerializer);
}
cache= new RedisCache(cacheName, prefix, redisTemplate,exipreTime);
}
/**
* Method name: put <BR>
* Description: put <BR>
......
......@@ -10,7 +10,10 @@ import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import com.zhaoonline.redis.serialize.SafeEncodeSerializer;
import com.zhaoonline.redis.template.DeSerializer;
import com.zhaoonline.redis.template.RedisTemplateFactory;
import com.zhaoonline.redis.transaction.TransactionValueOperation;
......@@ -26,7 +29,7 @@ import com.zhaoonline.redis.transaction.TransactionValueOperation;
public class ExcludeLock {
private Logger logger=LoggerFactory.getLogger(ExcludeLock.class);
private RedisTemplate<String, String> redisTemplate;
private DeSerializer deSerializer = new DeSerializer();
// private DeSerializer deSerializer = new DeSerializer();
private static final String sperator = ":";
private String lockNamespace = "lock" + sperator;
private final int lockExpiryInMillis;
......@@ -46,22 +49,30 @@ public class ExcludeLock {
public ExcludeLock(RedisTemplateFactory factory, String lockName) {
this(factory, lockName, DEFAULT_ACQUIRE_TIMEOUT_MILLIS);
this(factory, lockName,new SafeEncodeSerializer(),new GenericJackson2JsonRedisSerializer());
}
public ExcludeLock(RedisTemplateFactory factory, String lockName,RedisSerializer keySerializer,RedisSerializer valueSerializer) {
this(factory, lockName, DEFAULT_ACQUIRE_TIMEOUT_MILLIS,keySerializer,valueSerializer);
}
public ExcludeLock(RedisTemplateFactory factory, String lockName, int acquireTimeoutMillis) {
this(factory, lockName, DEFAULT_ACQUIRE_TIMEOUT_MILLIS, DEFAULT_EXPIRY_TIME_MILLIS, UUID.randomUUID());
public ExcludeLock(RedisTemplateFactory factory, String lockName, int acquireTimeoutMillis,RedisSerializer keySerializer,RedisSerializer valueSerializer) {
this(factory, lockName, DEFAULT_ACQUIRE_TIMEOUT_MILLIS, DEFAULT_EXPIRY_TIME_MILLIS, UUID.randomUUID(),keySerializer,valueSerializer);
}
public ExcludeLock(RedisTemplateFactory factory, String lockName, int acquireTimeoutMillis, int expiryTimeMillis) {
this(factory, lockName, acquireTimeoutMillis, expiryTimeMillis, UUID.randomUUID());
this(factory, lockName, acquireTimeoutMillis, expiryTimeMillis, UUID.randomUUID(),new SafeEncodeSerializer(),new GenericJackson2JsonRedisSerializer());
}
public ExcludeLock(RedisTemplateFactory factory, String lockName, int acquireTimeoutMillis, int expiryTimeMillis,
UUID uuid) {
UUID uuid,RedisSerializer keySerializer,RedisSerializer valueSerializer) {
redisTemplate = factory.createTemplate();
redisTemplate.setKeySerializer(keySerializer);
if(valueSerializer !=null){
redisTemplate.setValueSerializer(valueSerializer);
}
// redisTemplate.setEnableTransactionSupport(true);
deSerializer.setValueSerializer(redisTemplate.getValueSerializer());
// deSerializer.setValueSerializer(redisTemplate.getValueSerializer());
lockExpiryInMillis = expiryTimeMillis;
acquiryTimeoutInMillis = acquireTimeoutMillis;
lockKeyPath = lockNamespace + lockName;
......
......@@ -8,8 +8,11 @@ import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import com.zhaoonline.redis.template.RedisTemplateFactory;
import com.zhaoonline.redis.serialize.SafeEncodeSerializer;
import com.zhaoonline.redis.template.OperationBuilder;
/**
......@@ -22,8 +25,18 @@ import com.zhaoonline.redis.template.OperationBuilder;
public class DataHashRepository<V> {
private OperationBuilder<V> builder;
private RedisTemplate<String,V> redisTemplate;
@Autowired
public DataHashRepository(RedisTemplateFactory factory){
this(factory,new SafeEncodeSerializer());
}
@Autowired
public DataHashRepository(RedisTemplateFactory factory,RedisSerializer keySerializer){
this(factory,keySerializer,new GenericJackson2JsonRedisSerializer());
}
@Autowired
public DataHashRepository(RedisTemplateFactory factory,RedisSerializer keySerializer,RedisSerializer valueSerializer){
builder= new OperationBuilder<V>(factory);
redisTemplate = builder.template();
}
......
......@@ -10,7 +10,10 @@ import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import com.zhaoonline.redis.serialize.SafeEncodeSerializer;
import com.zhaoonline.redis.template.DeSerializer;
import com.zhaoonline.redis.template.RedisTemplateFactory;
import com.zhaoonline.redis.template.OperationBuilder;
......@@ -22,9 +25,21 @@ public class DataListRepository<V> {
@Autowired
public DataListRepository(RedisTemplateFactory factory){
this(factory,new SafeEncodeSerializer());
}
@Autowired
public DataListRepository(RedisTemplateFactory factory,RedisSerializer keySerializer){
this(factory,keySerializer,new GenericJackson2JsonRedisSerializer());
}
@Autowired
public DataListRepository(RedisTemplateFactory factory,RedisSerializer keySerializer,RedisSerializer valueSerializer){
builder= new OperationBuilder<V>(factory);
redisTemplate = builder.template();
deSerializer.setValueSerializer(redisTemplate.getValueSerializer());
redisTemplate.setKeySerializer(keySerializer);
redisTemplate.setValueSerializer(valueSerializer);
deSerializer.setValueSerializer(redisTemplate.getValueSerializer());
}
/**
......
......@@ -12,7 +12,10 @@ import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import com.zhaoonline.redis.serialize.SafeEncodeSerializer;
import com.zhaoonline.redis.template.DeSerializer;
import com.zhaoonline.redis.template.RedisTemplateFactory;
import com.zhaoonline.redis.template.OperationBuilder;
......@@ -24,11 +27,24 @@ public class DataSetRepository<V> {
@Autowired
public DataSetRepository(RedisTemplateFactory factory){
this(factory,new SafeEncodeSerializer());
}
@Autowired
public DataSetRepository(RedisTemplateFactory factory,RedisSerializer keySerializer){
this(factory,keySerializer,new GenericJackson2JsonRedisSerializer());
}
@Autowired
public DataSetRepository(RedisTemplateFactory factory,RedisSerializer keySerializer,RedisSerializer valueSerializer){
builder= new OperationBuilder<V>(factory);
redisTemplate = builder.template();
redisTemplate.setKeySerializer(keySerializer);
redisTemplate.setValueSerializer(valueSerializer);
deSerializer.setValueSerializer(redisTemplate.getValueSerializer());
}
/**
* Method name: set <BR>
* Description: 将values添加到key的set中,如果value已经存在了就会忽略,若key不在就会创建一个新的set <BR>
......
......@@ -8,15 +8,25 @@ import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import com.zhaoonline.redis.serialize.SafeEncodeSerializer;
import com.zhaoonline.redis.template.OperationBuilder;
import com.zhaoonline.redis.template.RedisTemplateFactory;
public class DataStringRepository {
private RedisTemplate<String,String> redisTemplate;
@Autowired
public DataStringRepository(RedisTemplateFactory factory){
this(factory,new SafeEncodeSerializer(),new SafeEncodeSerializer());
}
public DataStringRepository(RedisTemplateFactory factory,RedisSerializer keySerializer,RedisSerializer valueSerializer){
redisTemplate = factory.createTemplate();
redisTemplate.setKeySerializer(keySerializer);
redisTemplate.setValueSerializer(valueSerializer);
}
/**
* Method name: append <BR>
* Description: 将String 类型的value append到key的值之后, <BR>
......
......@@ -5,8 +5,12 @@ import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import com.zhaoonline.redis.template.RedisTemplateFactory;
import com.zhaoonline.redis.serialize.SafeEncodeSerializer;
import com.zhaoonline.redis.template.OperationBuilder;
public class DataValueRepository<V> {
......@@ -17,10 +21,24 @@ public class DataValueRepository<V> {
@Autowired
public DataValueRepository(RedisTemplateFactory factory){
this(factory,new SafeEncodeSerializer());
}
@Autowired
public DataValueRepository(RedisTemplateFactory factory,RedisSerializer keySerializer){
this(factory,keySerializer,new GenericJackson2JsonRedisSerializer());
}
@Autowired
public DataValueRepository(RedisTemplateFactory factory,RedisSerializer keySerializer,RedisSerializer valueSerializer){
builder= new OperationBuilder<V>(factory);
redisTemplate = builder.template();
redisTemplate.setKeySerializer(keySerializer);
redisTemplate.setValueSerializer(valueSerializer);
}
/**
* Method name: setIfAbsent <BR>
* Description: 若key不存在,成功set,返回true,若key存在,则返回false; <BR>
......
package com.zhaoonline.redis.serialize;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import redis.clients.util.SafeEncoder;
public class SafeEncodeSerializer implements RedisSerializer<String>{
@Override
public byte[] serialize(String t) throws SerializationException {
return SafeEncoder.encode(t);
}
@Override
public String deserialize(byte[] bytes) throws SerializationException {
if(bytes==null || bytes.length ==0){
return null;
}
return SafeEncoder.encode(bytes);
}
}
......@@ -23,8 +23,11 @@ public class MultiSubmitToken {
* @return Long<BR>
*/
public Long getSubmmitToken(String submitID){
Long submittime=token.getToken(submitID);
return submittime;
Object value=token.getToken(submitID);
if(value !=null){
return Long.valueOf(String.valueOf(value));
}
return null;
}
/**
......
......@@ -5,7 +5,10 @@ import java.util.concurrent.TimeUnit;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import com.zhaoonline.redis.serialize.SafeEncodeSerializer;
import com.zhaoonline.redis.template.RedisTemplateFactory;
/**
......@@ -31,15 +34,22 @@ public class Token<T> {
this(factory,DEFAULT_EXPIRY_TIME_MILLIS,defaultTokenNameSpace);
}
public Token(RedisTemplateFactory factory,int expiryTimeMillis){
this(factory,expiryTimeMillis,defaultTokenNameSpace);
}
public Token(RedisTemplateFactory factory,int expiryTimeMillis,String tokenNameSpace){
this(factory,expiryTimeMillis,defaultTokenNameSpace,new SafeEncodeSerializer(),new GenericJackson2JsonRedisSerializer() );
}
public Token(RedisTemplateFactory factory,int expiryTimeMillis,RedisSerializer keySerializer,RedisSerializer valueSerializer){
this(factory,expiryTimeMillis,defaultTokenNameSpace,new SafeEncodeSerializer(),new GenericJackson2JsonRedisSerializer() );
}
public Token(RedisTemplateFactory factory,int expiryTimeMillis,String tokenNameSpace,RedisSerializer keySerializer,RedisSerializer valueSerializer){
redisTemplate=factory.createTemplate();
redisTemplate.setKeySerializer(keySerializer);
redisTemplate.setValueSerializer(valueSerializer);
tokenExpiryInMillis = expiryTimeMillis;
this.tokenNameSpace=tokenNameSpace;
}
public boolean addToken(String key,T token){
BoundValueOperations<String, T> valuOper = redisTemplate.boundValueOps(buildTokenKeyWithNameSpace(key));
valuOper.expire(getTokenExpiryInMillis(), TimeUnit.MILLISECONDS);
......
......@@ -8,6 +8,7 @@ import static org.hamcrest.MatcherAssert.*;
import org.hamcrest.core.IsEqual;
import static org.hamcrest.CoreMatchers.*;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.util.Assert;
import com.zhaoonline.redis.template.RedisTemplateFactory;
......@@ -29,6 +30,7 @@ public class TestDataValueRepository {
User user=new User();
user.setId(0);
user.setName("yyp");
userRepositroy.delete("data:repo:test1");
userRepositroy.set("data:repo:test1", user);
User user2=userRepositroy.get("data:repo:test1");
......@@ -41,6 +43,19 @@ public class TestDataValueRepository {
}
@Test
public void testDelete(){
DataValueRepository<User> userRepositroy=new DataValueRepository<User>(factory);
Object value=userRepositroy.get("alpaca:product:dict:product:T161100017");
Assert.notNull(value);
userRepositroy.delete("alpaca:product:dict:product:T161100017");
}
@After
public void after(){
connectionFactory.destroy();
......@@ -51,6 +66,7 @@ public class TestDataValueRepository {
factory.setHostName("192.168.0.188");
factory.setPort(6377);
factory.setUsePool(true);
factory.setDatabase(7);
factory.afterPropertiesSet();
return factory;
}
......