package com.hubspot.singularity.data;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.LoadingCache;
import com.google.inject.Inject;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import javax.annotation.Nonnull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ManagerCache<K, V> {
  private static final Logger LOG = LoggerFactory.getLogger(ManagerCache.class);

  public final boolean isEnabled;
  private final LoadingCache<K, V> cache;

  @Inject
  public ManagerCache(boolean isEnabled, int cacheTtl, Function<? super K, V> loader) {
    this.isEnabled = isEnabled;
    cache =
      Caffeine
        .newBuilder()
        .expireAfterWrite(cacheTtl, TimeUnit.SECONDS)
        .build(loader::apply);
  }

  public V get(K key) {
    V values = cache.get(key);
    if (values != null) {
      LOG.trace("Grabbed values for {} from cache", key);
    } else {
      LOG.trace("{} not in cache, setting", key);
    }

    return values;
  }

  public Map<K, V> getAll(@Nonnull Iterable<? extends K> keys) {
    Map<K, V> values = cache.getAll(keys);
    if (!values.isEmpty()) {
      LOG.trace("Grabbed mapped values for {} from cache", keys);
    }

    return values;
  }

  public boolean isEnabled() {
    return isEnabled;
  }
}

@@ -905,37 +905,37 @@
 ll) %7B%0A      LOG.
-trace
+debug
 (%22Grabbed values
@@ -984,21 +984,21 @@
     LOG.
-trace
+debug
 (%22%7B%7D not
@@ -1206,13 +1206,13 @@
 LOG.
-trace
+debug
 (%22Gr
@@ -1216,16 +1216,19 @@
 %22Grabbed
+ %7B%7D
  mapped 
@@ -1225,39 +1225,32 @@
 %7B%7D mapped values
- for %7B%7D
  from cache%22, ke
@@ -1247,20 +1247,29 @@
 cache%22, 
-keys
+values.size()
 );%0A    %7D

update logging