Search in sources :

Example 11 with RedisCallback

use of org.springframework.data.redis.core.RedisCallback in project dq-easy-cloud by dq-open-cloud.

the class EcLock method releaseDistributedLock.

/**
 * 释放分布式锁
 *
 * @return 是否释放成功
 */
private boolean releaseDistributedLock() {
    RedisCallback<Object> redisCallback = new RedisCallback<Object>() {

        @Override
        public Object doInRedis(RedisConnection connection) throws DataAccessException {
            String script = "if redis.call('get', KEYS[1]) == ARGV[1] then return redis.call('del', KEYS[1]) else return 0 end";
            Jedis jedis = (Jedis) connection.getNativeConnection();
            return jedis.eval(script, Collections.singletonList(lockName), Collections.singletonList(lockValue));
        }
    };
    Object result = stringRedisTemplateLock.execute(redisCallback);
    if (EcBaseUtils.equals(RELEASE_SUCCESS, result)) {
        return true;
    }
    return false;
}
Also used : Jedis(redis.clients.jedis.Jedis) RedisCallback(org.springframework.data.redis.core.RedisCallback) RedisConnection(org.springframework.data.redis.connection.RedisConnection)

Aggregations

RedisCallback (org.springframework.data.redis.core.RedisCallback)11 RedisConnection (org.springframework.data.redis.connection.RedisConnection)10 DataAccessException (org.springframework.dao.DataAccessException)7 StringRedisSerializer (org.springframework.data.redis.serializer.StringRedisSerializer)7 Jedis (redis.clients.jedis.Jedis)3 TimeUnit (java.util.concurrent.TimeUnit)1 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1 Cache (org.apache.ibatis.cache.Cache)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 RedisTemplate (org.springframework.data.redis.core.RedisTemplate)1 ValueOperations (org.springframework.data.redis.core.ValueOperations)1