TransactionValueOperation.java
4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.zhaoonline.redis.transaction;
import java.util.concurrent.TimeUnit;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.TimeoutUtils;
import com.zhaoonline.redis.template.DeSerializer;
import com.zhaoonline.redis.template.RedisTemplateFactory;
import redis.clients.util.SafeEncoder;
/**
* class name:TransactionValueOperation <BR>
* class description: 所有的操作都是基于Transaction的<BR>
* Remark: <BR>
* @version 1.00 2016年8月22日
* @author zhaoonline)yangyoupeng
*/
public class TransactionValueOperation<V> {
private RedisTemplate<String,V> redisTemplate;
private DeSerializer deSerializer=new DeSerializer();
public TransactionValueOperation(RedisTemplateFactory factory){
redisTemplate = factory.createTemplate();
deSerializer.setValueSerializer(redisTemplate.getValueSerializer());
deSerializer.setKeySerializer(redisTemplate.getKeySerializer());
}
public void setIfAbsent(String key,V value){
final byte[] rawKey = deSerializer.rawKey(key);
final byte[] rawValue = deSerializer.rawValue(value);
redisTemplate.execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) throws DataAccessException {
connection.watch(rawKey);
connection.multi();
connection.setNX(rawKey, rawValue);
connection.exec();
connection.unwatch();
return null;
}
});
}
public void set(String key,V value){
final byte[] rawValue = deSerializer.rawValue(value);
final byte[] rawKey = deSerializer.rawKey(key);
redisTemplate.execute(new RedisCallback<Object>() {
@Override
public Object doInRedis(RedisConnection connection) throws DataAccessException {
connection.watch(rawKey);
connection.multi();
connection.set(rawKey, rawValue);
connection.exec();
connection.unwatch();
return null;
}
});
}
public void set(String key,V value,long expireTimeount,TimeUnit unit){
final byte[] rawKey = deSerializer.rawKey(key);
final byte[] rawValue = deSerializer.rawValue(value);
redisTemplate.execute(new RedisCallback<Object>() {
public Object doInRedis(RedisConnection connection) throws DataAccessException {
potentiallyUsePsetEx(connection);
return null;
}
public void potentiallyUsePsetEx(RedisConnection connection) {
if (!TimeUnit.MILLISECONDS.equals(unit) || !failsafeInvokePsetEx(connection)) {
connection.watch(rawKey);
connection.multi();
connection.setEx(rawKey, TimeoutUtils.toSeconds(expireTimeount, unit), rawValue);
connection.exec();
connection.unwatch();
}
}
private boolean failsafeInvokePsetEx(RedisConnection connection) {
boolean failed = false;
try {
connection.pSetEx(rawKey, expireTimeount, rawValue);
} catch (UnsupportedOperationException e) {
// in case the connection does not support pSetEx return false to allow fallback to other operation.
failed = true;
}
return !failed;
}
}, true);
}
public V get(String key){
final byte[] rawKey = deSerializer.rawKey(key);
return redisTemplate.execute(new RedisCallback<V>() {
@Override
public V doInRedis(RedisConnection connection) throws DataAccessException {
byte[] value=connection.get(rawKey);
V valueObject=(V)deSerializer.getValueSerializer().deserialize(value);
return valueObject;
}
});
}
/**
* Method name: delete <BR>
* Description: <BR>
* Remark: <BR>
* @param key void<BR>
*/
public void delete(String key){
final byte[] rawKey = deSerializer.rawKey(key);
redisTemplate.execute(new RedisCallback<Object>() {
public Object doInRedis(RedisConnection connection) {
connection.del(rawKey);
return null;
}
});
}
public RedisTemplate<String, V> getRedisTemplate() {
return redisTemplate;
}
}