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

commons.box.app.misc.LocalDataStore Maven / Gradle / Ivy

The newest version!
package commons.box.app.misc;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import commons.box.app.DataInit;
import commons.box.app.DataStore;
import commons.box.util.Types;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

/**
 * 基于 Guava 实现的本次 LocalStore
 */
public class LocalDataStore extends DataInit implements DataStore {
    private Cache cache;
    private int size = 0;
    private int timeout = -1;
    private TimeUnit timeUnit;

    private Function provider;

    public LocalDataStore() {
        this(0, -1, null, null);
    }

    public LocalDataStore(Function provider) {
        this(0, -1, null, provider);
    }

    public LocalDataStore(int size, int timeout, TimeUnit timeUnit) {
        this(size, timeout, timeUnit, null);
    }

    public LocalDataStore(int size, int timeout, TimeUnit timeUnit, Function provider) {
        this.size = size;
        this.timeout = timeout;
        this.timeUnit = timeUnit;
        this.provider = provider;
    }

    @Override
    protected void internalInit() {
        CacheBuilder builder = CacheBuilder.newBuilder();
        if (this.size > 0) builder.maximumSize(this.size);
        if (this.timeout >= 0) builder.expireAfterWrite(this.timeout, (this.timeUnit != null) ? this.timeUnit : TimeUnit.MILLISECONDS);
        this.cache = builder.build();
    }

    @Override
    public O get(K key) {
        this.init();

        if (key == null) return null;
        O obj = this.cache.getIfPresent(key);
        if (obj == null && this.provider != null) {
            obj = provider.apply(key);
            if (obj != null) this.set(key, obj);
        }
        return obj;
    }

    @Override
    public void set(K key, O value) {
        this.init();

        if (key == null) return;
        this.cache.put(key, value);
    }

    @Override
    public void remove(K key) {
        this.init();

        if (key == null) return;
        this.cache.invalidate(key);
    }

    public int getSize() {
        return size;
    }

    public int getTimeout() {
        return timeout;
    }

    public TimeUnit getTimeUnit() {
        return timeUnit;
    }

    public Function getProvider() {
        return provider;
    }

    public LocalDataStore withSize(int size) {
        this.size = size;
        this.reset();
        return this;
    }

    public LocalDataStore withTimeout(int timeout, TimeUnit timeUnit) {
        this.timeout = timeout;
        this.timeUnit = timeUnit;
        this.reset();
        return this;
    }

    public LocalDataStore withProvider(Function provider) {
        this.provider = provider;
        this.reset();
        return this;
    }

    public static void main(String[] args) {
        Object[] op = new Object[]{"aaa", "bbb"};
        Object[] op1 = new Object[]{"aaa", "bbbc"};
        for (int j = 0; j < 10; j++) {
            Types.isType(Map.class, LinkedHashMap.class);
            TimeMetric.inst(t -> {
                for (int i = 0; i < 1000000; i++) {
                    boolean type = Types.isType(Map.class, (i % 2 == 0) ? LinkedHashMap.class : Map.class);
                    if (!type) continue;
                }
            }).print("测试 百万次 class type");


            Map mo = new LinkedHashMap<>();
            Types.isType(Map.class, mo);

            TimeMetric.inst(t -> {
                for (int i = 0; i < 1000000; i++) {
                    boolean type = Types.isType(Map.class, mo);
                    if (!type) continue;
                }
            }).print("测试 百万次 class object");

            Map map = new LinkedHashMap<>();
            for (int i = 0; i < 100000; i++) map.put(i, i);

            TimeMetric.inst(t -> {
                for (int i = 0; i < 1000000; i++) {
                    Object p = map.get(i);
                    if (p == null) {
                    }
                }
            }).print("测试 百万次 map object");

            String p = "aaa";
            String b = "bbb";

            TimeMetric.inst(t -> {
                for (int i = 0; i < 1000000; i++) {
                    if (p.equals(b)) {
                    }
                }
            }).print("测试 百万次 array object");


            TimeMetric.inst(t -> {
                for (int i = 0; i < 1000000; i++) {
                    if (Arrays.equals(op, op1)) {
                    }
                }
            }).print("测试 百万次 equals object");
        }

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy