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

sirius.biz.locks.RedisLockManager Maven / Gradle / Ivy

There is a newer version: 9.6
Show newest version
/*
 * Made with all the love in the world
 * by scireum in Remshalden, Germany
 *
 * Copyright by scireum GmbH
 * http://www.scireum.de - [email protected]
 */

package sirius.biz.locks;

import sirius.db.redis.Redis;
import sirius.kernel.di.std.Part;
import sirius.kernel.di.std.Register;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.time.Duration;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Provides a fast multi node implementation which is based on Redis.
 */
@Register(classes = LockManager.class)
public class RedisLockManager implements LockManager {

    /**
     * Contains the name of this lock manager
     */
    public static final String NAME = "redis";

    @Part
    private Redis redis;

    @Nonnull
    @Override
    public String getName() {
        return NAME;
    }

    @Override
    public boolean tryLock(@Nonnull String lockName, @Nullable Duration acquireTimeout) {
        return redis.tryLock(lockName, acquireTimeout, Duration.ofMinutes(30));
    }

    @Override
    public boolean isLocked(@Nonnull String lock) {
        return redis.isLocked(lock);
    }

    @Override
    public void unlock(String lock, boolean force) {
        redis.unlock(lock, force);
    }

    @Override
    public List getLocks() {
        return redis.getLockList().stream().map(this::transformLockInfo).collect(Collectors.toList());
    }

    private LockInfo transformLockInfo(Redis.LockInfo redisLock) {
        return new LockInfo(redisLock.name, redisLock.value, "-", redisLock.since);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy