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

shz.core.cache.Cache Maven / Gradle / Ivy

package shz.core.cache;

import shz.core.NullHelp;

import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;

public interface Cache {
    V get(K key);

    void put(K key, V val, long expireMills);

    default void put(K key, V val) {
        put(key, val, 0L);
    }

    boolean containsKey(K key);

    int size();

    Set keys();

    Set keys(Predicate predicate);

    void delete(K key);

    default void delete(Set keys) {
        if (NullHelp.isEmpty(keys)) return;
        keys.forEach(this::delete);
    }

    default void delete(Predicate predicate) {
        delete(keys(predicate));
    }

    default long ttlMillis(K key) {
        return 0L;
    }

    void clear();

    default V computeIfAbsent(K key, Function mappingFunction) {
        V v;
        if ((v = get(key)) == null) {
            synchronized (this) {
                if ((v = get(key)) == null) {
                    V newValue;
                    if ((newValue = mappingFunction.apply(key)) != null) {
                        put(key, newValue);
                        return newValue;
                    }
                }
            }
        }
        return v;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy