java – EhCache键类型

在EhCache中,向缓存添加元素时:
cache.put(new Element("key1","value1"));

// Element constructors :
Element(Object key,Object value)

我看到我可以给对象作为关键指标.

如何使用这个“复杂”键,由多个int((userId,siteId,…)组成,而不是一个字符串作为索引)

谢谢

解决方法

将它包装在一个新类中:
public class CacheKey implements Serializable {
    private int userId;
    private int siteId;
    //override hashCode() and equals(..) using all the fields (use your IDE)
}

然后(假设你已经定义了适当的构造函数):

cache.put(new Element(new CacheKey(userId,siteId),value);

对于简单的情况,您可以使用字符串连接:

cache.put(new Element(userId + ":" + siteId,value));

相关文章

应用场景 C端用户提交工单、工单创建完成之后、会发布一条工...
线程类,设置有一个公共资源 package cn.org.chris.concurre...
Java中的数字(带有0前缀和字符串)
在Java 9中使用JLink的目的是什么?
Java Stream API Filter(过滤器)
在Java中找到正数和负数数组元素的数量