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 org.rx.bean.RxConfig;
import org.rx.core.cache.LocalCache;
import org.rx.core.cache.ThreadCache;
import org.rx.util.function.BiFunc;

import java.util.*;

import static org.rx.core.App.*;

public interface Cache {
    int NON_EXPIRE_MINUTES = 0;
    String LRU_CACHE = "LruCache";
    String LOCAL_CACHE = "localCache";
    String THREAD_CACHE = "ThreadCache";

    static  TV getOrSet(TK key, BiFunc supplier) {
        return getOrSet(key, supplier, NON_EXPIRE_MINUTES);
    }

    static  TV getOrSet(TK key, BiFunc supplier, int expireMinutes) {
        return getOrSet(key, supplier, expireMinutes, App.getConfig().getDefaultCache());
    }

    static  TV getOrSet(TK key, BiFunc supplier, String cacheName) {
        return getOrSet(key, supplier, NON_EXPIRE_MINUTES, cacheName);
    }

    static  TV getOrSet(TK key, BiFunc supplier, int expireMinutes, String cacheName) {
        require(key, supplier);

        return Cache.getInstance(cacheName).get(key, supplier, expireMinutes);
    }

    static  Cache getInstance() {
        return getInstance(App.getConfig().getDefaultCache());
    }

    static  Cache getInstance(String name) {
        return Container.getInstance().getOrRegister(name, () -> {
            switch (name) {
                case THREAD_CACHE:
                    return new ThreadCache<>();
                case LOCAL_CACHE:
                    return new LocalCache<>(4);
                default:
                    return new LocalCache<>();
            }
        });
    }

    long size();

    Set keySet();

    TV put(TK key, TV val);

    default TV put(TK key, TV value, int expireMinutes) {
        return put(key, value);
    }

    TV remove(TK key);

    default void remove(TK key, boolean destroy) {
        TV v = remove(key);
        if (destroy) {
            tryClose(v);
        }
    }

    void clear();

    TV get(TK key);

    TV get(TK key, BiFunc supplier);

    default TV get(TK key, BiFunc supplier, int expireMinutes) {
        return get(key, supplier);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy