org.grouplens.lenskit.eval.traintest.CachingDAOProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lenskit-eval Show documentation
Show all versions of lenskit-eval Show documentation
Facilities for evaluating recommender algorithms.
/*
* LensKit, an open source recommender systems toolkit.
* Copyright 2010-2014 LensKit Contributors. See CONTRIBUTORS.md.
* Work on LensKit has been funded by the National Science Foundation under
* grants IIS 05-34939, 08-08692, 08-12148, and 10-17697.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 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 General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.grouplens.lenskit.eval.traintest;
import com.google.common.base.Throwables;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.Maps;
import org.grouplens.lenskit.data.dao.*;
import javax.inject.Inject;
import javax.inject.Provider;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
/**
* Caching provider to reduce the duplication of DAO instances in evaluations.
*
* This assumes that each unique DAO object is unchanging. That may or may not be a safe
* assumption outside the evaluator!
*
* @since 2.1
* @author GroupLens Research
*/
public abstract class CachingDAOProvider implements Provider {
private static final ConcurrentMap,Cache> CACHE_MAP =
Maps.newConcurrentMap();
private static void registerProviderClass(Class> cls) {
Cache cache = CACHE_MAP.get(cls);
if (cache == null) {
cache = CacheBuilder.newBuilder()
.weakKeys()
.softValues()
.build();
CACHE_MAP.putIfAbsent(cls, cache);
}
}
private final EventDAO eventDAO;
CachingDAOProvider(EventDAO dao) {
registerProviderClass(getClass());
eventDAO = dao;
}
protected abstract T create(EventDAO dao);
@SuppressWarnings("unchecked")
@Override
public T get() {
Cache cache = CACHE_MAP.get(getClass());
assert cache != null;
try {
return (T) cache.get(eventDAO, new Callable
© 2015 - 2024 Weber Informatics LLC | Privacy Policy