提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.idempotent.core.redis; |
H |
2 |
|
|
3 |
import lombok.AllArgsConstructor; |
|
4 |
import org.springframework.data.redis.core.StringRedisTemplate; |
|
5 |
|
|
6 |
import java.util.concurrent.TimeUnit; |
|
7 |
|
|
8 |
/** |
|
9 |
* 幂等 Redis DAO |
|
10 |
* |
|
11 |
* @author iailab |
|
12 |
*/ |
|
13 |
@AllArgsConstructor |
|
14 |
public class IdempotentRedisDAO { |
|
15 |
|
|
16 |
/** |
|
17 |
* 幂等操作 |
|
18 |
* |
|
19 |
* KEY 格式:idempotent:%s // 参数为 uuid |
|
20 |
* VALUE 格式:String |
|
21 |
* 过期时间:不固定 |
|
22 |
*/ |
|
23 |
private static final String IDEMPOTENT = "idempotent:%s"; |
|
24 |
|
|
25 |
private final StringRedisTemplate redisTemplate; |
|
26 |
|
|
27 |
public Boolean setIfAbsent(String key, long timeout, TimeUnit timeUnit) { |
|
28 |
String redisKey = formatKey(key); |
|
29 |
return redisTemplate.opsForValue().setIfAbsent(redisKey, "", timeout, timeUnit); |
|
30 |
} |
|
31 |
|
|
32 |
public void delete(String key) { |
|
33 |
String redisKey = formatKey(key); |
|
34 |
redisTemplate.delete(redisKey); |
|
35 |
} |
|
36 |
|
|
37 |
private static String formatKey(String key) { |
|
38 |
return String.format(IDEMPOTENT, key); |
|
39 |
} |
|
40 |
|
|
41 |
} |