在 EhCache 中,向缓存添加元素时:
cache.put(new Element("key1", "value1"));
// Element constructors :
Element(Object key, Object value)
我知道我可以给一个Object
作为关键指标。
我如何使用它来获得一个由多个 int 组成的“复杂” key :(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));
关于java - EhCache key 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5908619/