java - EhCache key 类型

标签 java ehcache

在 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/

相关文章:

java - hibernatetemplate 清除ehcache?

java - ehcache hibernate 4

caching - Ehcache - 对象是否永恒

java - expected=JSONException.class 没有捕捉到

java - 为什么在 Java 中将 Integer 添加到 String 数组不会产生编译错误?

java - 需要从中获取 JSONObjects

java - 将独特的图像添加到 android 中的微调行

java - 如何增加 Java 6 的最大堆大小?

grails - Grails cache-ehcache无法正常工作

java.lang.UnsatisfiedLinkError no *****.dll in java.library.path