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

com.github.phantomthief.util.GlobalCache Maven / Gradle / Ivy

There is a newer version: 1.0.10
Show newest version
/**
 * 
 */
package com.github.phantomthief.util;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

/**
 * 
 * @author w.vela
 */
public final class GlobalCache implements IMultiDataAccess {

    private final Cache cache = CacheBuilder.newBuilder().weakKeys().weakValues().build();

    @Override
    public Map get(Collection keys) {
        Map result = new HashMap<>();
        for (K key : keys) {
            V value = cache.getIfPresent(key);
            if (value != null) {
                result.put(key, value);
            }
        }
        return result;
    }

    @Override
    public void set(Map dataMap) {
        cache.putAll(dataMap);
    };

    public void remove(K key) {
        cache.invalidate(key);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy