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

org.rx.core.Cache Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package org.rx.core;

import lombok.NonNull;
import lombok.SneakyThrows;
import org.rx.bean.AbstractMap;
import org.rx.core.cache.MemoryCache;
import org.rx.util.function.BiFunc;

import static org.rx.core.Constants.NON_RAW_TYPES;

public interface Cache extends AbstractMap {
    Object NULL_VALUE = new Object();

    static  TV getOrSet(TK key, BiFunc loadingFunc) {
        return getOrSet(key, loadingFunc, null);
    }

    static  TV getOrSet(@NonNull TK key, @NonNull BiFunc loadingFunc, CachePolicy expiration) {
        return Cache.getInstance().get(key, loadingFunc, expiration);
    }

    static  Cache getInstance() {
        return IOC.get(Cache.class);
    }

    /**
     * @param cacheType MEMORY_CACHE, DISK_CACHE
     * @param 
     * @param 
     * @return
     */
    @SuppressWarnings(NON_RAW_TYPES)
    static  Cache getInstance(Class cacheType) {
        return IOC.get(cacheType, (Class) MemoryCache.class);
    }

    default TV get(TK key, BiFunc loadingFunc) {
        return get(key, loadingFunc, null);
    }

    @SneakyThrows
    default TV get(TK key, BiFunc loadingFunc, CachePolicy policy) {
        TV v;
        if ((v = get(key)) == null) {
            TV newValue;
            if ((newValue = loadingFunc.invoke(key)) != null) {
                put(key, newValue, policy);
                return newValue;
            }
        }
        return v;
    }

    @Override
    default TV put(TK key, TV value) {
        return put(key, value, null);
    }

    TV put(TK key, TV value, CachePolicy policy);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy