0

我想使用mapdb库来缓存 max n 对象。我写了这样的东西:

DB dbMemory = DBMaker
              .memoryDB()
              .make();

HTreeMap<Long, String> inMemory = dbMemory
              .hashMap("inMemory", Serializer.LONG, Serializer.STRING)
              .expireMaxSize(2)
              .create();

inMemory.put((long)1, "1");
inMemory.put((long)2, "2");
inMemory.put((long)3, "3");
inMemory.put((long)4, "4");

inMemory.getValues().forEach(val -> System.out.println(val));

我的预期结果应该是:

3
4

但我得到了(并不总是按这个顺序):

1
2
3
4

我确信这只是我对使用这个库的误解,所以有人可以告诉我我做错了什么吗?

4

1 回答 1

0

HTreeMap文档说:

基于时间的驱逐将始终将条目放入过期队列。但其他过期标准(大小和空间限制)也需要提示何时将条目放入过期队列。在以下示例中,没有条目被放入队列中,并且没有条目过期。

HTreeMap cache = db
    .hashMap("cache")
    .expireMaxSize(1000)
    .create();

所以你需要添加一个提示。该文件进一步说:

有三种可能的触发器会将条目放入过期队列expireAfterCreate()expireAfterUpdate()expireAfterGet()

添加这些 - 您可能需要创建和更新提示。

于 2018-12-11T16:22:34.483 回答