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

org.spf4j.concurrent.UnboundedLoadingCache Maven / Gradle / Ivy

Go to download

A continuously growing collection of utilities to measure performance, get better diagnostics, improve performance, or do things more reliably, faster that other open source libraries...

There is a newer version: 8.10.0
Show newest version
/*
 * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
 *
 * This library 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 library 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 Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Additionally licensed with:
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.spf4j.concurrent;

import com.google.common.cache.CacheLoader;
import com.google.common.cache.CacheStats;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableMap;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Collection;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.ExecutionException;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import org.spf4j.base.Callables;
import org.spf4j.base.UncheckedExecutionException;

/**
 *
 * custom build high performance implementation for a unbounded guava cache: UnboundedLoadingCache is implemented with
 * JDK concurrent map UnboundedLoadingCache2 is using the JDK 1.8 computing map functionality, but benchmarks show worse
 * performance.
 *
 * Benchmark Mode Cnt Score Error Units CacheBenchmark.guavaCache thrpt 15 29011674.275 ± 710672.413 ops/s
 * CacheBenchmark.spf4j2Cache thrpt 15 30567248.015 ± 807965.535 ops/s CacheBenchmark.spf4jCache thrpt 15 37961593.882 ±
 * 1136244.254 ops/s CacheBenchmark.spf4jRacyCache thrpt 15 37553655.751 ± 855349.501 ops/s
 *
 * @author zoly
 */
@ParametersAreNonnullByDefault
public final class UnboundedLoadingCache implements LoadingCache {

  private final ConcurrentMap> map;

  private final CacheLoader loader;

  public UnboundedLoadingCache(final int initialSize, final CacheLoader loader) {
    this(initialSize, 8, loader);
  }

  public UnboundedLoadingCache(final int initialSize, final int concurrency, final CacheLoader loader) {
    this.map = new ConcurrentHashMap<>(
            initialSize, 0.75f, concurrency);
    this.loader = loader;
  }

  /**
   * Will use a ConcurrentSkipListMap to store the underlying data.
   * @param comparator
   * @param loader
   */
  public UnboundedLoadingCache(final Comparator comparator, final CacheLoader loader) {
    this.map = new ConcurrentSkipListMap<>(comparator);
    this.loader = loader;
  }


  @Override
  public V get(final K key) throws ExecutionException {
    Callable existingValHolder = map.get(key);
    if (existingValHolder == null) {
      Callable newHolder = Callables.memorized(() -> loader.load(key));
      existingValHolder = map.putIfAbsent(key, newHolder);
      if (existingValHolder == null) {
        existingValHolder = newHolder;
      }
    }
    try {
      return existingValHolder.call();
    } catch (Exception ex) {
      throw new ExecutionException(ex);
    }

  }

  @Override
  public V getUnchecked(final K key) {
    try {
      return get(key);
    } catch (ExecutionException ex) {
      throw new UncheckedExecutionException(ex);
    }
  }

  @Override
  @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED")
  public ImmutableMap getAll(final Iterable keys) throws ExecutionException {
    ImmutableMap.Builder builder = ImmutableMap.builder();
    for (K key : keys) {
      builder.put(key, get(key));
    }
    return builder.build();
  }

  @Override
  @SuppressFBWarnings("NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE")
  public V apply(final K key) {
    return getUnchecked(key);
  }

  @Override
  public void refresh(final K key) {
    Callable newHolder = Callables.memorized(new Callable() {
      @Override
      public V call() throws Exception {
        return loader.load(key);
      }
    });
    map.put(key, newHolder);
  }

  @Override
  public ConcurrentMap asMap() {
    return new MapView();
  }

  @Override
  @Nullable
  public V getIfPresent(final Object key) {
    Callable existingValHolder = map.get(key);
    if (existingValHolder != null) {
      try {
        return existingValHolder.call();
      } catch (Exception ex) {
        throw new UncheckedExecutionException(ex);
      }
    } else {
      return null;
    }
  }

