refactor
(cherry picked from commit c4424f07) (cherry picked from commit 04bf6a8243d48163c1534984993eefe9249d0ff1)
Showing
1 changed file
with
17 additions
and
5 deletions
1 | package com.yohoufo.common.cache; | 1 | package com.yohoufo.common.cache; |
2 | 2 | ||
3 | +import com.google.common.collect.Lists; | ||
3 | import com.yoho.core.redis.cluster.annotation.Redis; | 4 | import com.yoho.core.redis.cluster.annotation.Redis; |
4 | import com.yoho.core.redis.cluster.operations.nosync.YHRedisTemplate; | 5 | import com.yoho.core.redis.cluster.operations.nosync.YHRedisTemplate; |
5 | import com.yoho.core.redis.cluster.operations.nosync.YHValueOperations; | 6 | import com.yoho.core.redis.cluster.operations.nosync.YHValueOperations; |
6 | import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder; | 7 | import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder; |
7 | import lombok.extern.slf4j.Slf4j; | 8 | import lombok.extern.slf4j.Slf4j; |
8 | import org.apache.commons.lang3.StringUtils; | 9 | import org.apache.commons.lang3.StringUtils; |
10 | +import org.springframework.data.redis.core.script.DefaultRedisScript; | ||
11 | +import org.springframework.data.redis.core.script.RedisScript; | ||
9 | import org.springframework.stereotype.Service; | 12 | import org.springframework.stereotype.Service; |
10 | 13 | ||
11 | import java.util.concurrent.TimeUnit; | 14 | import java.util.concurrent.TimeUnit; |
@@ -14,6 +17,8 @@ import java.util.concurrent.TimeUnit; | @@ -14,6 +17,8 @@ import java.util.concurrent.TimeUnit; | ||
14 | @Slf4j | 17 | @Slf4j |
15 | public class RedisLock { | 18 | public class RedisLock { |
16 | 19 | ||
20 | + private static final Long RELEASE_SUCCESS = 1L; | ||
21 | + | ||
17 | @Redis("gwNoSyncRedis") | 22 | @Redis("gwNoSyncRedis") |
18 | private YHRedisTemplate redis; | 23 | private YHRedisTemplate redis; |
19 | 24 | ||
@@ -40,15 +45,22 @@ public class RedisLock { | @@ -40,15 +45,22 @@ public class RedisLock { | ||
40 | try { | 45 | try { |
41 | deleteKeyIfValueEquals(key, value); | 46 | deleteKeyIfValueEquals(key, value); |
42 | } catch (Exception e1) { | 47 | } catch (Exception e1) { |
43 | - log.warn("release lock {} fail", key, e); | 48 | + log.warn("release lock fail, key is {} value is {}", key, value); |
44 | } | 49 | } |
45 | } | 50 | } |
46 | } | 51 | } |
47 | 52 | ||
48 | - private void deleteKeyIfValueEquals(RedisKeyBuilder key, String value) { | ||
49 | - String redisLockValue = valueOperations.get(key); | ||
50 | - if (value.equals(redisLockValue)) { | ||
51 | - redis.delete(key); | 53 | + private boolean deleteKeyIfValueEquals(RedisKeyBuilder keyBuilder, String value) { |
54 | + String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end"; | ||
55 | + RedisScript<Long> redisScript = new DefaultRedisScript(script, Long.class); | ||
56 | + String key = keyBuilder.getKey(); | ||
57 | + Long result = redis.getStringRedisTemplate().execute(redisScript, Lists.newArrayList(key), value); | ||
58 | + if (RELEASE_SUCCESS.equals(result)) { | ||
59 | + log.info("release lock ok, key is {} value is {}", key, value); | ||
60 | + return true; | ||
61 | + } else { | ||
62 | + log.info("release lock ko, key is {} value is {}", key, value); | ||
63 | + return false; | ||
52 | } | 64 | } |
53 | } | 65 | } |
54 | 66 |
-
Please register or login to post a comment