Search in sources :

Example 46 with RedisConnection

use of org.springframework.data.redis.connection.RedisConnection 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

RedisConnection (org.springframework.data.redis.connection.RedisConnection)46 RedisCallback (org.springframework.data.redis.core.RedisCallback)10 RedisConnectionFailureException (org.springframework.data.redis.RedisConnectionFailureException)9 RedisConnectionFactory (org.springframework.data.redis.connection.RedisConnectionFactory)9 Properties (java.util.Properties)8 DataAccessException (org.springframework.dao.DataAccessException)7 StringRedisSerializer (org.springframework.data.redis.serializer.StringRedisSerializer)7 Test (org.junit.Test)5 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)4 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)4 ExpiringOAuth2RefreshToken (org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken)3 Jedis (redis.clients.jedis.Jedis)3 com.alicp.jetcache (com.alicp.jetcache)2 AbstractExternalCache (com.alicp.jetcache.external.AbstractExternalCache)2 java.util (java.util)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 TimeUnit (java.util.concurrent.TimeUnit)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Function (java.util.function.Function)2