简体   繁体   English

如何在Java中缓存具有可变参数(Varargs)的函数的结果

[英]How to cache result for a function with variable arguments (Varargs) in Java

I have Java method like this: 我有这样的Java方法:

  public <T> T queryDB(Class<T> klass, Object... objects) {
    return dbClient.get(klass, objects);
  }

How can I cache this call? 如何缓存此呼叫? It is using variable arguments. 它使用可变参数。 I am not sure how to correctly build a cache key? 我不确定如何正确构建缓存键?

If you return some value based on your klass and object variables that means you have a composite key of klass and object . 如果您基于klassobject变量返回一些值,则意味着您具有klassobject的复合键。

What you can can do is to encapsulate these objects into a class, say, MyKey : 您可以做的就是将这些对象封装到一个类中,例如MyKey

      public class MyKey <T> {
             Class<T> klass;
             Object[] objects;

             public MyKey(Class<T> klass, Object... objects) {
                    this.klass = klass;
                    this.objects = objects;
             }

             @Override
             public boolean equals(Object obj) {
                    //implement this method comparing klass and objects
             }

             @Override
             public int hashCode() {
                    //calculate a hash based on klass and objects' hashes
             }
       }

Override equals() and hashCode() methods of this class (mandatory!) that calculate their values based on klass and objects variables . 覆盖此类的强制equals()hashCode()方法(强制!),这些方法基于klass和objects variables计算其值。
Note : objects is an array, so you have to use Arrays.equals() method to compare it. 注意objects是一个数组,因此必须使用Arrays.equals()方法进行比较。

And finally, create a HashMap with your MyKey class as a key and the cached value as the map's value: 最后,创建一个HashMap其中将MyKey类作为键,并将缓存的值作为地图的值:

Map<MyKey<T>, T> cache = new HashMap<>();

Each time you want to check if the value is hashed, call 每次您要检查该值是否为哈希值时,请调用

cache.get(new MyKey(klass, objects));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查找作为变量 arguments (varargs) 参数传递的数组总和,然后将结果作为数组返回 Java - Find Sum of array passed as variable arguments (varargs) parameter then return result as an array in Java 如何在 Java 中将 arguments 作为可变参数动态传递 - How to dynamically pass arguments as varargs in Java 以可变参数作为参数的类构造函数,存储为类属性并将可变参数作为函数结果返回 - class contructor with varargs as arguments, store as class attribute and return varargs as function result 在Java中,可以使用变量参数而不使用varargs工具吗? - In Java, can you use variable arguments without using varargs facility? 如何在编译时验证变量 Arguments (Varargs) 的长度? - How to validate the length of Variable Arguments (Varargs) at compile time? 可变参数(varargs)Java用法 - Variable aguments(varargs) java usage Java中varargs的最大参数个数是多少? - What is the maximum of number of arguments for varargs in java? Java多参数点符号 - Varargs - Java multiple arguments dot notation - Varargs varargs作为java 8中函数的输入参数 - varargs as input parameter to a function in java 8 如何在函数内部定位 Java 中 Varargs 方法的元素? - How to target the elements of the Varargs method in Java inside the function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM