0

Here's the code:

package misc;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.Cache;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

@State(Scope.Thread)
public class caff {

public Cache<String, Integer> cache = Caffeine.newBuilder().build();
public ArrayList<String> k = new ArrayList<>();
int p;

@Setup(Level.Iteration)
public void setup(){
    k.clear();
    //cache.clear();
    for(int m=0;m<1000;m++){
        int j=ThreadLocalRandom.current().nextInt(0,10000);
        String jk=Integer.toString(j);
        k.add(jk);
        cache.put(jk,j);
    }
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
@OperationsPerInvocation(10)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 3)
@Measurement(iterations = 100)
public void putKey() {
    int n= ThreadLocalRandom.current().nextInt(0,10000);
    String nk=Integer.toString(n);
    k.add(nk);
    cache.put(nk,n);
}

@Benchmark
@BenchmarkMode(Mode.Throughput)
@OperationsPerInvocation(10)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 3)
@Measurement(iterations = 100)
public void getKey() {
    String j;
    p = ThreadLocalRandom.current().nextInt(0, 10000);
    p = p % k.size();
    j = k.get(p);
    cache.get(j);
}

public static void main(String[] args) throws RunnerException {
    Options opt = new OptionsBuilder()
            .include(".*" + caff.class.getSimpleName() + ".*")
            .forks(1)
            //.addProfiler(GCProfiler.class)
            .jvmArgs("-Xmx1025M")
            .build();
    new Runner(opt).run();
}
}

The problem is that the cache.get() gives the error:

Error:(60, 14) java: method get in interface     com.github.benmanes.caffeine.cache.Cache<K,V> cannot be applied to given types;
  required: java.lang.String,java.util.function.Function<? super java.lang.String,? extends java.lang.Integer>
  found: java.lang.String
  reason: actual and formal argument lists differ in length

What exactly is expected in the get() method? I couldn't find any examples, and I don't understand why it would require another function.

Saurav Sircar
  • 1,743
  • 2
  • 12
  • 12
  • 1
    The signature for [Cache.get](https://github.com/ben-manes/caffeine/blob/master/caffeine/src/main/java/com/github/benmanes/caffeine/cache/Cache.java#L82) takes a key `K`, and a mapping `Function`. You're only providing a key. The javadocs seem fairly clear to me as to how these two arguments work together; if there's something you don't get about them, you'll need to be more specific. – yshavit Jun 20 '16 at 05:15
  • Sounds like you meant to use `getIfPresent(K)` – Ben Manes Jun 20 '16 at 05:21

0 Answers0