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

org.babyfish.jimmer.sql.cache.chain.LockableBinder Maven / Gradle / Ivy

There is a newer version: 0.9.19
Show newest version
package org.babyfish.jimmer.sql.cache.chain;

import org.babyfish.jimmer.sql.cache.CacheLocker;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.time.Duration;

public interface LockableBinder extends SimpleBinder {

    @NotNull
    String keyPrefix();

    @NotNull
    default SimpleBinder hardLock(
            @Nullable CacheLocker cacheLocker,
            @Nullable Duration leaseDuration
    ) {
        return lock(cacheLocker, null, leaseDuration);
    }

    @NotNull
    default SimpleBinder softLock(
            @Nullable CacheLocker cacheLocker,
            @Nullable Duration leaseDuration
    ) {
        return lock(cacheLocker, Duration.ZERO, leaseDuration);
    }

    @NotNull
    default SimpleBinder lock(
            @Nullable CacheLocker cacheLocker,
            @Nullable Duration waitDuration,
            @Nullable Duration leaseDuration
    ) {
        if (cacheLocker == null) {
            return this;
        }
        if (this instanceof LockableBinder.Parameterized) {
            return new ParameterizedLockedSimpleBinder<>(
                    (LockableBinder.Parameterized) this,
                    cacheLocker,
                    waitDuration,
                    leaseDuration
            );
        }
        return new LockedSimpleBinder<>(
                this,
                cacheLocker,
                waitDuration,
                leaseDuration
        );
    }

    interface Parameterized extends LockableBinder, SimpleBinder.Parameterized {

        @NotNull
        default SimpleBinder.Parameterized hardLock(
                @Nullable CacheLocker cacheLocker,
                @Nullable Duration leaseDuration
        ) {
            return lock(cacheLocker, Duration.ZERO, leaseDuration);
        }

        @NotNull
        default SimpleBinder softLock(
                @Nullable CacheLocker cacheLocker,
                @Nullable Duration leaseDuration
        ) {
            return lock(cacheLocker, Duration.ZERO, leaseDuration);
        }

        @NotNull
        default  SimpleBinder.Parameterized lock(
                @Nullable CacheLocker cacheLocker,
                @Nullable Duration waitDuration,
                @Nullable Duration leaseDuration
        ) {
            if (cacheLocker == null) {
                return this;
            }
            return new ParameterizedLockedSimpleBinder<>(
                    this,
                    cacheLocker,
                    waitDuration,
                    leaseDuration
            );
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy