
fiftyone.caching.LruLoadingCache Maven / Gradle / Ivy
/* *********************************************************************
* This Original Work is copyright of 51 Degrees Mobile Experts Limited.
* Copyright 2019 51 Degrees Mobile Experts Limited, 5 Charlotte Close,
* Caversham, Reading, Berkshire, United Kingdom RG4 7BY.
*
* This Original Work is licensed under the European Union Public Licence (EUPL)
* v.1.2 and is subject to its terms as set out below.
*
* If a copy of the EUPL was not distributed with this file, You can obtain
* one at https://opensource.org/licenses/EUPL-1.2.
*
* The 'Compatible Licences' set out in the Appendix to the EUPL (as may be
* amended by the European Commission) shall be deemed incompatible for
* the purposes of the Work and the provisions of the compatibility
* clause in Article 5 of the EUPL shall not apply.
*
* If using the Work as, or as part of, a network application, by
* including the attribution notice(s) required under Article 5 of the EUPL
* in the end user terms of the application under an appropriate heading,
* such notice(s) shall fulfill the requirements of that article.
* ********************************************************************* */
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