All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ltd.fdsa.redis.lock.RedisLockManager Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
package ltd.fdsa.redis.lock;

import lombok.extern.slf4j.Slf4j;
import ltd.fdsa.core.lock.LockManager;
import ltd.fdsa.redis.properties.RedisConfigProperties;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.integration.redis.util.RedisLockRegistry;

import java.util.concurrent.locks.Lock;

/**
 * 基于Redis的互斥锁
 */
@Slf4j
public class RedisLockManager implements LockManager {

    private static RedisLockRegistry registry;
    private final RedisConfigProperties properties;
    private final RedisConnectionFactory redisConnectionFactory;

    public RedisLockManager(RedisConfigProperties properties, RedisConnectionFactory redisConnectionFactory) {
        this.properties = properties;
        this.redisConnectionFactory = redisConnectionFactory;
    }


    @Override
    public Lock getLock(String lockKey) {
        if (registry == null) {
            synchronized (registry) {
                if (registry == null) {
                    registry = new RedisLockRegistry(redisConnectionFactory, this.properties.getCacheKey());
                }
            }
        }
        return registry.obtain(lockKey);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy