Expiry of items in-memory cache not working.

238 views
Skip to first unread message

Santoor

unread,
Oct 4, 2015, 10:11:43 PM10/4/15
to MapDB
I'm experimenting in-memory cache of MapDB. I set expiry time for the entries. MapDB returns expired entries when I do get even after expiry. Following is my code.

public static void main(String args[]) throws InterruptedException {

 


 

 
Date start = new Date();

 
System.out.println("Start: " + start);


 

 DB db
= DBMaker.memoryDB().transactionDisable().cacheHashTableEnable()

 
.cacheSize(20).closeOnJvmShutdown().make();


 

 
// Create a Hash Map in DB.

 
HTreeMap<String, Object> map = db.hashMapCreate("test").counterEnable()

 
.expireAfterAccess(2, TimeUnit.SECONDS)

 
.expireAfterWrite(2, TimeUnit.SECONDS)

 
.keySerializer(Serializer.STRING).makeOrGet();


 

 map
.put("key", "value");


 

 db
.commit(); // persist changes into disk


 

 
System.out.println(map.get("key"));


 

 
Thread.sleep(5000);


 

 
System.out.println(map.get("key"));


 

 db
.close();


 

 
Date end = new Date();

 
System.out.println("End: " + end);


 

 
System.out.println("Total Time in s: "

 
+ (end.getTime() - start.getTime()) / 1000);


 

 
}


Output


Start: Sun Oct 04 21:57:46 EDT 2015
value
value
End: Sun Oct 04 21:57:51 EDT 2015
Total Time in s: 5

Santoor

unread,
Oct 4, 2015, 11:16:10 PM10/4/15
to MapDB
I just found executorEnable in this page http://www.mapdb.org/doc/concurrency.html. I get correct output after using executor. However, I'm not sure if I must global executor. I don't need background processing in my application except for may this expiry items case. 

Santoor

unread,
Oct 5, 2015, 12:04:33 AM10/5/15
to MapDB
I apologize for several messages. I now tried with expireAfterWrite = 2 seconds and expireAfterAccess = 10 seconds. The entry never expired. Then I set expireAfterWrite = 2, expireAfterAccess = 0. Entry expired after 2 seconds. Is this a bug or my understanding is wrong w.r.t expiry ?

Another question is how to use expiration when using disk caching with HTreeMap. I see following error when I create fileDB with executorEnable

WARNING: HTreeMap Expiration should not be used with transaction enabled. It can lead to data corruption, commit might happen while background thread works, and only part of expiration data will be commited.

Jan Kotek

unread,
Oct 6, 2015, 5:05:47 AM10/6/15
to ma...@googlegroups.com

Expiration times are approximate. It would be too big overhead to do exact calculation, so MapDB expires entries as best effort. Actual numbers could be a few hundreds different.

 

Jan

--
You received this message because you are subscribed to the Google Groups "MapDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mapdb+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



Reply all
Reply to author
Forward
0 new messages