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

fiftyone.caching.LruLoadingCache Maven / Gradle / Ivy

/* ********************************************************************
 * Copyright (C) 2019  51Degrees Mobile Experts Limited.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 * ******************************************************************** */

package fiftyone.caching;

import java.io.IOException;

public class LruLoadingCache extends LruCacheBase implements LoadingCache {

    private ValueLoader loader;

    LruLoadingCache(int cacheSize) {
        super(cacheSize, Runtime.getRuntime().availableProcessors(), false);
    }

    LruLoadingCache(int cacheSize, int concurrency) {
        super(cacheSize, concurrency, false);
    }

    public LruLoadingCache(int cacheSize, ValueLoader loader) {
        this(cacheSize, loader, Runtime.getRuntime().availableProcessors());
    }

    LruLoadingCache(int cacheSize, ValueLoader loader, int concurrency) {
        super(cacheSize, concurrency, false);
        setCacheLoader(loader);
    }

    /**
     * Loader used to fetch items not in the cache.
     */
    public void setCacheLoader(ValueLoader loader) {
        this.loader = loader;
    }

    /**
     * Retrieves the value for key requested. If the key does not exist
     * in the cache then the Fetch method of the cache's loader is used to
     * retrieve the value.
     *
     * @param key or the item required.
     * @return An instance of the value associated with the key.
     * @throws java.lang.IllegalStateException if there was a problem accessing data file.
     */
    @Override
    public V get(K key) {
        try {
            return get(key, loader);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }


    /**
     * Retrieves the value for key requested. If the key does not exist
     * in the cache then the Fetch method is used to retrieve the value
     * from another loader.
     *
     * @param key    or the item required
     * @param loader to fetch the items from
     * @return An instance of the value associated with the key
     * @throws java.io.IOException if there was a problem accessing data file.
     */
    public V get(K key, ValueLoader loader) throws IOException {
        boolean added = false;
        V result = super.get(key);

        if (result == null) {
            result = loader.load(key);
            super.add(key, result);
        }
        return result;
    }

    public static class Builder implements LoadingCacheBuilder {

        @Override
        public  Cache build(Cache c, int cacheSize) {
            return new LruLoadingCache(cacheSize);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy