0

这是代码:

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();
}
}

问题是 cache.get() 给出了错误:

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

get() 方法中究竟期望什么?我找不到任何示例,我不明白为什么它需要另一个函数。

4

0 回答 0