  @Override
  public V get(final K key, final Callable valueLoader) throws ExecutionException {
    Callable existingValHolder = map.get(key);
    if (existingValHolder == null) {
      Callable newHolder = Callables.memorized(valueLoader);
      existingValHolder = map.putIfAbsent(key, newHolder);
      if (existingValHolder == null) {
        existingValHolder = newHolder;
      }
    }
    try {
      return existingValHolder.call();
    } catch (Exception ex) {
      throw new ExecutionException(ex);
    }
  }

  @Override
  @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED")
  public ImmutableMap getAllPresent(final Iterable keys) {
    ImmutableMap.Builder builder = ImmutableMap.builder();
    for (K key : (Iterable) keys) {
      V val = getIfPresent(key);
      if (val != null) {
        builder.put(key, val);
      }
    }
    return builder.build();
  }

  @Override
  public void put(final K key, final V value) {
    map.put(key, Callables.constant(value));
  }

  @Override
  public void putAll(final Map m) {
    for (Map.Entry entry : m.entrySet()) {
      put(entry.getKey(), entry.getValue());
    }
  }

  @Override
  public void invalidate(final Object key) {
    map.remove(key);
  }

  @Override
  public void invalidateAll(final Iterable keys) {
    for (K key : (Iterable) keys) {
      invalidate(key);
    }
  }

  @Override
  public void invalidateAll() {
    map.clear();
  }

  @Override
  public long size() {
    return map.size();
  }

  @Override
  public CacheStats stats() {
    throw new UnsupportedOperationException("Not supported");
  }

  @Override
  @SuppressFBWarnings("NM_CONFUSING")
  public void cleanUp() {
    map.clear();
  }

  public Set getKeysLoaded() {
    return map.keySet();
  }

  @Override
  public String toString() {
    return "UnboundedLoadingCache{" + "map=" + map + ", loader=" + loader + '}';
  }

  private class MapView implements ConcurrentMap {

    @Override
    public V putIfAbsent(final K key, final V value) {
      Callable result = map.putIfAbsent(key, Callables.constant(value));
      try {
        return result == null ? null : result.call();
      } catch (Exception ex) {
        throw new UncheckedExecutionException(ex);
      }
    }

    @Override
    public boolean remove(final Object key, final Object value) {
      throw new UnsupportedOperationException();
    }

    @Override
    public boolean replace(final K key, final V oldValue, final V newValue) {
      throw new UnsupportedOperationException();
    }

    @Override
    public V replace(final K key, final V value) {
      throw new UnsupportedOperationException();
    }

    @Override
    public int size() {
      return map.size();
    }

    @Override
    public boolean isEmpty() {
      return map.isEmpty();
    }

    @Override
    public boolean containsKey(final Object key) {
      return map.containsKey(key);
    }

    @Override
    public boolean containsValue(final Object value) {
      throw new UnsupportedOperationException();
    }

    @Override
    public V get(final Object key) {
      Callable result = map.get(key);
      try {
        return result == null ? null : result.call();
      } catch (Exception ex) {
        throw new UncheckedExecutionException(ex);
      }
    }

    @Override
    public V put(final K key, final V value) {
      Callable result = map.put(key, Callables.constant(value));
      try {
        return result == null ? null : result.call();
      } catch (Exception ex) {
        throw new UncheckedExecutionException(ex);
      }
    }

    @Override
    public V remove(final Object key) {
      Callable result = map.remove(key);
      try {
        return result == null ? null : result.call();
      } catch (Exception ex) {
        throw new UncheckedExecutionException(ex);
      }
    }

    @Override
    public void putAll(final Map m) {
      for (Map.Entry entry : m.entrySet()) {
        map.put(entry.getKey(), entry::getValue);
      }
    }

    @Override
    public void clear() {
      map.clear();
    }

    @Override
    public Set keySet() {
      return map.keySet();
    }

    @Override
    public Collection values() {
      throw new UnsupportedOperationException();
    }

    @Override
    public Set> entrySet() {
      throw new UnsupportedOperationException();
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy