yangyoupeng

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

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